Passed
Push — 2.x ( 44da33...f0bfd9 )
by Terry
01:53
created

ComponentTrait::initComponents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Kernel;
14
15
use Shieldon\Firewall\Component\ComponentInterface;
16
use Shieldon\Firewall\Component\ComponentProvider;
17
18
/*
19
 * @since 1.0.0
20
 */
21
trait ComponentTrait
22
{
23
    /**
24
     * Container for Shieldon components.
25
     *
26
     * @var array
27
     */
28
    public $component = [];
29
30
    /**
31
     * Set a commponent.
32
     *
33
     * @param ComponentProvider $instance
34
     *
35
     * @return void
36
     */
37
    public function setComponent(ComponentProvider $instance): void
38
    {
39
        $class = $this->getClassName($instance);
0 ignored issues
show
Bug introduced by
It seems like getClassName() 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

39
        /** @scrutinizer ignore-call */ 
40
        $class = $this->getClassName($instance);
Loading history...
40
        $this->component[$class] = $instance;
41
    }
42
43
    /**
44
     * Get a component instance from component's container.
45
     *
46
     * @param string $name The component's class name.
47
     *
48
     * @return ComponentInterface|null
49
     */
50
    public function getComponent(string $name)
51
    {
52
        if (!isset($this->component[$name])) {
53
            return null;
54
        }
55
56
        return $this->component[$name];
57
    }
58
59
    /*
60
    |--------------------------------------------------------------------------
61
    | Stage in Kernel
62
    |--------------------------------------------------------------------------
63
    | The below methods are used in "process" method in Kernel.
64
    */
65
66
    /**
67
     * Initialize components.
68
     *
69
     * @return void
70
     */
71
    protected function initComponents()
72
    {
73
        foreach (array_keys($this->component) as $name) {
74
            $this->component[$name]->setIp($this->ip);
75
            $this->component[$name]->setRdns($this->rdns);
76
77
            // Apply global strict mode to all components by `strictMode()` if nesscessary.
78
            if (isset($this->strictMode)) {
79
                $this->component[$name]->setStrict($this->strictMode);
80
            }
81
        }
82
    }
83
84
    /**
85
     * Check if current IP is trusted or not.
86
     *
87
     * @return bool
88
     */
89
    protected function isTrustedBot()
90
    {
91
        if ($this->getComponent('TrustedBot')) {
92
93
            // We want to put all the allowed robot into the rule list, so that the checking of IP's resolved hostname 
94
            // is no more needed for that IP.
95
            if ($this->getComponent('TrustedBot')->isAllowed()) {
0 ignored issues
show
Bug introduced by
The method isAllowed() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\Ip or Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

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

95
            if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isAllowed()) {
Loading history...
96
97
                if ($this->getComponent('TrustedBot')->isGoogle()) {
0 ignored issues
show
Bug introduced by
The method isGoogle() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

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

97
                if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isGoogle()) {
Loading history...
98
                    // Add current IP into allowed list, because it is from real Google domain.
99
                    $this->action(
0 ignored issues
show
Bug introduced by
It seems like action() 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

99
                    $this->/** @scrutinizer ignore-call */ 
100
                           action(
Loading history...
100
                        self::ACTION_ALLOW,
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...nentTrait::ACTION_ALLOW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
101
                        self::REASON_IS_GOOGLE
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...Trait::REASON_IS_GOOGLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
102
                    );
103
104
                } elseif ($this->getComponent('TrustedBot')->isBing()) {
0 ignored issues
show
Bug introduced by
The method isBing() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

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

104
                } elseif ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isBing()) {
Loading history...
105
                    // Add current IP into allowed list, because it is from real Bing domain.
106
                    $this->action(
107
                        self::ACTION_ALLOW,
108
                        self::REASON_IS_BING
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...ntTrait::REASON_IS_BING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
109
                    );
110
111
                } elseif ($this->getComponent('TrustedBot')->isYahoo()) {
0 ignored issues
show
Bug introduced by
The method isYahoo() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

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

111
                } elseif ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isYahoo()) {
Loading history...
112
                    // Add current IP into allowed list, because it is from real Yahoo domain.
113
                    $this->action(
114
                        self::ACTION_ALLOW,
115
                        self::REASON_IS_YAHOO
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...tTrait::REASON_IS_YAHOO was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
116
                    );
117
118
                } else {
119
                    // Add current IP into allowed list, because you trust it.
120
                    // You have already defined it in the settings.
121
                    $this->action(
122
                        self::ACTION_ALLOW,
123
                        self::REASON_IS_SEARCH_ENGINE
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...REASON_IS_SEARCH_ENGINE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
124
                    );
125
                }
126
                // Allowed robots not join to our traffic handler.
127
                $this->result = self::RESPONSE_ALLOW;
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The constant Shieldon\Firewall\Kernel...ntTrait::RESPONSE_ALLOW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
                return true;
129
            }
130
        }
131
        return false;
132
    }
