Add ValidationByName and CompanyValidationByName.
continuous-integration/drone/push Build is passing Details

main
KKlochko 2 years ago
parent 135f4c0c93
commit f437d777a5

@ -21,6 +21,8 @@ use App\DotsAPI\Fetcher\v2\AuthApiFetcher;
use App\DotsAPI\Fetcher\v2\AuthApiSender;
use App\Http\Resources\API\v2\CartItemCollection;
use App\Models\Validation\CompanyValidationByName;
class CartController extends Controller
{
/**
@ -165,9 +167,9 @@ class CartController extends Controller
return response()->json($validation);
// check for not valid company
$validation = Company::validateWithName($companyName);
if(array_key_exists('error', $validation))
return response()->json($validation);
$validator = new CompanyValidationByName($companyName);
if(!$validator->isValid())
return response()->json($validator->getMessageMap());
// Get objects
$user = User::where('matrix_username', $matrixUsername)->first();

@ -7,7 +7,9 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Company extends Model
use App\Models\Validation\ValidationByNameInterface;
class Company extends Model implements ValidationByNameInterface
{
use HasFactory;
@ -28,31 +30,22 @@ class Company extends Model
return $this->hasMany(Category::class);
}
public static function isExist(int $company_id): bool
public static function isExist(int $id): bool
{
$count = Company::where('id', $company_id)->count();
$count = Company::where('id', $id)->count();
return $count != 0;
}
public static function validateWithName(string $name)
public static function isExistByName(string $name): bool
{
$name = $name ?? '';
if($name == '')
return [
'error' => 'The company name is empty, please, write the name!!!'
];
$company = Company::where('name', $name)->first();
$count = Company::where('name', $name)->count();
if(!$company)
return [
'error' => 'A company with the name does not exist!!!'
];
return $count != 0;
}
return [
'ok' => 'A company with the name is valid.'
];
public static function isNameValid(string $name): bool
{
return $name != '';
}
}

@ -0,0 +1,18 @@
<?php
namespace App\Models\Validation;
use App\Models\Validation\ModelValidationByName;
use App\Models\Validation\Messages\CompanyMessagesFactory;
class CompanyValidationByName extends ModelValidationByName
{
public function __construct(string $name)
{
parent::__construct(
$name,
'App\Models\Company',
(new CompanyMessagesFactory())->create(),
);
}
}

@ -0,0 +1,25 @@
<?php
namespace App\Models\Validation\Messages;
use App\Models\Validation\ValidationStatus;
class BaseMessages
{
protected array $messages;
public function __construct($messages)
{
$this->messages = $messages;
}
public function getMessage($status): string
{
return match($status)
{
ValidationStatus::FOUND => $this->messages['found'],
ValidationStatus::NOT_FOUND => $this->messages['not_found'],
ValidationStatus::INVALID_NAME => $this->messages['invalid_name'],
};
}
}

@ -0,0 +1,20 @@
<?php
namespace App\Models\Validation\Messages;
use App\Models\Validation\Messages\BaseMessages;
class CompanyMessagesFactory
{
protected array $messages = [
'found' => 'A company with the name is valid.',
'not_found' => 'A company with the name does not exist!!!',
'invalid_name' => 'The company name is empty, please, write the name!!!',
];
public function create()
{
return new BaseMessages($this->messages);
}
}

@ -0,0 +1,50 @@
<?php
namespace App\Models\Validation;
use App\Models\Validation\ValidationByNameInterface;
use App\Models\Validation\ValidationStatus;
use App\Models\Validation\Messages\BaseMessages;
class ModelValidationByName
{
protected static BaseMessages $messages;
protected static string $className;
protected string $name;
public function __construct(string $name, string $className, BaseMessages $messages)
{
if (!in_array('App\Models\Validation\ValidationByNameInterface', class_implements($className)))
throw new \RuntimeException("$className does not implement the ValidationByNameInterface interface.");
$this->name = $name;
static::$className = $className;
static::$messages = $messages;
}
public function getStatus()
{
if(!call_user_func([static::$className, 'isNameValid'], $this->name))
return ValidationStatus::INVALID_NAME;
if(!call_user_func([static::$className, 'isExistByName'], $this->name))
return ValidationStatus::NOT_FOUND;
return ValidationStatus::FOUND;
}
public function getMessageMap()
{
$status = $this->getStatus();
return [
$status->status() => static::$messages->getMessage($status)
];
}
public function isValid()
{
return $this->getStatus()->isOk();
}
}

@ -0,0 +1,10 @@
<?php
namespace App\Models\Validation;
interface ValidationByNameInterface {
public static function isNameValid(string $name);
public static function isExistByName(string $name);
}

@ -0,0 +1,40 @@
<?php
namespace App\Models\Validation;
enum ValidationStatus
{
case FOUND;
case NOT_FOUND;
case INVALID_NAME;
public function value(): string
{
return match($this)
{
ValidationStatus::FOUND => 'found',
ValidationStatus::NOT_FOUND => 'not_found',
ValidationStatus::INVALID_NAME => 'invalid_name',
};
}
public function status(): string
{
return match($this)
{
ValidationStatus::FOUND => 'ok',
ValidationStatus::NOT_FOUND => 'error',
ValidationStatus::INVALID_NAME => 'error',
};
}
public function isError(): bool
{
return $this->status() == 'error';
}
public function isOk(): bool
{
return $this->status() == 'ok';
}
}

@ -0,0 +1,46 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Validation\CompanyValidationByName;
class CompanyValidationByNameTest extends TestCase
{
public function testCompanyWithEmptyName(): void
{
$name = '';
$validator = new CompanyValidationByName($name);
$json = $validator->getMessageMap();
$isValid = $validator->isValid();
$this->assertEquals($json['error'], 'The company name is empty, please, write the name!!!');
$this->assertFalse($isValid);
}
public function testNotExistingCompanyWithName(): void
{
$name = '404 Company';
$validator = new CompanyValidationByName($name);
$json = $validator->getMessageMap();
$isValid = $validator->isValid();
$this->assertEquals($json['error'], 'A company with the name does not exist!!!');
$this->assertFalse($isValid);
}
public function testValidCompanyWithName(): void
{
$name = 'testCompany';
$validator = new CompanyValidationByName($name);
$json = $validator->getMessageMap();
$isValid = $validator->isValid();
$this->assertEquals($json['ok'], 'A company with the name is valid.');
$this->assertTrue($isValid);
}
}

@ -1,35 +0,0 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Company;
class CompanyValidationTest extends TestCase
{
public function testCompanyWithEmptyName(): void
{
$json = Company::validateWithName('');
$this->assertEquals($json['error'], 'The company name is empty, please, write the name!!!');
}
public function testNotExistingCompanyWithName(): void
{
$name = '404 Company';
$json = Company::validateWithName($name);
$this->assertEquals($json['error'], 'A company with the name does not exist!!!');
}
public function testValidCompanyWithName(): void
{
$name = 'testCompany';
$json = Company::validateWithName($name);
$this->assertEquals($json['ok'], 'A company with the name is valid.');
}
}
Loading…
Cancel
Save