Skip to content

Commit

Permalink
#60 - add getuser helper (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKermes authored Jul 1, 2024
1 parent 18d2381 commit 8b6373c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
4 changes: 3 additions & 1 deletion config/blt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
return [
"namespaces" => [
"default" => "App\\",
"types" => [],
"types" => [
"User" => "App\Models\User",
],
],
];
4 changes: 3 additions & 1 deletion src/Features/Traits/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Blumilk\BLT\Features\Traits;

use Behat\Gherkin\Node\TableNode;
use Blumilk\BLT\Helpers\UserHelper;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Container\BindingResolutionException;
use PHPUnit\Framework\Assert;
Expand All @@ -21,7 +22,8 @@ trait Authentication
public function userIsAuthenticatedInSessionAs(string $value, string $field): void
{
$auth = $this->getContainer()->make(Guard::class);
$auth->login($this->getUserModel()::query()->where($field, $value)->first());
$user = UserHelper::getBy($field, $value);
$auth->login($user);
}

/**
Expand Down
38 changes: 19 additions & 19 deletions src/Helpers/RecognizeClassHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ public static function recognizeObjectClass(string $objectName): string
return $objectName;
}

$objectName = Str::ucfirst(Str::singular($objectName));
$typeNamespaces = config("blt.namespaces.types");
$objectName = Str::lcfirst($objectName);

return self::getObjectNamespace($objectName) . $objectName;
}
if (array_key_exists($objectName, $typeNamespaces)) {
return $typeNamespaces[$objectName];
}

public static function getObjectNamespace(string $objectName): string
{
$type = self::guessType($objectName);
$typeNamespace = self::getNamespaceFromConfig($objectName, $type);
$objectName = Str::ucfirst($objectName);

if ($typeNamespace) {
return $typeNamespace;
if (array_key_exists($type, $typeNamespaces)) {
return $typeNamespaces[$type] . $objectName;
}

return self::getObjectNamespace(Str::singular($objectName), $type) . $objectName;
}

public static function getObjectNamespace(string $objectName, string $type): string
{
$type = Str::plural(Str::ucfirst($type));
$defaultNamespace = config("blt.namespaces.default", "App\\");
$defaultNamespace = config("blt.namespaces.default") ?? "App\\";

return $defaultNamespace . $type . "\\";
}
Expand All @@ -39,18 +44,13 @@ public static function guessType(string $objectName): string
$slug = Str::slug($objectName);

foreach (TypesEnum::cases() as $objectType) {
if (Str::contains($slug, $objectType->value)) {
return Str::singular($objectType->value);
$objectTypeName = $objectType->value;

if (Str::contains($slug, $objectTypeName)) {
return Str::singular($objectTypeName);
}
}

return "model";
}

protected static function getNamespaceFromConfig(string $objectName, string $type): ?string
{
$typeNamespaces = config("blt.namespaces.types");

return $typeNamespaces[$objectName] ?? $typeNamespaces[$type] ?? null;
return TypesEnum::Model->value;
}
}
22 changes: 22 additions & 0 deletions src/Helpers/UserHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Helpers;

class UserHelper
{
public static function getBy(string $field, string $value): ?object
{
$userClass = RecognizeClassHelper::recognizeObjectClass("User");

return $userClass::query()->where($field, $value)->first();
}

public static function getByEmail(string $value): ?object
{
$userClass = RecognizeClassHelper::recognizeObjectClass("User");

return $userClass::query()->where("email", $value)->first();
}
}

0 comments on commit 8b6373c

Please sign in to comment.