Completed
Push — master ( b6ee6f...445cbd )
by Phil
03:31
created

SqlStore::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Percy\Store;
4
5
use Aura\Filter\FilterFactory;
6
use Aura\SqlQuery\QueryFactory;
7
use Aura\Sql\ExtendedPdo;
8
use PDOException;
9
use Percy\Entity\Collection;
10
use Percy\Entity\EntityInterface;
11
12
class SqlStore extends AbstractStore
13
{
14
    /**
15
     * @var \Aura\Sql\ExtendedPdo
16
     */
17
    protected $dbal;
18
19
    /**
20
     * Construct.
21
     *
22
     * @param \Aura\Sql\ExtendedPdo      $dbal
23
     * @param \Aura\Filter\FilterFactory $filter
24
     */
25
    public function __construct(ExtendedPdo $dbal, FilterFactory $filter)
26
    {
27
        $this->dbal  = $dbal;
28
        $this->query = new QueryFactory('mysql');
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
        parent::__construct($filter);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function create(Collection $collection, array $scopes = [])
36
    {
37
        $this->validate($collection);
38
        $this->decorate($collection, StoreInterface::ON_CREATE);
39
        return $this->collectionIterator($collection, 'insertEntity');
0 ignored issues
show
Documentation introduced by
$collection is of type object<Percy\Entity\Collection>, but the function expects a object<Percy\Store\EntityCollection>.

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...
40
    }
41
42
    /**
43
     * Insert an entity to the database.
44
     *
45
     * @param \Percy\Entity\EntityInterface $entity
46
     *
47
     * @return void
48
     */
49
    protected function insertEntity(EntityInterface $entity)
50
    {
51
        $insert = $this->query->newInsert();
52
        $insert->into($entity->getDataSource());
53
        $insert->cols($entity->toArray(false));
0 ignored issues
show
Documentation introduced by
false is of type boolean, 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...
54
55
        $this->dbal->perform($insert->getStatement(), $insert->getBindValues());
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function read(Collection $collection, array $scopes = [])
62
    {
63
        // mysql need do nothing with the data on a read request
64
        return true;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function update(Collection $collection, array $scopes = [])
71
    {
72
        $this->validate($collection);
73
        $this->decorate($collection, StoreInterface::ON_UPDATE);
74
        return $this->collectionIterator($collection, 'updateEntity');
0 ignored issues
show
Documentation introduced by
$collection is of type object<Percy\Entity\Collection>, but the function expects a object<Percy\Store\EntityCollection>.

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...
75
    }
76
77
    /**
78
     * Update an entity in the database.
79
     *
80
     * @param \Percy\Entity\EntityInterface $entity
81
     *
82
     * @return void
83
     */
84
    protected function updateEntity(EntityInterface $entity)
85
    {
86
        $update = $this->query->newUpdate();
87
        $update->table($entity->getDataSource());
88
        $update->cols($entity->toArray(false));
0 ignored issues
show
Documentation introduced by
false is of type boolean, 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...
89
        $update->where(sprintf('%s = ?', $entity->getPrimary()), $entity[$entity->getPrimary()]);
90
91
        $this->dbal->perform($update->getStatement(), $update->getBindValues());
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function delete(Collection $collection, array $scopes = [])
98
    {
99
        $this->validate($collection);
100
        $this->decorate($collection, StoreInterface::ON_DELETE);
101
        return $this->collectionIterator($collection, 'deleteEntity');
0 ignored issues
show
Documentation introduced by
$collection is of type object<Percy\Entity\Collection>, but the function expects a object<Percy\Store\EntityCollection>.

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...
102
    }
103
104
    /**
105
     * Delete an entity from the database. Be aware, this handles hard deletes.
106
     * To handle soft deletes, extend this class and overload this method.
107
     *
108
     * @param \Percy\Entity\EntityInterface $entity
109
     *
110
     * @return void
111
     */
112
    protected function deleteEntity(EntityInterface $entity)
113
    {
114
        $delete = $this->query->newDelete();
115
        $delete->from($entity->getDataSource());
116
        $delete->where(sprintf('%s = ?', $entity->getPrimary()), $entity[$entity->getPrimary()]);
117
118
        $this->dbal->perform($delete->getStatement(), $delete->getBindValues());
119
    }
120
121
    /**
122
     * Iterate a collection with the correct callback.
123
     *
124
     * @param \Percy\Entity\Collection $collection
125
     * @param string                   $callable
126
     *
127
     * @return boolean
128
     */
129
    protected function collectionIterator(EntityCollection $collection, $callable)
130
    {
131
        $this->dbal->beginEntity();
0 ignored issues
show
Bug introduced by
The method beginEntity() does not seem to exist on object<Aura\Sql\ExtendedPdo>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
        try {
134
            foreach ($collection->getIterator() as $entity) {
135
                call_user_func_array([$this, $callable], [$entity]);
136
            }
137
        } catch (PDOException $e) {
138
            $this->dbal->rollBack();
139
            return false;
140
        }
141
142
        $this->dbal->commit();
143
        return true;
144
    }
145
}
146