Issues (106)

src/Service/Website/Index.php (1 issue)

Labels
Severity
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\Constant\Html5;
9
use AbterPhp\Framework\Html\Helper\StringHelper;
10
use AbterPhp\Framework\Template\Engine;
11
use AbterPhp\Website\Constant\Event;
12
use AbterPhp\Website\Domain\Entities\Page as Entity;
13
use AbterPhp\Website\Events\PageViewed;
14
use AbterPhp\Website\Orm\PageRepo;
15
use Casbin\Exceptions\CasbinException;
16
use Opulence\Events\Dispatchers\IEventDispatcher;
17
use Opulence\Orm\OrmException;
18
19
class Index
20
{
21
    /** @var Engine */
22
    protected $engine;
23
24
    /** @var PageRepo */
25
    protected $pageRepo;
26
27
    /** @var UserRepo */
28
    protected $userRepo;
29
30
    /** @var IEventDispatcher */
31
    protected $eventDispatcher;
32
33
    /**
34
     * Index constructor.
35
     *
36
     * @param Engine           $engine
37
     * @param PageRepo         $pageRepo
38
     * @param UserRepo         $userRepo
39
     * @param IEventDispatcher $eventDispatcher
40
     */
41
    public function __construct(
42
        Engine $engine,
43
        PageRepo $pageRepo,
44
        UserRepo $userRepo,
45
        IEventDispatcher $eventDispatcher
46
    ) {
47
        $this->engine          = $engine;
48
        $this->pageRepo        = $pageRepo;
49
        $this->userRepo        = $userRepo;
50
        $this->eventDispatcher = $eventDispatcher;
51
    }
52
53
    /**
54
     * @param string   $identifier
55
     * @param string[] $userGroupIdentifiers
56
     *
57
     * @return Entity
58
     * @throws OrmException
59
     * @throws CasbinException
60
     */
61
    public function getRenderedPage(string $identifier, array $userGroupIdentifiers): Entity
62
    {
63
        $page = $this->pageRepo->getWithLayout($identifier);
64
65
        if ($page === null) {
66
            throw new OrmException('page not found: ' . $identifier);
67
        }
68
69
        assert($page instanceof Entity);
70
71
        $pageEvent = new PageViewed($page, $userGroupIdentifiers);
72
73
        $this->eventDispatcher->dispatch(Event::PAGE_VIEWED, $pageEvent);
74
        if (!$pageEvent->isAllowed()) {
75
            throw new CasbinException(sprintf('viewing page not allowed" %s', $pageEvent->getPage()->getId()));
76
        }
77
78
        $page      = $pageEvent->getPage();
79
        $ledeLines = StringHelper::wrapByLines($page->getLede(), Html5::TAG_P);
80
        $lede      = StringHelper::wrapInTag($ledeLines, Html5::TAG_DIV, [Html5::ATTR_CLASS => 'lede']);
81
82
        $vars      = [
83
            'title' => $page->getTitle(),
84
            'lede'  => $lede,
85
        ];
86
        $templates = [
87
            'body'   => $page->getBody(),
88
            'layout' => $page->getLayout(),
89
        ];
90
91
        $renderedBody = $this->engine->run('page', $page->getIdentifier(), $templates, $vars);
92
93
        $page->setRenderedBody($renderedBody);
94
95
        return $page;
96
    }
97
98
    /**
99
     * @param string|null $visitorUsername
100
     *
101
     * @return string[]
102
     */
103
    public function getUserGroupIdentifiers(?string $visitorUsername): array
104
    {
105
        try {
106
            $user = $this->userRepo->getByUsername($visitorUsername);
0 ignored issues
show
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

106
            $user = $this->userRepo->getByUsername(/** @scrutinizer ignore-type */ $visitorUsername);
Loading history...
107
        } catch (OrmException $exc) {
108
            return [];
109
        }
110
111
        $userGroupIdentifiers = [];
112
        foreach ($user->getUserGroups() as $userGroup) {
113
            $userGroupIdentifiers[] = $userGroup->getIdentifier();
114
        }
115
116
        return $userGroupIdentifiers;
117
    }
118
}
119