This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace FMUP\Cache\Driver; |
||
3 | |||
4 | use FMUP\Cache\CacheInterface; |
||
5 | use FMUP\Cache\Exception; |
||
6 | |||
7 | class Shm implements CacheInterface |
||
8 | { |
||
9 | const SETTING_NAME = 'SETTING_NAME'; |
||
10 | const SETTING_SIZE = 'SETTING_SIZE'; |
||
11 | |||
12 | private $shmInstance = null; |
||
13 | private $isAvailable = null; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $settings = array(); |
||
19 | |||
20 | /** |
||
21 | * constructor of File |
||
22 | * @param array $settings |
||
23 | */ |
||
24 | 14 | public function __construct($settings = array()) |
|
25 | { |
||
26 | 14 | $this->setSettings($settings); |
|
27 | 14 | } |
|
28 | |||
29 | /** |
||
30 | * Can define settings of the component |
||
31 | * @param array $settings |
||
32 | * @return $this |
||
33 | */ |
||
34 | 14 | public function setSettings($settings = array()) |
|
35 | { |
||
36 | 14 | $this->settings = $settings; |
|
37 | 14 | return $this; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param string $setting |
||
42 | * @param mixed $value |
||
43 | * @return $this |
||
44 | */ |
||
45 | 2 | public function setSetting($setting, $value) |
|
46 | { |
||
47 | 2 | $this->settings[$setting] = $value; |
|
48 | 2 | return $this; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Get a specific value of setting |
||
53 | * @param string $setting |
||
54 | * @return mixed |
||
55 | */ |
||
56 | 8 | public function getSetting($setting) |
|
57 | { |
||
58 | 8 | return isset($this->settings[$setting]) ? $this->settings[$setting] : null; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Internal method to secure a SHM name |
||
63 | * @param string $name |
||
64 | * @return int |
||
65 | */ |
||
66 | 7 | private function secureName($name = null) |
|
67 | { |
||
68 | 7 | return is_null($name) ? 1 : $this->stringToUniqueId($name); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Convert string to a unique id |
||
73 | * @param string $string |
||
74 | * @return int |
||
75 | */ |
||
76 | 7 | private function stringToUniqueId($string) |
|
77 | { |
||
78 | 7 | if (is_numeric($string)) { |
|
79 | 6 | return (int)$string; |
|
80 | } |
||
81 | 6 | $length = strlen($string); |
|
82 | 6 | $return = 0; |
|
83 | 6 | View Code Duplication | for ($i = 0; $i < $length; $i++) { |
0 ignored issues
–
show
|
|||
84 | 6 | $return += ord($string{$i}); |
|
85 | } |
||
86 | 6 | return (int)$length . '1' . $return; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get SHM resource |
||
91 | * @return resource |
||
92 | * @throws Exception |
||
93 | */ |
||
94 | 8 | private function getShm() |
|
95 | { |
||
96 | 8 | if (!$this->isAvailable()) { |
|
97 | 1 | throw new Exception('SHM is not available'); |
|
98 | } |
||
99 | 7 | if (!$this->shmInstance) { |
|
100 | 7 | $memorySize = $this->getSetting(self::SETTING_SIZE); |
|
101 | 7 | $shmName = $this->secureName($this->getSetting(self::SETTING_NAME)); |
|
102 | 7 | $this->shmInstance = is_numeric($memorySize) |
|
103 | 1 | ? $this->shmAttach($shmName, (int)$memorySize) |
|
104 | 6 | : $this->shmAttach($shmName); |
|
105 | } |
||
106 | 7 | return $this->shmInstance; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return resource |
||
111 | * @codeCoverageIgnore |
||
112 | */ |
||
113 | protected function shmAttach() |
||
114 | { |
||
115 | return call_user_func_array('shm_attach', func_get_args()); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return mixed |
||
120 | * @codeCoverageIgnore |
||
121 | */ |
||
122 | protected function shmGetVar() |
||
123 | { |
||
124 | return call_user_func_array('shm_get_var', func_get_args()); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Retrieve stored value |
||
129 | * @param string $key |
||
130 | * @return mixed|null |
||
131 | * @throws Exception |
||
132 | */ |
||
133 | 2 | View Code Duplication | public function get($key) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
134 | { |
||
135 | 2 | if (!$this->isAvailable()) { |
|
136 | 1 | throw new Exception('SHM is not available'); |
|
137 | } |
||
138 | 1 | $key = $this->secureName($key); |
|
139 | 1 | return ($this->has($key)) ? $this->shmGetVar($this->getShm(), $key) : null; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return bool |
||
144 | * @codeCoverageIgnore |
||
145 | */ |
||
146 | protected function shmHasVar() |
||
147 | { |
||
148 | return call_user_func_array('shm_has_var', func_get_args()); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Check whether key exists in SHM |
||
153 | * @param string $key |
||
154 | * @return bool |
||
155 | * @throws Exception |
||
156 | */ |
||
157 | 5 | View Code Duplication | public function has($key) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
158 | { |
||
159 | 5 | if (!$this->isAvailable()) { |
|
160 | 1 | throw new Exception('SHM is not available'); |
|
161 | } |
||
162 | 4 | $key = $this->secureName($key); |
|
163 | 4 | return $this->shmHasVar($this->getShm(), $key); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Remove a stored key if exists |
||
168 | * @param string $key |
||
169 | * @return $this |
||
170 | * @throws Exception |
||
171 | */ |
||
172 | 3 | View Code Duplication | public function remove($key) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
173 | { |
||
174 | 3 | if (!$this->isAvailable()) { |
|
175 | 1 | throw new Exception('SHM is not available'); |
|
176 | } |
||
177 | 2 | $key = $this->secureName($key); |
|
178 | 2 | if ($this->has($key) && !$this->shmRemoveVar($this->getShm(), $key)) { |
|
179 | 1 | throw new Exception('Unable to delete key from cache Shm'); |
|
180 | } |
||
181 | 1 | return $this; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param resource $shmResource |
||
186 | * @param string $key |
||
187 | * @codeCoverageIgnore |
||
188 | * @return bool |
||
189 | */ |
||
190 | protected function shmRemoveVar($shmResource, $key) |
||
191 | { |
||
192 | return shm_remove_var($shmResource, $key); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Define a key in SHM |
||
197 | * @param string $key |
||
198 | * @param mixed $value |
||
199 | * @throws Exception |
||
200 | * @return $this |
||
201 | */ |
||
202 | 4 | View Code Duplication | public function set($key, $value) |
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
203 | { |
||
204 | 4 | if (!$this->isAvailable()) { |
|
205 | 1 | throw new Exception('SHM is not available'); |
|
206 | } |
||
207 | 3 | $key = $this->secureName($key); |
|
208 | 3 | if (!$this->shmPutVar($this->getShm(), $key, $value)) { |
|
209 | 1 | throw new Exception('Unable to define key into cache Shm'); |
|
210 | } |
||
211 | 2 | return $this; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param resource $shmResource |
||
216 | * @param string $key |
||
217 | * @param mixed $value |
||
218 | * @return bool |
||
219 | * @codeCoverageIgnore |
||
220 | */ |
||
221 | protected function shmPutVar($shmResource, $key, $value) |
||
222 | { |
||
223 | return shm_put_var($shmResource, $key, $value); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Check whether apc is available |
||
228 | * @return bool |
||
229 | */ |
||
230 | 1 | public function isAvailable() |
|
231 | { |
||
232 | 1 | if (is_null($this->isAvailable)) { |
|
233 | 1 | $this->isAvailable = function_exists('shm_attach'); |
|
234 | } |
||
235 | 1 | return $this->isAvailable; |
|
236 | } |
||
237 | } |
||
238 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.