Passed
Push — master ( df08cc...276b2d )
by Peter
02:35
created

Index::getRenderedPage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 28
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Service\Website;
6
7
use AbterPhp\Admin\Orm\UserRepo;
8
use AbterPhp\Framework\Template\Engine;
9
use AbterPhp\Website\Constant\Event;
10
use AbterPhp\Website\Domain\Entities\Page as Entity;
11
use AbterPhp\Website\Events\PageViewed;
12
use AbterPhp\Website\Orm\PageRepo;
13
use Opulence\Events\Dispatchers\IEventDispatcher;
14
use Opulence\Orm\OrmException;
15
16
class Index
17
{
18
    /** @var Engine */
19
    protected $templateEngine;
20
21
    /** @var PageRepo */
22
    protected $pageRepo;
23
24
    /** @var IEventDispatcher */
25
    protected $eventDispatcher;
26
27
    /**
28
     * Index constructor.
29
     *
30
     * @param Engine           $templateEngine
31
     * @param PageRepo         $pageRepo
32
     * @param UserRepo         $userRepo
33
     * @param IEventDispatcher $eventDispatcher
34
     */
35
    public function __construct(
36
        Engine $templateEngine,
37
        PageRepo $pageRepo,
38
        UserRepo $userRepo,
39
        IEventDispatcher $eventDispatcher
40
    ) {
41
        $this->templateEngine  = $templateEngine;
42
        $this->pageRepo        = $pageRepo;
43
        $this->userRepo        = $userRepo;
0 ignored issues
show
Bug Best Practice introduced by
The property userRepo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
        $this->eventDispatcher = $eventDispatcher;
45
    }
46
47
    /**
48
     * @param string   $identifier
49
     * @param string[] $userGroupIdentifiers
50
     *
51
     * @return Entity|null
52
     */
53
    public function getRenderedPage(string $identifier, array $userGroupIdentifiers): ?Entity
54
    {
55
        try {
56
            $page = $this->pageRepo->getWithLayout($identifier);
57
        } catch (OrmException $exc) {
58
            return null;
59
        }
60
61
        $pageEvent = new PageViewed($page, $userGroupIdentifiers);
0 ignored issues
show
Bug introduced by
It seems like $page can also be of type null; however, parameter $page of AbterPhp\Website\Events\PageViewed::__construct() does only seem to accept AbterPhp\Website\Domain\Entities\Page, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        $pageEvent = new PageViewed(/** @scrutinizer ignore-type */ $page, $userGroupIdentifiers);
Loading history...
62
63
        $this->eventDispatcher->dispatch(Event::PAGE_VIEWED, $pageEvent);
64
        if (!$pageEvent->isAllowed()) {
65
            return null;
66
        }
67
68
        $page = $pageEvent->getPage();
69
70
        $vars      = ['title' => $page->getTitle()];
71
        $templates = [
72
            'body'   => $page->getBody(),
73
            'layout' => $page->getLayout(),
74
        ];
75
76
        $renderedBody = $this->templateEngine->run('page', $page->getIdentifier(), $templates, $vars);
77
78
        $page->setRenderedBody($renderedBody);
79
80
        return $page;
81
    }
82
83
    /**
84
     * @param string      $identifier
85
     * @param string|null $visitorUsername
86
     *
87
     * @return string[]
88
     */
89
    public function getUserGroupIdentifiers(?string $visitorUsername): array
90
    {
91
        try {
92
            $user = $this->userRepo->getByUsername($visitorUsername);
0 ignored issues
show
Bug introduced by
It seems like $visitorUsername can also be of type null; however, parameter $username of AbterPhp\Admin\Orm\UserRepo::getByUsername() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            $user = $this->userRepo->getByUsername(/** @scrutinizer ignore-type */ $visitorUsername);
Loading history...
93
        } catch (OrmException $exc) {
94
            return [];
95
        }
96
97
        $userGroupIdentifiers = [];
98
        foreach ($user->getUserGroups() as $userGroup) {
99
            $userGroupIdentifiers[] = $userGroup->getIdentifier();
100
        }
101
102
103
        return $userGroupIdentifiers;
104
    }
105
}
106