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.
1 | <?php |
||
2 | /** |
||
3 | * This file is part of the LockerManager package. |
||
4 | * |
||
5 | * (c) Mauro Cassani<https://github.com/mauretto78> |
||
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 | namespace LockerManager\Infrastructure; |
||
12 | |||
13 | use Cocur\Slugify\Slugify; |
||
14 | use LockerManager\Domain\Lock; |
||
15 | use LockerManager\Infrastructure\Exception\ExistingKeyException; |
||
16 | use LockerManager\Infrastructure\Exception\InvalidArgumentException; |
||
17 | use LockerManager\Infrastructure\Exception\LockingKeyException; |
||
18 | use LockerManager\Infrastructure\Exception\NotExistingKeyException; |
||
19 | |||
20 | class FLockerStore implements LockerStoreInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var null|string |
||
24 | */ |
||
25 | private $lockPath; |
||
26 | |||
27 | /** |
||
28 | * FLockerStore constructor. |
||
29 | * @param null $lockPath |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
30 | */ |
||
31 | public function __construct($lockPath = null) |
||
32 | { |
||
33 | if (null === $lockPath) { |
||
34 | $lockPath = sys_get_temp_dir(); |
||
35 | } |
||
36 | |||
37 | if (!is_dir($lockPath)) { |
||
38 | mkdir($lockPath, 0755, true); |
||
39 | } |
||
40 | |||
41 | if (!is_writable($lockPath)) { |
||
42 | throw new InvalidArgumentException(sprintf('The directory "%s" is not writable.', $lockPath)); |
||
43 | } |
||
44 | |||
45 | $this->lockPath = $lockPath; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param Lock $lock |
||
50 | * @throws ExistingKeyException |
||
51 | */ |
||
52 | View Code Duplication | public function acquire(Lock $lock) |
|
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. ![]() |
|||
53 | { |
||
54 | $key = $lock->key(); |
||
55 | $fileName = $this->getLockPath($key); |
||
56 | |||
57 | if (file_exists($fileName)) { |
||
58 | throw new ExistingKeyException(sprintf('The key "%s" already exists.', $key)); |
||
59 | } |
||
60 | |||
61 | $this->save($lock, $fileName); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param Lock $lock |
||
66 | * @param $fileName |
||
67 | * @throws LockingKeyException |
||
68 | */ |
||
69 | private function save(Lock $lock, $fileName) |
||
70 | { |
||
71 | $file = @fopen($fileName, 'w'); |
||
72 | |||
73 | if (flock($file, LOCK_EX)) { |
||
74 | fwrite($file, serialize($lock)); |
||
75 | flock($file, LOCK_UN); |
||
76 | } else { |
||
77 | throw new LockingKeyException(sprintf('Error locking file "%s".', $lock->key())); |
||
78 | } |
||
79 | |||
80 | fclose($file); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * clear all locks |
||
85 | */ |
||
86 | public function clear() |
||
87 | { |
||
88 | $files = scandir($this->lockPath); |
||
89 | |||
90 | foreach ($files as $file) { |
||
91 | if (is_file($this->lockPath.$file)) { |
||
92 | unlink($this->lockPath.$file); |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param $key |
||
99 | * @throws NotExistingKeyException |
||
100 | */ |
||
101 | public function delete($key) |
||
102 | { |
||
103 | if (!$this->exists($key)) { |
||
104 | throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key)); |
||
105 | } |
||
106 | |||
107 | unlink($this->getLockPath($key)); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param $key |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function exists($key) |
||
115 | { |
||
116 | if (!@fopen($this->getLockPath($key), 'r')) { |
||
117 | return false; |
||
118 | } |
||
119 | |||
120 | return true; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param $key |
||
125 | * @return string |
||
126 | */ |
||
127 | private function getLockPath($key) |
||
128 | { |
||
129 | return $this->lockPath.(new Slugify())->slugify($key).'.lock'; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param $key |
||
134 | * @return mixed |
||
135 | * @throws NotExistingKeyException |
||
136 | */ |
||
137 | public function get($key) |
||
138 | { |
||
139 | if (!$this->exists($key)) { |
||
140 | throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key)); |
||
141 | } |
||
142 | |||
143 | return unserialize(file_get_contents($this->getLockPath($key))); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function getAll() |
||
150 | { |
||
151 | $files = scandir($this->lockPath); |
||
152 | $locks = []; |
||
153 | |||
154 | unset($files[0]); |
||
155 | unset($files[1]); |
||
156 | |||
157 | foreach ($files as $lock) { |
||
158 | $locks[] = str_replace('.lock', '', $lock); |
||
159 | } |
||
160 | |||
161 | return array_values($locks); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param $key |
||
166 | * @param $payload |
||
167 | * @throws LockingKeyException |
||
168 | * @throws NotExistingKeyException |
||
169 | */ |
||
170 | View Code Duplication | public function update($key, $payload) |
|
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. ![]() |
|||
171 | { |
||
172 | $fileName = $this->getLockPath($key); |
||
173 | |||
174 | if (!file_exists($fileName)) { |
||
175 | throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key)); |
||
176 | } |
||
177 | |||
178 | /** @var Lock $lock */ |
||
179 | $lock = $this->get($key); |
||
180 | $lock->update($payload); |
||
181 | |||
182 | $this->save($lock, $fileName); |
||
183 | } |
||
184 | } |
||
185 |