EventsManagerProvider::attachEvents()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 17
ccs 0
cts 13
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Canvas\Providers;
4
5
use Phalcon\Di\ServiceProviderInterface;
0 ignored issues
show
Bug introduced by
The type Phalcon\Di\ServiceProviderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Phalcon\DiInterface;
0 ignored issues
show
Bug introduced by
The type Phalcon\DiInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Canvas\Listener\Subscription;
8
use Canvas\EventsManager;
9
use Canvas\Listener\User;
10
use Canvas\Listener\Notification;
11
use Canvas\Listener\Company;
12
13
class EventsManagerProvider implements ServiceProviderInterface
14
{
15
    /**
16
     * List of the listeners use by the app.
17
     *
18
     * [
19
     *  'eventName' => 'className'
20
     * ];
21
     *
22
     * @var array
23
     */
24
    protected $listeners = [
25
    ];
26
27
    protected $canvasListeners = [
28
        'subscription' => Subscription::class,
29
        'user' => User::class,
30
        'company' => Company::class,
31
        'notification' => Notification::class,
32
    ];
33
34
    /**
35
     * @param DiInterface $container
36
     */
37
    public function register(DiInterface $container)
38
    {
39
        $container->setShared(
40
            'events',
41
            function () {
42
                $eventsManager = new EventsManager();
43
44
                return $eventsManager;
45
            }
46
        );
47
48
        $this->attachEvents($container, $this->listeners);
49
        $this->attachEvents($container, $this->canvasListeners);
50
    }
51
52
    /**
53
     * given the DI attach the list of containers.
54
     *
55
     * @param DiInterface $container
56
     * @param array $listeners
57
     * @return void
58
     */
59
    protected function attachEvents(DiInterface $container, array $listeners): bool
60
    {
61
        if (empty($listeners)) {
62
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type void.
Loading history...
63
        }
64
        $eventsManager = $container->get('eventsManager');
65
66
        //navigate the list of listener and create the events
67
        foreach ($listeners as $key => $listen) {
68
            //create the events given the key
69
            $eventsManager->attach(
70
                $key,
71
                new $listen()
72
            );
73
        }
74
75
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type void.
Loading history...
76
    }
77
}
78