Passed
Push — master ( 746421...da8e79 )
by Gabriel
07:08
created

DiscoverProvider::detectDiscoveryFromConfig()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 10
c 2
b 0
f 1
nc 5
nop 0
dl 0
loc 17
ccs 0
cts 1
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace ByTIC\EventDispatcher\ListenerProviders\Discover;
4
5
use ByTIC\EventDispatcher\ListenerProviders\DefaultProvider;
6
7
/**
8
 * Class DiscoverProvider
9
 * @package ByTIC\EventDispatcher\ListenerProviders\Discover
10
 */
11
class DiscoverProvider extends DefaultProvider
12
{
13
    protected $discovered = null;
14
15
    protected $discoveryPath = null;
16
17
    /**
18
     * @inheritDoc
19
     */
20 1
    public function getListenersForEvent(object $event): iterable
21
    {
22 1
        $this->discover();
23 1
        return parent::getListenersForEvent($event);
24
    }
25
26 1
    protected function discover()
27
    {
28 1
        if ($this->discovered !== null) {
29
            return;
30
        }
31 1
        $eventsArray = DiscoverEvents::within($this->discoverEventsWithin());
32 1
        foreach ($eventsArray as $event => $listeners) {
33 1
            foreach ($listeners as $listener) {
34 1
                $this->listen($event, $listener);
35
            }
36
        }
37 1
    }
38
39
    /**
40
     * Get the listener directories that should be used to discover events.
41
     *
42
     * @return array
43
     */
44 1
    protected function discoverEventsWithin(): array
45
    {
46 1
        if ($this->discoveryPath === null) {
47
            $this->discoveryPath = $this->detectDiscoveryPath();
48
        }
49
50 1
        return $this->discoveryPath;
51
    }
52
53
    /**
54
     * @param string $path
55
     */
56 1
    public function addDiscoveryPath(string $path)
57
    {
58 1
        $this->discoveryPath[] = $path;
59 1
    }
60
61
    /**
62
     * @return array
63
     */
64
    protected function detectDiscoveryPath(): array
65
    {
66
        $paths = $this->detectDiscoveryFromConfig();
67
        if (count($paths)) {
68
            return $paths;
69
        }
70
        return $this->detectDiscoveryFromApplication();
71
    }
72
73
    protected function detectDiscoveryFromConfig(): array
74
    {
75
        if (!function_exists('config')) {
76
            return [];
77
        }
78
79
        $config = config('event-dispatcher.listener_paths');
80
        if (!$config) {
81
            return [];
82
        }
83
        $paths = $config->toArray();
84
        foreach ($paths as $key => $path) {
85
            if (!is_dir($path)) {
86
                unset($paths[$key]);
87
            }
88
        }
89
        return $paths;
90
    }
91
92
    protected function detectDiscoveryFromApplication(): array
93
    {
94
        $basePath = $this->detectApplicationPath();
95
        if (!is_dir($basePath)) {
0 ignored issues
show
Bug introduced by
It seems like $basePath can also be of type false; however, parameter $filename of is_dir() 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

95
        if (!is_dir(/** @scrutinizer ignore-type */ $basePath)) {
Loading history...
96
            return [];
97
        }
98
99
        $folder = $basePath . '/Listeners';
0 ignored issues
show
Bug introduced by
Are you sure $basePath of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

99
        $folder = /** @scrutinizer ignore-type */ $basePath . '/Listeners';
Loading history...
100
        if (!is_dir($folder)) {
101
            return [];
102
        }
103
104
        return [
105
            $folder,
106
        ];
107
    }
108
109
    /**
110
     * @return false|string
111
     */
112
    protected function detectApplicationPath()
113
    {
114
        if (defined('APPLICATION_PATH')) {
115
            return APPLICATION_PATH;
0 ignored issues
show
Bug introduced by
The constant ByTIC\EventDispatcher\Li...scover\APPLICATION_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
116
        }
117
        if (!function_exists('app')) {
118
            return false;
119
        }
120
        $app = app();
121
        if ($app->has('path')) {
122
            return $app->get('path');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $app->get('path') also could return the type object which is incompatible with the documented return type false|string.
Loading history...
123
        }
124
       return false;
125
    }
126
}
127