Passed
Branch master (754829)
by Herberto
03:42 queued 01:55
created

MySqlPdoRepository::deleteById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Hgraca\MicroOrm\Repository;
3
4
use Hgraca\Common\Collection\Contract\CollectionInterface;
5
use Hgraca\Common\Entity\Concept\EntityAbstract;
6
use Hgraca\Common\Http\HttpStatusCode;
7
use Hgraca\MicroOrm\DataSource\ClientInterface;
8
use Hgraca\MicroOrm\Entity\Contract\EntityDataMapperInterface;
9
use Hgraca\MicroOrm\Entity\EntityDataMapper;
10
use Hgraca\MicroOrm\Repository\Contract\RepositoryInterface;
11
use Hgraca\MicroOrm\Repository\Exception\EntityNotFoundMicroOrmException;
12
use Hgraca\MicroOrm\Repository\Exception\RepositoryMicroOrmException;
13
14
class MySqlPdoRepository implements RepositoryInterface
15
{
16
    /** @var  ClientInterface */
17
    protected $pdoClient;
18
19
    /** @var  EntityDataMapperInterface */
20
    protected $entityDataMapper;
21
22
    public function __construct(
23
        ClientInterface $pdoClient,
24
        EntityDataMapper $entityDataMapper
25
    ) {
26
        $this->pdoClient        = $pdoClient;
27
        $this->entityDataMapper = $entityDataMapper;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     *
33
     * TODO implement $orderBy, $limit and $offset
34
     */
35
    public function findAll(): CollectionInterface
36
    {
37
        return $this->findBy([]);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function findOneById(int $id): EntityAbstract
44
    {
45
        return $this->findOneBy([static::ID_PROPERTY_NAME => $id]);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function findOneBy(array $filter): EntityAbstract
52
    {
53
        $resultArray = $this->findBy($filter, []);
54
55
        $count = count($resultArray);
56
57
        $msg = "Should find exactly one entity and found $count.";
58
59
        if (0 === $count) {
60
            $statusCode = new HttpStatusCode(HttpStatusCode::NOT_FOUND);
61
            throw new EntityNotFoundMicroOrmException($msg, $statusCode->getValue());
62
        }
63
64
        if (1 < $count) {
65
            $statusCode = new HttpStatusCode(HttpStatusCode::INTERNAL_SERVER_ERROR);
66
            throw new RepositoryMicroOrmException($msg, $statusCode->getValue());
67
        }
68
69
        return $resultArray[0];
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     *
75
     * TODO implement $limit and $offset
76
     */
77
    public function findBy(
78
        array $propertyFilter,
79
        array $orderBy = [],
80
        int $limit = null,
81
        int $offset = null
82
    ): CollectionInterface
83
    {
84
        $tableName    = $this->entityDataMapper->getTableName();
85
        $columnFilter = $this->entityDataMapper->mapPropertiesToColumns($propertyFilter);
86
        $recordList   = $this->pdoClient->select($tableName, $columnFilter, $orderBy);
87
88
        return $this->entityDataMapper->mapRecordListToEntityCollection($recordList);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function persist(EntityAbstract $entity): int
95
    {
96
        $record = $this->entityDataMapper->mapEntityToRecord($entity);
97
98
        // if the entity has an ID set, it will update, otherwise it will insert
99
        $id = $entity->getId();
100
        if (empty($id)) {
101
            $affectedRows = $this->pdoClient->insert($this->entityDataMapper->getTableName(), $record);
102
        } else {
103
            $affectedRows = $this->pdoClient->update($this->entityDataMapper->getTableName(), $record);
104
        }
105
106
        $this->entityDataMapper->updateEntityFromRecord($entity, $record);
107
108
        return $affectedRows;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function deleteById(int $id): int
115
    {
116
        return $this->deleteBy([static::ID_PROPERTY_NAME => $id]);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function deleteBy(array $propertyFilter): int
123
    {
124
        $columnFilter = $this->entityDataMapper->mapPropertiesToColumns($propertyFilter);
125
126
        return $this->pdoClient->delete($this->entityDataMapper->getTableName(), $columnFilter);
127
    }
128
129
    /**
130
     * @param EntityAbstract $entity
131
     *
132
     * @return int The nbr of affected rows
133
     */
134
    public function delete(EntityAbstract $entity): int
135
    {
136
        return $this->deleteById($entity->getId());
137
    }
138
}
139