Issues (38)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/services/traits/ObjectById.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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
It seems like createFromRecord() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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