Passed
Push — 2.x ( ea8275...d224d2 )
by Terry
02:42
created

ComponentTrait::disableComponents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <[email protected]>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20
21
declare(strict_types=1);
22
23
namespace Shieldon\Firewall\Kernel;
24
25
use Shieldon\Firewall\Component\ComponentInterface;
26
use Shieldon\Firewall\Component\ComponentProvider;
27
28
/*
29
 * @since 1.0.0
30
 */
31
trait ComponentTrait
32
{
33
    /**
34
     * Container for Shieldon components.
35
     *
36
     * @var array
37
     */
38
    public $component = [];
39
40
    /**
41
     * Set a commponent.
42
     *
43
     * @param ComponentProvider $instance The component instance.
44
     *
45
     * @return void
46
     */
47
    public function setComponent(ComponentProvider $instance): void
48
    {
49
        $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

49
        /** @scrutinizer ignore-call */ 
50
        $class = $this->getClassName($instance);
Loading history...
50
        $this->component[$class] = $instance;
51
    }
52
53
    /**
54
     * Get a component instance from component's container.
55
     *
56
     * @param string $name The component's class name.
57
     *
58
     * @return ComponentInterface|null
59
     */
60
    public function getComponent(string $name)
61
    {
62
        if (!isset($this->component[$name])) {
63
            return null;
64
        }
65
66
        return $this->component[$name];
67
    }
68
69
    /**
70
     * Disable components.
71
     *
72
     * @return void
73
     */
74
    public function disableComponents(): void
75
    {
76
        $this->component = [];
77
    }
78
79
    /*
80
    |--------------------------------------------------------------------------
81
    | Stage in Kernel
82
    |--------------------------------------------------------------------------
83
    | The below methods are used in "process" method in Kernel.
84
    */
85
86
    /**
87
     * Initialize components.
88
     *
89
     * @return void
90
     */
91
    protected function initComponents()
92
    {
93
        foreach (array_keys($this->component) as $name) {
94
            $this->component[$name]->setIp($this->ip);
95
            $this->component[$name]->setRdns($this->rdns);
96
97
            // Apply global strict mode to all components by `strictMode()` if nesscessary.
98
            if (isset($this->strictMode)) {
99
                $this->component[$name]->setStrict($this->strictMode);
100
            }
101
        }
102
    }
103
104
    /**
105
     * Check if current IP is trusted or not.
106
     *
107
     * @return bool
108
     */
109
    protected function isTrustedBot()
110
    {
111
        if ($this->getComponent('TrustedBot')) {
112
113
            // We want to put all the allowed robot into the rule list, so that the checking of IP's resolved hostname 
114
            // is no more needed for that IP.
115
            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

115
            if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isAllowed()) {
Loading history...
116
117
                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

117
                if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isGoogle()) {
Loading history...
118
                    // Add current IP into allowed list, because it is from real Google domain.
119
                    $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

119
                    $this->/** @scrutinizer ignore-call */ 
120
                           action(
Loading history...
120
                        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...
121
                        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...
122
                    );
123
124
                } 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

124
                } elseif ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isBing()) {
Loading history...
125
                    // Add current IP into allowed list, because it is from real Bing domain.
126
                    $this->action(
127
                        self::ACTION_ALLOW,
128
                        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...
129
                    );
130
131
                } 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

131
                } elseif ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isYahoo()) {
Loading history...
132
                    // Add current IP into allowed list, because it is from real Yahoo domain.
133
                    $this->action(
134
                        self::ACTION_ALLOW,
135
                        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...
136
                    );
137
138
                } else {
139
                    // Add current IP into allowed list, because you trust it.
140
                    // You have already defined it in the settings.
141
                    $this->action(
142
                        self::ACTION_ALLOW,
143
                        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...
144
                    );
145
                }
146
                // Allowed robots not join to our traffic handler.
147
                $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...
148
                return true;
149
            }
150
        }
151
        return false;
152
    }
153
154
    /**
155
     * Check whether the IP is fake search engine or not.
156
     * The method "isTrustedBot()" must be executed before this method.
157
     *
158
     * @return bool
159
     */
160
    protected function isFakeRobot(): bool
161
    {
162
        if ($this->getComponent('TrustedBot')) {
163
            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

163
            if ($this->getComponent('TrustedBot')->/** @scrutinizer ignore-call */ isFakeRobot()) {
Loading history...
164
                $this->action(
165
                    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
                    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...
167
                );
168
                $this->result = self::RESPONSE_DENY;
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...entTrait::RESPONSE_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
169
                return true;
170
            }
171
        }
172
        return false;
173
    }
174
175
    /**
176
     * Check whether the IP is handled by IP compoent or not.
177
     *
178
     * @return bool
179
     */
180
    protected function isIpComponent(): bool
181
    {
182
        if ($this->getComponent('Ip')) {
183
184
            $result = $this->getComponent('Ip')->check($this->ip);
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

184
            $result = $this->getComponent('Ip')->/** @scrutinizer ignore-call */ check($this->ip);
Loading history...
185
186
            $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...
187
188
            if (!empty($result)) {
189
190
                switch ($result['status']) {
191
192
                    case 'allow':
193
                        $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...
194
                        $reasonCode = $result['code'];
195
                        break;
196
    
197
                    case 'deny':
198
                        $actionCode = self::ACTION_DENY;
199
                        $reasonCode = $result['code']; 
200
                        break;
201
                }
202
203
                $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...
204
205
                // $resultCode = $actionCode
206
                $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

206
                /** @scrutinizer ignore-call */ 
207
                $this->result = $this->sessionHandler($actionCode);
Loading history...
207
                return true;
208
            }
209
        }
210
        return false;
211
    }
212
213
    /**
214
     * Check other compoents.
215
     *
216
     * @return bool
217
     */
218
    protected function isComponents()
219
    {
220
        foreach ($this->component as $component) {
221
222
            if ($component->isDenied()) {
223
224
                $this->action(
225
                    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...
226
                    $component->getDenyStatusCode()
227
                );
228
229
                $this->result = self::RESPONSE_DENY;
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...entTrait::RESPONSE_DENY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
230
                return true;
231
            }
232
        }
233
        return false;
234
    }
235
}
236