Passed
Push — master ( 5b5c00...4c0c12 )
by Julien
04:52
created

Behavior::beforeExecuteRoute()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 33
ccs 0
cts 17
cp 0
rs 8.0555
cc 9
nc 12
nop 1
crap 90
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Controller;
13
14
use Phalcon\Events\Manager;
15
use Phalcon\Mvc\Dispatcher;
16
use Zemit\Di\Injectable;
17
use Zemit\Mvc\Controller\AbstractTrait\AbstractInjectable;
18
19
trait Behavior
20
{
21
    use AbstractInjectable;
22
    
23
    public function beforeExecuteRoute(Dispatcher $dispatcher): void
0 ignored issues
show
Unused Code introduced by
The parameter $dispatcher is not used and could be removed. ( Ignorable by Annotation )

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

23
    public function beforeExecuteRoute(/** @scrutinizer ignore-unused */ Dispatcher $dispatcher): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        // @todo use eventsManager from service provider instead
26
        $this->eventsManager->enablePriorities(true);
0 ignored issues
show
Bug introduced by
The property eventsManager does not exist on Zemit\Mvc\Controller\Behavior. Did you mean eventsManager;?
Loading history...
27
        
28
        // @todo see if we can implement receiving an array of responses globally: V2
29
        // $this->eventsManager->collectResponses(true);
30
        
31
        // retrieve events based on the config roles and features
32
        $permissions = $this->config->get('permissions')->toArray() ?? [];
33
        $featureList = $permissions['features'] ?? [];
34
        $roleList = $permissions['roles'] ?? [];
35
        
36
        foreach ($roleList as $role => $rolePermission) {
37
            // do not attach other roles behaviors
38
            if (!$this->identity->hasRole([$role])) {
0 ignored issues
show
Bug introduced by
The property identity does not exist on Zemit\Mvc\Controller\Behavior. Did you mean identity;?
Loading history...
39
                continue;
40
            }
41
            
42
            if (isset($rolePermission['features'])) {
43
                foreach ($rolePermission['features'] as $feature) {
44
                    $rolePermission = array_merge_recursive($rolePermission, $featureList[$feature] ?? []);
45
                    // @todo remove duplicates
46
                }
47
            }
48
            
49
            $behaviorsContext = $rolePermission['behaviors'] ?? [];
50
            foreach ($behaviorsContext as $className => $behaviors) {
51
                if (is_int($className) || get_class($this) === $className) {
52
                    $this->attachBehaviors($behaviors, 'rest');
53
                }
54
                if ($this->getModelClassName() === $className) {
0 ignored issues
show
Bug introduced by
It seems like getModelClassName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

54
                if ($this->/** @scrutinizer ignore-call */ getModelClassName() === $className) {
Loading history...
55
                    $this->attachBehaviors($behaviors, 'model');
56
                }
57
            }
58
        }
59
    }
60
    
61
    /**
62
     * Attach a behavior to the events' manager.
63
     *
64
     * @param string $behavior The name of the behavior to attach.
65
     * @param string $eventType Optional. The event type to attach the behavior to. Default is 'rest'.
66
     *
67
     * @return void
68
     */
69
    public function attachBehavior(string $behavior, string $eventType = 'rest'): void
70
    {
71
        $event = new $behavior();
72
        
73
        // inject DI
74
        if ($event instanceof Injectable || method_exists($event, 'setDI')) {
75
            $event->setDI($this->getDI());
76
        }
77
        
78
        // attach behavior
79
        $this->eventsManager->attach($event->eventType ?? $eventType, $event, $event->priority ?? Manager::DEFAULT_PRIORITY);
0 ignored issues
show
Bug introduced by
The property eventsManager does not exist on Zemit\Mvc\Controller\Behavior. Did you mean eventsManager;?
Loading history...
Bug Best Practice introduced by
The property eventType does not exist on Zemit\Di\Injectable. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property priority does not exist on Zemit\Di\Injectable. Since you implemented __get, consider adding a @property annotation.
Loading history...
80
    }
81
    
82
    /**
83
     * Attach behaviors to the current object.
84
     *
85
     * @param array $behaviors The array of behaviors to attach.
86
     * @param string $eventType The event type to attach the behaviors to. Default is 'rest'.
87
     *
88
     * @return void
89
     */
90
    public function attachBehaviors(array $behaviors = [], string $eventType = 'rest'): void
91
    {
92
        foreach ($behaviors as $behavior) {
93
            $this->attachBehavior($behavior, $eventType);
94
        }
95
    }
96
    
97
}
98