From f437d777a592d25b89c954bd409348e275f83f72 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Thu, 17 Aug 2023 19:22:10 +0300 Subject: [PATCH] Add ValidationByName and CompanyValidationByName. --- .../Controllers/API/v2/CartController.php | 8 +-- app/Models/Company.php | 31 +++++------- .../Validation/CompanyValidationByName.php | 18 +++++++ .../Validation/Messages/BaseMessages.php | 25 ++++++++++ .../Messages/CompanyMessagesFactory.php | 20 ++++++++ .../Validation/ModelValidationByName.php | 50 +++++++++++++++++++ .../Validation/ValidationByNameInterface.php | 10 ++++ app/Models/Validation/ValidationStatus.php | 40 +++++++++++++++ .../CompanyValidationByNameTest.php | 46 +++++++++++++++++ .../Validation/CompanyValidationTest.php | 35 ------------- 10 files changed, 226 insertions(+), 57 deletions(-) create mode 100644 app/Models/Validation/CompanyValidationByName.php create mode 100644 app/Models/Validation/Messages/BaseMessages.php create mode 100644 app/Models/Validation/Messages/CompanyMessagesFactory.php create mode 100644 app/Models/Validation/ModelValidationByName.php create mode 100644 app/Models/Validation/ValidationByNameInterface.php create mode 100644 app/Models/Validation/ValidationStatus.php create mode 100644 tests/Feature/Validation/CompanyValidationByNameTest.php delete mode 100644 tests/Feature/Validation/CompanyValidationTest.php diff --git a/app/Http/Controllers/API/v2/CartController.php b/app/Http/Controllers/API/v2/CartController.php index 66a75ad..fea8938 100644 --- a/app/Http/Controllers/API/v2/CartController.php +++ b/app/Http/Controllers/API/v2/CartController.php @@ -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(); diff --git a/app/Models/Company.php b/app/Models/Company.php index 7c89086..48cd672 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -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 != ''; } } diff --git a/app/Models/Validation/CompanyValidationByName.php b/app/Models/Validation/CompanyValidationByName.php new file mode 100644 index 0000000..684a706 --- /dev/null +++ b/app/Models/Validation/CompanyValidationByName.php @@ -0,0 +1,18 @@ +create(), + ); + } +} diff --git a/app/Models/Validation/Messages/BaseMessages.php b/app/Models/Validation/Messages/BaseMessages.php new file mode 100644 index 0000000..3b5c6d0 --- /dev/null +++ b/app/Models/Validation/Messages/BaseMessages.php @@ -0,0 +1,25 @@ +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'], + }; + } +} diff --git a/app/Models/Validation/Messages/CompanyMessagesFactory.php b/app/Models/Validation/Messages/CompanyMessagesFactory.php new file mode 100644 index 0000000..bc3d6f6 --- /dev/null +++ b/app/Models/Validation/Messages/CompanyMessagesFactory.php @@ -0,0 +1,20 @@ + '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); + } +} + diff --git a/app/Models/Validation/ModelValidationByName.php b/app/Models/Validation/ModelValidationByName.php new file mode 100644 index 0000000..eb2bd0e --- /dev/null +++ b/app/Models/Validation/ModelValidationByName.php @@ -0,0 +1,50 @@ +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(); + } +} + diff --git a/app/Models/Validation/ValidationByNameInterface.php b/app/Models/Validation/ValidationByNameInterface.php new file mode 100644 index 0000000..bab0575 --- /dev/null +++ b/app/Models/Validation/ValidationByNameInterface.php @@ -0,0 +1,10 @@ + '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'; + } +} diff --git a/tests/Feature/Validation/CompanyValidationByNameTest.php b/tests/Feature/Validation/CompanyValidationByNameTest.php new file mode 100644 index 0000000..87034c8 --- /dev/null +++ b/tests/Feature/Validation/CompanyValidationByNameTest.php @@ -0,0 +1,46 @@ +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); + } +} diff --git a/tests/Feature/Validation/CompanyValidationTest.php b/tests/Feature/Validation/CompanyValidationTest.php deleted file mode 100644 index 00dfd06..0000000 --- a/tests/Feature/Validation/CompanyValidationTest.php +++ /dev/null @@ -1,35 +0,0 @@ -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.'); - } -}