1 | <?php |
||
2 | /** |
||
3 | * Copyright (c) 2019. Volodymyr Hryvinskyi. All rights reserved. |
||
4 | * @author: <mailto:[email protected]> |
||
5 | * @github: <https://github.com/hryvinskyi> |
||
6 | */ |
||
7 | |||
8 | namespace Hryvinskyi\InvisibleCaptcha\Model; |
||
9 | |||
10 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Backend; |
||
11 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\Frontend; |
||
12 | use Hryvinskyi\InvisibleCaptcha\Helper\Config\General; |
||
13 | use InvalidArgumentException; |
||
14 | use Magento\Framework\App\Area; |
||
0 ignored issues
–
show
|
|||
15 | |||
16 | abstract class AbstractCheckEnabledVerify implements CheckEnabledVerifyInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $area; |
||
22 | |||
23 | /** |
||
24 | * @var General |
||
25 | */ |
||
26 | private $generalConfig; |
||
27 | |||
28 | /** |
||
29 | * @var Frontend |
||
30 | */ |
||
31 | private $frontendConfig; |
||
32 | |||
33 | /** |
||
34 | * @var Backend |
||
35 | */ |
||
36 | private $backendConfig; |
||
37 | |||
38 | /** |
||
39 | * AbstractCheckEnableVerify constructor. |
||
40 | * |
||
41 | * @param General $generalConfig |
||
42 | * @param Frontend $frontendConfig |
||
43 | * @param Backend $backendConfig |
||
44 | * @param string $area |
||
45 | */ |
||
46 | public function __construct( |
||
47 | General $generalConfig, |
||
48 | Frontend $frontendConfig, |
||
49 | Backend $backendConfig, |
||
50 | string $area |
||
51 | ) { |
||
52 | $this->generalConfig = $generalConfig; |
||
53 | $this->frontendConfig = $frontendConfig; |
||
54 | $this->backendConfig = $backendConfig; |
||
55 | $this->area = $area; |
||
56 | |||
57 | if (!in_array($area, [Area::AREA_FRONTEND, Area::AREA_ADMINHTML])) { |
||
58 | throw new InvalidArgumentException('Area parameter must be one of frontend or adminhtml'); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return General |
||
64 | */ |
||
65 | public function getGeneralConfig(): General |
||
66 | { |
||
67 | return $this->generalConfig; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return Frontend |
||
72 | */ |
||
73 | public function getFrontendConfig(): Frontend |
||
74 | { |
||
75 | return $this->frontendConfig; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return Backend |
||
80 | */ |
||
81 | public function getBackendConfig(): Backend |
||
82 | { |
||
83 | return $this->backendConfig; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Return true if area is configured to be active |
||
88 | * |
||
89 | * @return bool |
||
90 | */ |
||
91 | public function checkArea(): bool |
||
92 | { |
||
93 | $return = false; |
||
94 | |||
95 | if ($this->getGeneralConfig()->hasEnabled() === false) { |
||
96 | return $return; |
||
97 | } |
||
98 | |||
99 | switch ($this->area) { |
||
100 | case Area::AREA_FRONTEND: |
||
101 | $return = $this->getFrontendConfig()->hasEnabled(); |
||
102 | break; |
||
103 | |||
104 | case Area::AREA_ADMINHTML: |
||
105 | $return = $this->getBackendConfig()->hasEnabled(); |
||
106 | break; |
||
107 | } |
||
108 | |||
109 | return $return; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @inheritDoc |
||
114 | */ |
||
115 | public function verify(): bool |
||
116 | { |
||
117 | return $this->checkArea(); |
||
118 | } |
||
119 | } |
||
120 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: