AbstractEventManagerAwareAssertion   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 8
eloc 31
c 0
b 0
f 0
dl 0
loc 104
ccs 30
cts 31
cp 0.9677
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventManager() 0 7 2
A preAssert() 0 7 1
A setEventManager() 0 12 1
A getEventManagerIdentifiers() 0 3 1
A assert() 0 31 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Acl\Assertion;
12
13
use Laminas\EventManager\EventManager;
14
use Laminas\EventManager\EventManagerAwareInterface;
15
use Laminas\EventManager\EventManagerInterface;
16
use Laminas\Permissions\Acl\Acl;
17
use Laminas\Permissions\Acl\Assertion\AssertionInterface;
18
use Laminas\Permissions\Acl\Resource\ResourceInterface;
19
use Laminas\Permissions\Acl\Role\RoleInterface;
20
21
/**
22
 * Skeleton for an EventManagerAware Assertion.
23
 *
24
 * Handles the management of the EventManager, the creation of the AssertionEvent, triggers the event and
25
 * introspecting the result set.
26
 *
27
 * Listeners to that event should return a simple boolean value. Other return types are treated as they were the
28
 * boolean TRUE. If a listener returns FALSE the propagation is stopped immediatly and no further listeners are invoked.
29
 * The assertion returns FALSE in this case.
30
 *
31
 * @author Mathias Gelhausen <[email protected]>
32
 * @since 0.18
33
 */
34
abstract class AbstractEventManagerAwareAssertion implements EventManagerAwareInterface, AssertionInterface
35
{
36
    /**
37
     * The Event manager.
38
     *
39
     * @var EventManagerInterface
40
     */
41
    protected $events;
42
43
    /**
44
     * Identifiers for the SharedEventManager
45
     *
46
     * @var string[]
47
     */
48
    protected $identifiers = array();
49
50 6
    public function setEventManager(EventManagerInterface $eventManager)
51
    {
52 6
        $identifiers = $this->getEventManagerIdentifiers() + array(
53 6
            __NAMESPACE__,
54
            __CLASS__,
55 6
            get_class($this),
56 6
            'Acl/Assertion',
57
        );
58
59 6
        $eventManager->setIdentifiers($identifiers);
60
61 6
        $this->events = $eventManager;
62
    }
63
64 4
    public function getEventManager()
65
    {
66 4
        if (!$this->events) {
67 1
            $this->setEventManager(new EventManager());
68
        }
69
70 4
        return $this->events;
71
    }
72
73
    /**
74
     * Gets the identifiers to be used by the SharedEventManager.
75
     *
76
     * Overwrite this to manipulate identifiers.
77
     *
78
     * @return string[]
79
     */
80 6
    protected function getEventManagerIdentifiers()
81
    {
82 6
        return $this->identifiers;
83
    }
84
85 3
    public function assert(
86
        Acl $acl,
87
        RoleInterface $role = null,
88
        ResourceInterface $resource = null,
89
        $privilege = null
90
    ) {
91 3
        $preCheck = $this->preAssert($acl, $role, $resource, $privilege);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $preCheck is correct as $this->preAssert($acl, $... $resource, $privilege) targeting Acl\Assertion\AbstractEv...eAssertion::preAssert() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
92
93 3
        if (is_bool($preCheck)) {
0 ignored issues
show
introduced by
The condition is_bool($preCheck) is always false.
Loading history...
94 1
            return $preCheck;
95
        }
96
97 3
        $event = new AssertionEvent(null, $this);
98 3
        $event->setAcl($acl)
99 3
              ->setRole($role)
100 3
              ->setResource($resource)
101 3
              ->setPrivilege($privilege);
102
        
103 3
        $events = $this->getEventManager();
104
105
        $callback = function ($r) {
106
	        return false === $r;
107 3
        };
108
        
109 3
        $results = $events->triggerUntil(
110 3
        	$callback,
111 3
            $event->getName(),
112
            $event
113
        );
114
115 3
        return false === $results->last() ? false : true; // result must be BOOLEAN false (not "", null or 0 or any other value evaluated to FALSE)
116
    }
117
118
    /**
119
     * Overwrite this to check some conditions before the event is triggered.
120
     *
121
     * If this method returns a boolean value, this value will be returned as the assertions' result and
122
     * no event will be triggered.
123
     *
124
     * @param Acl               $acl
125
     * @param RoleInterface     $role
126
     * @param ResourceInterface $resource
127
     * @param null|string       $privilege
128
     *
129
     * @return null|bool
130
     */
131 3
    protected function preAssert(
132
        Acl $acl,
133
        RoleInterface $role = null,
134
        ResourceInterface $resource = null,
135
        $privilege = null
136
    ) {
137 3
        return null;
138
    }
139
}
140