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 | |||
3 | /** |
||
4 | * @copyright Copyright (c) Flipbox Digital Limited |
||
5 | * @license https://github.com/flipbox/spark/blob/master/LICENSE |
||
6 | * @link https://github.com/flipbox/spark |
||
7 | */ |
||
8 | |||
9 | namespace flipbox\spark\services\traits; |
||
10 | |||
11 | use flipbox\spark\exceptions\ObjectNotFoundException; |
||
12 | use flipbox\spark\exceptions\RecordNotFoundException; |
||
13 | use flipbox\spark\objects\ObjectWithId; |
||
14 | use flipbox\spark\records\Record; |
||
15 | use flipbox\spark\records\RecordWithId; |
||
16 | use yii\base\Object as BaseObject; |
||
17 | |||
18 | /** |
||
19 | * @author Flipbox Factory <[email protected]> |
||
20 | * @since 2.0.0 |
||
21 | * |
||
22 | * @method RecordWithId|null findRecordByCondition($condition, string $toScenario = null) |
||
23 | */ |
||
24 | trait ObjectById |
||
25 | { |
||
26 | |||
27 | use Object; |
||
28 | |||
29 | /** |
||
30 | * @var BaseObject[] |
||
31 | */ |
||
32 | protected $cacheById = []; |
||
33 | |||
34 | /** |
||
35 | * @param Record $record |
||
36 | * @param string|null $toScenario |
||
37 | * @return BaseObject |
||
38 | */ |
||
39 | abstract protected function findByRecord(Record $record, string $toScenario = null): BaseObject; |
||
40 | |||
41 | |||
42 | /******************************************* |
||
43 | * FIND/GET BY ID |
||
44 | *******************************************/ |
||
45 | |||
46 | /** |
||
47 | * @param int $id |
||
48 | * @param string|null $toScenario |
||
49 | * @return BaseObject|null |
||
50 | */ |
||
51 | public function findById(int $id, string $toScenario = null) |
||
52 | { |
||
53 | |||
54 | // Check cache |
||
55 | if (!$object = $this->findCacheById($id)) { |
||
56 | // Find record in db |
||
57 | if ($record = $this->findRecordByCondition( |
||
58 | ['id' => $id] |
||
59 | ) |
||
60 | ) { |
||
61 | // Perhaps in cache |
||
62 | $object = $this->findByRecord($record, $toScenario); |
||
63 | } else { |
||
64 | $this->cacheById[$id] = null; |
||
65 | |||
66 | return null; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | return $object; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param int $id |
||
75 | * @param string|null $toScenario |
||
76 | * @return BaseObject |
||
77 | * @throws ObjectNotFoundException |
||
78 | */ |
||
79 | public function getById(int $id, string $toScenario = null): BaseObject |
||
80 | { |
||
81 | |||
82 | // Find by ID |
||
83 | if (!$object = $this->findById($id, $toScenario)) { |
||
84 | $this->notFoundByIdException($id); |
||
85 | } |
||
86 | |||
87 | return $object; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param int $id |
||
92 | * @param string|null $toScenario |
||
93 | * @return BaseObject|null |
||
94 | */ |
||
95 | public function freshFindById(int $id, string $toScenario = null) |
||
96 | { |
||
97 | |||
98 | // Find record in db |
||
99 | if (!$record = $this->findRecordById($id)) { |
||
100 | return null; |
||
101 | } |
||
102 | |||
103 | return $this->createFromRecord($record, $toScenario); |
||
0 ignored issues
–
show
|
|||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param int $id |
||
108 | * @param string|null $toScenario |
||
109 | * @return BaseObject |
||
110 | * @throws ObjectNotFoundException |
||
111 | */ |
||
112 | public function freshGetById(int $id, string $toScenario = null): BaseObject |
||
113 | { |
||
114 | |||
115 | if (!$object = $this->freshFindById($id, $toScenario)) { |
||
116 | $this->notFoundByIdException($id); |
||
117 | } |
||
118 | |||
119 | return $object; |
||
120 | } |
||
121 | |||
122 | |||
123 | /******************************************* |
||
124 | * CACHE |
||
125 | *******************************************/ |
||
126 | |||
127 | /** |
||
128 | * Find an existing cache by ID |
||
129 | * |
||
130 | * @param $id |
||
131 | * @return BaseObject|null |
||
132 | */ |
||
133 | public function findCacheById(int $id) |
||
134 | { |
||
135 | |||
136 | // Check if already in addToCache |
||
137 | if ($this->isCachedById($id)) { |
||
138 | return $this->cacheById[$id]; |
||
139 | } |
||
140 | |||
141 | return null; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Identify whether in cache by ID |
||
146 | * |
||
147 | * @param $id |
||
148 | * @return bool |
||
149 | */ |
||
150 | protected function isCachedById(int $id) |
||
151 | { |
||
152 | return array_key_exists($id, $this->cacheById); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param ObjectWithId $object |
||
157 | * @return $this |
||
158 | */ |
||
159 | protected function cacheById(ObjectWithId $object) |
||
160 | { |
||
161 | |||
162 | // Check if already in cache |
||
163 | if (!$id = $this->isCachedById($object->id)) { |
||
164 | // Cache it |
||
165 | $this->cacheById[$id] = $object; |
||
166 | } |
||
167 | |||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | |||
172 | /******************************************* |
||
173 | * RECORD BY ID |
||
174 | *******************************************/ |
||
175 | |||
176 | /** |
||
177 | * @param int $id |
||
178 | * @param string|null $toScenario |
||
179 | * @return RecordWithId|null |
||
180 | */ |
||
181 | public function findRecordById(int $id, string $toScenario = null) |
||
182 | { |
||
183 | |||
184 | return $this->findRecordByCondition( |
||
185 | [ |
||
186 | 'id' => $id |
||
187 | ], |
||
188 | $toScenario |
||
189 | ); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param int $id |
||
194 | * @param string|null $toScenario |
||
195 | * @throws RecordNotFoundException |
||
196 | * @return RecordWithId|null |
||
197 | */ |
||
198 | public function getRecordById(int $id, string $toScenario = null) |
||
199 | { |
||
200 | |||
201 | if (!$record = $this->findRecordById($id, $toScenario)) { |
||
202 | $this->notFoundRecordByIdException($id); |
||
203 | } |
||
204 | |||
205 | return $record; |
||
206 | } |
||
207 | |||
208 | |||
209 | /******************************************* |
||
210 | * EXCEPTIONS |
||
211 | *******************************************/ |
||
212 | |||
213 | /** |
||
214 | * @param int|null $id |
||
215 | * @throws ObjectNotFoundException |
||
216 | */ |
||
217 | protected function notFoundByIdException(int $id = null) |
||
218 | { |
||
219 | |||
220 | throw new ObjectNotFoundException( |
||
221 | sprintf( |
||
222 | 'Object does not exist with the id "%s".', |
||
223 | (string)$id |
||
224 | ) |
||
225 | ); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @param int|null $id |
||
230 | * @throws RecordNotFoundException |
||
231 | */ |
||
232 | protected function notFoundRecordByIdException(int $id = null) |
||
233 | { |
||
234 | |||
235 | throw new RecordNotFoundException( |
||
236 | sprintf( |
||
237 | 'Record does not exist with the id "%s".', |
||
238 | (string)$id |
||
239 | ) |
||
240 | ); |
||
241 | } |
||
242 | } |
||
243 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.