Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

PublishNotification   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 8
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 13 1
A checkNotificationDoesNotExist() 0 10 3
A checkUserExists() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\Notifier\Application\Inbox\Notification;
16
17
use Kreta\Notifier\Domain\Model\Inbox\Notification\Notification;
18
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationAlreadyExists;
19
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationBody;
20
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationDoesNotExist;
21
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationId;
22
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationRepository;
23
use Kreta\Notifier\Domain\Model\Inbox\UserId;
24
use Kreta\Notifier\Domain\Model\Inbox\UserRepository;
25
26
class PublishNotification
27
{
28
    private $repository;
29
    private $userRepository;
30
31
    public function __construct(NotificationRepository $repository, UserRepository $userRepository)
32
    {
33
        $this->repository = $repository;
34
        $this->userRepository = $userRepository;
35
    }
36
37
    public function __invoke(PublishNotificationCommand $command) : void
38
    {
39
        $id = NotificationId::generate($command->notificationId());
40
        $userId = UserId::generate($command->userId());
41
        $body = new NotificationBody($command->body());
42
43
        $this->checkNotificationDoesNotExist($id);
44
        $this->checkUserExists($userId);
45
46
        $notification = Notification::broadcast($id, $userId, $body);
47
48
        $this->repository->save($notification);
49
    }
50
51
    private function checkNotificationDoesNotExist(NotificationId $notificationId) : void
52
    {
53
        try {
54
            $notification = $this->repository->get($notificationId);
55
            if ($notification instanceof Notification) {
56
                throw new NotificationAlreadyExists($notification->id());
57
            }
58
        } catch (NotificationDoesNotExist $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
59
        }
60
    }
61
62
    private function checkUserExists(UserId $userId) : void
63
    {
64
        $this->userRepository->get($userId);
65
    }
66
}
67