133
134
    /**
135
     * Check whether the IP is fake search engine or not.
136
     * The method "isTrustedBot()" must be executed before this method.
137
     *
138
     * @return bool
139
     */
140
    protected function isFakeRobot(): bool
141
    {
142
        if ($this->getComponent('TrustedBot')) {
143
            if ($this->getComponent('TrustedBot')->isFakeRobot()) {
0 ignored issues
show
Bug introduced by
The method isFakeRobot() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\TrustedBot. ( Ignorable by Annotation )

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

143
            if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isFakeRobot()) {
Loading history...
144
                $this->action(
145
                    self::ACTION_DENY,
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...onentTrait::ACTION_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
146
                    self::REASON_COMPONENT_TRUSTED_ROBOT
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...COMPONENT_TRUSTED_ROBOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
147
                );
148
                $this->result = self::RESPONSE_DENY;
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...entTrait::RESPONSE_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
149
                return true;
150
            }
151
        }
152
        return false;
153
    }
154
155
    /**
156
     * Check whether the IP is handled by IP compoent or not.
157
     *
158
     * @return bool
159
     */
160
    protected function isIpComponent(): bool
161
    {
162
        if ($this->getComponent('Ip')) {
163
164
            $result = $this->getComponent('Ip')->check();
0 ignored issues
show
Bug introduced by
The method check() does not exist on Shieldon\Firewall\Component\ComponentInterface. It seems like you code against a sub-type of Shieldon\Firewall\Component\ComponentInterface such as Shieldon\Firewall\Component\Ip. ( Ignorable by Annotation )

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

164
            $result = $this->getComponent('Ip')->/** @scrutinizer ignore-call */ check();
Loading history...
165
            $actionCode = self::ACTION_DENY;
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...onentTrait::ACTION_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
166
167
            if (!empty($result)) {
168
169
                switch ($result['status']) {
170
171
                    case 'allow':
172
                        $actionCode = self::ACTION_ALLOW;
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...nentTrait::ACTION_ALLOW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
173
                        $reasonCode = $result['code'];
174
                        break;
175
    
176
                    case 'deny':
177
                        $actionCode = self::ACTION_DENY;
178
                        $reasonCode = $result['code']; 
179
                        break;
180
                }
181
182
                $this->action($actionCode, $reasonCode);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $reasonCode does not seem to be defined for all execution paths leading up to this point.
Loading history...
183
184
                // $resultCode = $actionCode
185
                $this->result = $this->sessionHandler($actionCode);
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
It seems like sessionHandler() 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

185
                /** @scrutinizer ignore-call */ 
186
                $this->result = $this->sessionHandler($actionCode);
Loading history...
186
                return true;
187
            }
188
        }
189
        return false;
190
    }
191
192
    /**
193
     * Check other compoents.
194
     *
195
     * @return bool
196
     */
197
    protected function isComponents()
198
    {
199
        foreach ($this->component as $component) {
200
201
            if ($component->isDenied()) {
202
203
                $this->action(
204
                    self::ACTION_DENY,
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...onentTrait::ACTION_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
205
                    $component->getDenyStatusCode()
206
                );
207
208
                $this->result = self::RESPONSE_DENY;
0 ignored issues
show
Bug introduced by
The constant Shieldon\Firewall\Kernel...entTrait::RESPONSE_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
209
                return true;
210
            }
211
        }
212
        return false;
213
    }
214
}
215