Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SaveUserPreferenceHandler | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Core\Preference\Application\Command; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Preference\Domain\Model\PreferenceScope; |
| 12 | use App\Core\Preference\Domain\Model\UserPreference; |
| 13 | use App\Core\Preference\Domain\Repository\UserPreferenceRepositoryInterface; |
| 14 | use InvalidArgumentException; |
| 15 | |
| 16 | /** |
| 17 | * Command Handler for saving user preferences. |
| 18 | * |
| 19 | * @package App\Core\Preference\Application\Command |
| 20 | */ |
| 21 | final readonly class SaveUserPreferenceHandler |
| 22 | { |
| 23 | /** |
| 24 | * SaveUserPreferenceHandler constructor. |
| 25 | * |
| 26 | * @param UserPreferenceRepositoryInterface $repository Preference repository. |
| 27 | */ |
| 28 | public function __construct( |
| 29 | private UserPreferenceRepositoryInterface $repository, |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Handles SaveUserPreferenceCommand execution. |
| 35 | * |
| 36 | * @param SaveUserPreferenceCommand $command The command containing preference DTO. |
| 37 | * @return void |
| 38 | * @throws InvalidArgumentException If key or user ID is missing. |
| 39 | */ |
| 40 | public function handle(SaveUserPreferenceCommand $command): void |
| 41 | { |
| 42 | $dto = $command->dto; |
| 43 | if ($dto->userId <= 0) { |
| 44 | throw new InvalidArgumentException('User ID must be a positive integer.'); |
| 45 | } |
| 46 | |
| 47 | if (trim($dto->key) === '') { |
| 48 | throw new InvalidArgumentException('Preference key cannot be empty.'); |
| 49 | } |
| 50 | |
| 51 | $scope = new PreferenceScope( |
| 52 | userId: $dto->userId, |
| 53 | deviceFingerprint: $dto->deviceFingerprint, |
| 54 | moduleName: $dto->moduleName, |
| 55 | entityType: $dto->entityType, |
| 56 | entityId: $dto->entityId, |
| 57 | ); |
| 58 | |
| 59 | $preference = UserPreference::create($scope, $dto->key, $dto->value); |
| 60 | $this->repository->save($preference); |
| 61 | } |
| 62 | } |