NodeService::viewNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace AppBundle\Service;
3
4
use AppBundle\Entity\Node;
5
use AppBundle\Entity\Repository\NodeRepository;
6
use Doctrine\ORM\EntityManagerInterface;
7
8
class NodeService
9
{
10
    /** @var EntityManagerInterface */
11
    private $entityManager;
12
13
    /** @var NodeRepository */
14
    private $nodeRepository;
15
16
    /** @var PermissionService */
17
    private $permissionService;
18
19
    public function __construct(
20
        EntityManagerInterface $entityManager,
21
        NodeRepository $nodeRepository,
22
        PermissionService $permissionService
23
    ) {
24
        $this->entityManager = $entityManager;
25
        $this->nodeRepository = $nodeRepository;
26
        $this->permissionService = $permissionService;
27
    }
28
29
    /**
30
     * @param int $id
31
     *
32
     * @return Node
33
     *
34
     * @throws \Exception
35
     */
36
    public function getNodeById($id)
37
    {
38
        $node = $this->nodeRepository->findOneBy(['id' => $id]);
39
40
        if ($node === null) {
41
            throw new \Exception(sprintf('Node "%d" does not exists', $id));
42
        }
43
44
        return $node;
45
    }
46
47
    /**
48
     * @param int $id
49
     *
50
     * @return Node
51
     */
52
    public function viewNode($id)
53
    {
54
        $node = $this->getNodeById($id);
55
56
        $node->incrementViews();
57
        $this->entityManager->persist($node);
58
        $this->entityManager->flush();
59
60
        return $node;
61
    }
62
63
    /**
64
     * @param Node $node
65
     *
66
     * @return Node[]
67
     */
68
    public function getChildren(Node $node)
69
    {
70
        // Read permissions required for getting children
71
        $this->permissionService->canViewNode($node);
72
73
        $children = $node->getChildren();
74
75
        return $children;
76
    }
77
78
    /**
79
     * @param int $id
80
     *
81
     * @return Node[]
82
     */
83
    public function getChildrenById($id)
84
    {
85
        $node = $this->getNodeById($id);
86
87
        return $this->getChildren($node);
88
    }
89
}
90