-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAddAllowedIpCommandHandler.php
More file actions
42 lines (33 loc) · 1.29 KB
/
Copy pathAddAllowedIpCommandHandler.php
File metadata and controls
42 lines (33 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace Squidmin\Application\Command\AllowedIp;
use Squidmin\Application\Command\CommandHandlerInterface;
use Squidmin\Application\Command\CommandInterface;
use Squidmin\Domain\AllowedIp\AllowedIp;
use Squidmin\Domain\AllowedIp\AllowedIpRepositoryInterface;
use Squidmin\Domain\AllowedIp\ValueObject\IpAddress;
use Squidmin\Domain\User\ValueObject\UserId;
final class AddAllowedIpCommandHandler implements CommandHandlerInterface
{
public function __construct(
private readonly AllowedIpRepositoryInterface $allowedIpRepository
) {
}
public function handle(CommandInterface $command): void
{
if (!$command instanceof AddAllowedIpCommand) {
throw new \InvalidArgumentException('Invalid command type');
}
$ipAddress = new IpAddress($command->getIpAddress());
$ownerId = new UserId($command->getOwnerId());
if ($this->allowedIpRepository->existsByIpAndOwnerId($ipAddress, $ownerId)) {
throw new \DomainException('This IP address is already allowed for this user');
}
$allowedIp = AllowedIp::add(
$this->allowedIpRepository->nextIdentity(),
$ipAddress,
$ownerId
);
$this->allowedIpRepository->save($allowedIp);
}
}