NotificationManager::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace JK\NotificationBundle\Manager;
4
5
use JK\NotificationBundle\Entity\Notification;
6
use JK\NotificationBundle\Factory\NotificationFactoryInterface;
7
use JK\NotificationBundle\Repository\NotificationRepository;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10
class NotificationManager implements NotificationManagerInterface
11
{
12
    /**
13
     * @var NotificationRepository
14
     */
15
    private $repository;
16
17
    /**
18
     * @var NotificationFactoryInterface
19
     */
20
    private $factory;
21
22
    public function __construct(NotificationFactoryInterface $factory, NotificationRepository $repository)
23
    {
24
        $this->repository = $repository;
25
        $this->factory = $factory;
26
    }
27
28
    public function get(int $id): Notification
29
    {
30
        $notification = $this->repository->find($id);
31
32
        if (!$notification instanceof Notification) {
33
            throw new NotFoundHttpException();
34
        }
35
36
        return $notification;
37
    }
38
39
    public function create(string $title, string $content, string $ownerId = null): Notification
40
    {
41
        $notification = $this->factory->create($title, $content, $ownerId);
42
        $this->save($notification);
43
44
        return $notification;
45
    }
46
47
    public function markAsRead(Notification $notification): void
48
    {
49
        $notification->setRead(true);
50
        $this->save($notification);
51
    }
52
53
    public function save(Notification $notification): void
54
    {
55
        $this->repository->save($notification);
56
    }
57
}
58