-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1471 from bakaphp/sku-validation
[Fix][Improvement] SKU validation
- Loading branch information
Showing
5 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/Domains/Inventory/Variants/Actions/UpdateVariantsAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Inventory\Variants\Actions; | ||
|
||
use Baka\Support\Str; | ||
use Baka\Users\Contracts\UserInterface; | ||
use Illuminate\Support\Facades\Validator; | ||
use Kanvas\Companies\Repositories\CompaniesRepository; | ||
use Kanvas\Exceptions\ValidationException; | ||
use Kanvas\Inventory\Variants\DataTransferObject\Variants as VariantsDto; | ||
use Kanvas\Inventory\Variants\Models\Variants; | ||
use Kanvas\Inventory\Variants\Validations\UniqueSkuRule; | ||
|
||
class UpdateVariantsAction | ||
{ | ||
/** | ||
* __construct. | ||
*/ | ||
public function __construct( | ||
protected Variants $variant, | ||
protected VariantsDto $variantDto, | ||
protected UserInterface $user | ||
) { | ||
} | ||
|
||
/** | ||
* execute. | ||
*/ | ||
public function execute(): Variants | ||
{ | ||
CompaniesRepository::userAssociatedToCompany( | ||
$this->variantDto->product->company()->get()->first(), | ||
$this->user | ||
); | ||
|
||
$validator = Validator::make( | ||
['sku' => $this->variantDto->sku], | ||
['sku' => new UniqueSkuRule($this->variant->app, $this->variant->company, $this->variant)] | ||
); | ||
|
||
if ($validator->fails()) { | ||
throw new ValidationException($validator->messages()->__toString()); | ||
} | ||
|
||
$this->variant->update( | ||
[ | ||
'name' => $this->variantDto->name, | ||
'slug' => $this->variantDto->slug ?? Str::slug($this->variantDto->name), | ||
'sku' => $this->variantDto->sku, | ||
'users_id' => $this->user->getId(), | ||
'description' => $this->variantDto->description, | ||
'short_description' => $this->variantDto->short_description, | ||
'html_description' => $this->variantDto->html_description, | ||
'status_id' => $this->variantDto->status_id, | ||
'ean' => $this->variantDto->ean, | ||
'barcode' => $this->variantDto->barcode, | ||
'serial_number' => $this->variantDto->serial_number, | ||
|
||
] | ||
); | ||
|
||
return $this->variant; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Domains/Inventory/Variants/Validations/UniqueSkuRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kanvas\Inventory\Variants\Validations; | ||
|
||
use Baka\Contracts\AppInterface; | ||
use Baka\Contracts\CompanyInterface; | ||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Kanvas\Inventory\Variants\Models\Variants; | ||
|
||
class UniqueSkuRule implements ValidationRule | ||
{ | ||
public function __construct( | ||
protected AppInterface $app, | ||
protected CompanyInterface $company, | ||
protected ?Variants $variant = null | ||
) { | ||
} | ||
|
||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$query = Variants::where('sku', $value) | ||
->fromCompany($this->company) | ||
->fromApp($this->app); | ||
|
||
if ($this->variant) { | ||
$query->where('id', '!=', $this->variant->getId()); | ||
} | ||
|
||
if ($query->exists()) { | ||
$fail("The $attribute has already been taken."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters