Extension::getManifest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the EasyWeChatComposer.
7
 *
8
 * (c) 张铭阳 <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace EasyWeChatComposer;
15
16
use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
0 ignored issues
show
Bug introduced by
The type EasyWeChat\Kernel\Contracts\EventHandlerInterface 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...
17
use Pimple\Container;
0 ignored issues
show
Bug introduced by
The type Pimple\Container 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...
18
use ReflectionClass;
19
20
class Extension
21
{
22
    /**
23
     * @var \Pimple\Container
24
     */
25
    protected $app;
26
27
    /**
28
     * @var string
29
     */
30
    protected $manifestPath;
31
32
    /**
33
     * @var array|null
34
     */
35
    protected $manifest;
36
37
    /**
38
     * @param \Pimple\Container $app
39
     */
40
    public function __construct(Container $app)
41
    {
42
        $this->app = $app;
43
        $this->manifestPath = __DIR__.'/../extensions.php';
44
    }
45
46
    /**
47
     * Get observers.
48
     *
49
     * @return array
50
     */
51
    public function observers(): array
52
    {
53
        if ($this->shouldIgnore()) {
54
            return [];
55
        }
56
57
        $observers = [];
58
59
        foreach ($this->getManifest() as $name => $extra) {
60
            $observers = array_merge($observers, $extra['observers'] ?? []);
61
        }
62
63
        return array_map([$this, 'listObserver'], array_filter($observers, [$this, 'validateObserver']));
64
    }
65
66
    /**
67
     * @param mixed $observer
68
     *
69
     * @return bool
70
     */
71
    protected function isDisable($observer): bool
72
    {
73
        return in_array($observer, $this->app->config->get('disable_observers', []));
74
    }
75
76
    /**
77
     * Get the observers should be ignore.
78
     *
79
     * @return bool
80
     */
81
    protected function shouldIgnore(): bool
82
    {
83
        return !file_exists($this->manifestPath) || $this->isDisable('*');
84
    }
85
86
    /**
87
     * Validate the given observer.
88
     *
89
     * @param mixed $observer
90
     *
91
     * @return bool
92
     *
93
     * @throws \ReflectionException
94
     */
95
    protected function validateObserver($observer): bool
96
    {
97
        return !$this->isDisable($observer)
98
            && (new ReflectionClass($observer))->implementsInterface(EventHandlerInterface::class)
99
            && $this->accessible($observer);
100
    }
101
102
    /**
103
     * Determine whether the given observer is accessible.
104
     *
105
     * @param string $observer
106
     *
107
     * @return bool
108
     */
109
    protected function accessible($observer): bool
110
    {
111
        if (!method_exists($observer, 'getAccessor')) {
112
            return true;
113
        }
114
115
        return in_array(get_class($this->app), (array) $observer::getAccessor());
116
    }
117
118
    /**
119
     * @param mixed $observer
120
     *
121
     * @return array
122
     */
123
    protected function listObserver($observer): array
124
    {
125
        $condition = method_exists($observer, 'onCondition') ? $observer::onCondition() : '*';
126
127
        return [$observer, $condition];
128
    }
129
130
    /**
131
     * Get the easywechat manifest.
132
     *
133
     * @return array
134
     */
135
    protected function getManifest(): array
136
    {
137
        if (!is_null($this->manifest)) {
138
            return $this->manifest;
139
        }
140
141
        return $this->manifest = file_exists($this->manifestPath) ? require $this->manifestPath : [];
142
    }
143
}
144