GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#4)
by Felix
05:55 queued 03:42
created

BasicDbEntityService   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 30
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 198
ccs 79
cts 79
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B load() 0 25 3
D save() 0 87 19
A delete() 0 13 2
A getPrimaryKeyWhereSql() 0 14 3
A getPrimaryKeyWhereParameters() 0 10 2
1
<?php
2
/**
3
 * Starlit Db.
4
 *
5
 * @copyright Copyright (c) 2016 Starweb / Ehandelslogik i Lund AB
6
 * @license   BSD 3-Clause
7
 */
8
9
namespace Starlit\Db;
10
11
use Starlit\Db\Exception;
12
13
/**
14
 * A database entity service is intended to handle database operations for existing
15
 * entity objects, i.e. load, save and load secondary objects etc.
16
 *
17
 * @author Andreas Nilsson <http://github.com/jandreasn>
18
 */
19
class BasicDbEntityService
20
{
21
    /**
22
     * The database adapter/connection/handler.
23
     *
24
     * @var Db
25
     */
26
    protected $db;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param Db $db
32
     */
33 14
    public function __construct(Db $db)
34
    {
35 14
        $this->db = $db;
36 14
    }
37
38
    /**
39
     * Load object's values from database table.
40
     *
41
     * @param AbstractDbEntity $dbEntity
42
     * @throws Exception\EntityNotFoundException
43
     */
44 3
    public function load(AbstractDbEntity $dbEntity)
45
    {
46
        // Check that the primary key is set
47 3
        if (!$dbEntity->getPrimaryDbValue()) {
48 1
            throw new \InvalidArgumentException(
49 1
                'Database entity can not be loaded because primary value is not set'
50
            );
51
        }
52
53
        // Fetch ad from db
54 2
        $row = $this->db->fetchRow(
55
            '
56
                SELECT *
57 2
                FROM `' . $dbEntity->getDbTableName() . '`
58 2
                WHERE ' . $this->getPrimaryKeyWhereSql($dbEntity) . '
59 2
            ',
60 2
            $this->getPrimaryKeyWhereParameters($dbEntity)
0 ignored issues
show
Documentation introduced by
$this->getPrimaryKeyWhereParameters($dbEntity) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
        );
62
63 2
        if (!$row) {
64 1
            throw new Exception\EntityNotFoundException("Db entity[{$dbEntity->getPrimaryDbValue()}] does not exist");
65
        }
66
67 1
        $dbEntity->setDbDataFromRow($row);
68 1
    }
69
70
    /**
71
     * Save entity to database table.
72
     *
73
     * @param AbstractDbEntity $dbEntity
74
     * @return bool
75
     */
76 9
    public function save(AbstractDbEntity $dbEntity)
77
    {
78 9
        if ($dbEntity->shouldBeDeletedFromDbOnSave()) {
79
            // Only delete if previously saved to db
80 2
            if ($dbEntity->getPrimaryDbValue()) {
81 1
                $this->delete($dbEntity);
82
            }
83
84 2
            return false;
85
        }
86
87 7
        if ($dbEntity->shouldInsertOnDbSave()) {
88
            // Note that database data always contains all properties, with defaults for non set properties
89 4
            $dataToSave = $dbEntity->getDbData();
90
        } else {
91 3
            if ($dbEntity->hasModifiedDbProperties()) {
92 2
                $dataToSave = $dbEntity->getModifiedDbData();
93
            } else {
94
                // Return if no value has been modified and it's not an insert
95
                // (we always want to insert if no id exist, since some child objects might
96
                // depend on the this primary id being available)
97 1
                return false;
98
            }
99
        }
100
101
        // We don't the want to insert/update primary value unless forced insert
102 6
        if (!$dbEntity->shouldForceDbInsertOnSave()) {
103 6
            $primaryKey = $dbEntity->getPrimaryDbPropertyKey();
104 6
            if (is_array($primaryKey)) {
105 1
                foreach ($primaryKey as $keyPart) {
106 1
                    unset($dataToSave[$keyPart]);
107
                }
108
            } else {
109 5
                unset($dataToSave[$primaryKey]);
110
            }
111
        }
112
113
        // Convert & check data
114 6
        $sqlData = [];
115 6
        foreach ($dataToSave as $propertyName => $value) {
116 6
            if ($value
117 6
                && $dbEntity->getDbPropertyMaxLength($propertyName)
118 6
                && mb_strlen($value) > $dbEntity->getDbPropertyMaxLength($propertyName)
119
            ) {
120 1
                throw new \RuntimeException(
121 1
                    "Database field \"{$propertyName}\" exceeds field max length (value: \"{$value}\")"
122
                );
123 5
            } elseif (empty($value) && $dbEntity->getDbPropertyNonEmpty($propertyName)) {
124 1
                throw new \RuntimeException("Database field \"{$propertyName}\" is empty and required");
125 5
            } elseif ((((string) $value) === '') && $dbEntity->getDbPropertyRequired($propertyName)) {
126 1
                throw new \RuntimeException("Database field \"{$propertyName}\" is required to be set");
127
            }
128
129
            // Set data keys db field format
130 4
            $fieldName = $dbEntity->getDbFieldName($propertyName);
131 4
            $sqlData[$fieldName] = $value;
132
        }
133
134
135
        // Insert new database row
136 3
        if ($dbEntity->shouldInsertOnDbSave()) {
137 1
            $this->db->insert(
138 1
                $dbEntity->getDbTableName(),
139
                $sqlData
140
            );
141
142 1
            if (!is_array($dbEntity->getPrimaryDbPropertyKey())
143 1
                && !empty($lastInsertId = $this->db->getLastInsertId())
144
            ) {
145 1
                $dbEntity->setPrimaryDbValue($lastInsertId);
146
            }
147
        // Update existing database row
148
        } else {
149 2
            $this->db->update(
150 2
                $dbEntity->getDbTableName(),
151
                $sqlData,
152 2
                $this->getPrimaryKeyWhereSql($dbEntity),
153 2
                $this->getPrimaryKeyWhereParameters($dbEntity)
0 ignored issues
show
Documentation introduced by
$this->getPrimaryKeyWhereParameters($dbEntity) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
154
155
            );
156
        }
157
158 3
        $dbEntity->clearModifiedDbProperties();
159 3
        $dbEntity->setForceDbInsertOnSave(false);
160
161 3
        return true;
162
    }
163
164
    /**
165
     * Delete entity's corresponding database row.
166
     *
167
     * @param AbstractDbEntity $dbEntity
168
     */
169 3
    public function delete(AbstractDbEntity $dbEntity)
170
    {
171 3
        if (!$dbEntity->getPrimaryDbValue()) {
172 1
            throw new \InvalidArgumentException('Primary database value not set');
173
        }
174
175 2
        $this->db->exec(
176 2
            'DELETE FROM `' . $dbEntity->getDbTableName() . '` WHERE ' . $this->getPrimaryKeyWhereSql($dbEntity),
177 2
            $this->getPrimaryKeyWhereParameters($dbEntity)
0 ignored issues
show
Documentation introduced by
$this->getPrimaryKeyWhereParameters($dbEntity) is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
        );
179
180 2
        $dbEntity->setDeleted();
181 2
    }
182
183
    /**
184
     * @param AbstractDbEntity $dbEntity
185
     * @return string
186
     */
187 6
    private function getPrimaryKeyWhereSql(AbstractDbEntity $dbEntity)
188
    {
189 6
        if (is_array($dbEntity->getPrimaryDbPropertyKey())) {
190 1
            $whereStrings = [];
191 1
            foreach ($dbEntity->getPrimaryDbPropertyKey() as $primaryKeyPart) {
192 1
                $whereStrings[] = '`' . $primaryKeyPart . '` = ?';
193
            }
194 1
            $whereSql = implode(' AND ', $whereStrings);
195
        } else {
196 5
            $whereSql = '`' . $dbEntity->getPrimaryDbFieldKey() . '` = ?';
197
        }
198
199 6
        return $whereSql;
200
    }
201
202
    /**
203
     * @param AbstractDbEntity $dbEntity
204
     * @return string
205
     */
206 6
    private function getPrimaryKeyWhereParameters(AbstractDbEntity $dbEntity)
207
    {
208 6
        if (is_array($dbEntity->getPrimaryDbPropertyKey())) {
209 1
            $whereParameters = $dbEntity->getPrimaryDbValue();
210
        } else {
211 5
            $whereParameters = [$dbEntity->getPrimaryDbValue()];
212
        }
213
214 6
        return $whereParameters;
215
    }
216
}
217