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

DraftPageChecker   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleAllowed() 0 21 1
A handle() 0 16 4
A handleNotAllowed() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Events\Listeners;
6
7
use AbterPhp\Framework\Authorization\Constant\Role;
8
use AbterPhp\Framework\Constant\Session;
9
use AbterPhp\Framework\I18n\ITranslator;
10
use AbterPhp\Website\Events\PageViewed;
11
use Casbin\Enforcer;
12
13
class DraftPageChecker
14
{
15
    const RESOURCE_IDENTIFIER = 'admin_resource_pages';
16
17
    /** @var Enforcer */
18
    protected $enforcer;
19
20
    /** @var ITranslator */
21
    protected $translator;
22
23
    /**
24
     * NavigationBuilder constructor.
25
     *
26
     * @param Enforcer    $enforcer
27
     * @param ITranslator $translator
28
     */
29
    public function __construct(Enforcer $enforcer, ITranslator $translator)
30
    {
31
        $this->enforcer   = $enforcer;
32
        $this->translator = $translator;
33
    }
34
35
    /**
36
     * @param PageViewed $event
37
     *
38
     * @throws \Casbin\Exceptions\CasbinException
39
     */
40
    public function handle(PageViewed $event)
41
    {
42
        $page = $event->getPage();
43
44
        // Page is not a draft
45
        if (!$page->isDraft()) {
46
            return;
47
        }
48
49
        foreach ($event->getUserGroupIdentifiers() as $userGroupIdentifier) {
50
            if ($this->enforcer->enforce($userGroupIdentifier, static::RESOURCE_IDENTIFIER, Role::READ)) {
51
                return $this->handleAllowed($event);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->handleAllowed($event) targeting AbterPhp\Website\Events\...hecker::handleAllowed() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
            }
53
        }
54
55
        return $this->handleNotAllowed($event);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->handleNotAllowed($event) targeting AbterPhp\Website\Events\...ker::handleNotAllowed() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
56
    }
57
58
    /**
59
     * @param PageViewed $event
60
     */
61
    protected function handleAllowed(PageViewed $event)
62
    {
63
        $page = clone $event->getPage();
64
65
        $page->setTitle(
66
            sprintf(
67
                '%s - %s',
68
                $this->translator->translate('website:pageDraftAllowedTitle'),
69
                $page->getTitle()
70
            )
71
        );
72
        $page->setBody(
73
            sprintf(
74
                "%s\n%s",
75
                $this->translator->translate('website:pageDraftAllowedBody'),
76
                $page->getBody()
77
            )
78
        );
79
80
        $event->setPage($page);
81
        $event->setIsAllowed(true);
82
    }
83
84
    /**
85
     * @param PageViewed $event
86
     */
87
    protected function handleNotAllowed(PageViewed $event)
88
    {
89
        $event->setIsAllowed(false);
90
    }
91
}
92