Completed
Pull Request — development (#546)
by Nick
06:41
created

BlockRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Oc\Page;
4
5
use Doctrine\DBAL\Connection;
6
use InvalidArgumentException;
7
use Oc\Repository\Exception\RecordAlreadyExistsException;
8
use Oc\Repository\Exception\RecordNotPersistedException;
9
10
/**
11
 * Class BlockRepository
12
 *
13
 * @package Oc\Page
14
 */
15
class BlockRepository
16
{
17
    /**
18
     * Database table name that this repository maintains.
19
     *
20
     * @var string
21
     */
22
    const TABLE = 'page_block';
23
24
    /**
25
     * @var Connection
26
     */
27
    private $connection;
28
29
    /**
30
     * BlockRepository constructor.
31
     *
32
     * @param Connection $connection
33
     */
34
    public function __construct(Connection $connection)
35
    {
36
        $this->connection = $connection;
37
    }
38
39
    /**
40
     * Fetches all blocks of a page.
41
     *
42
     * @param array $where
43
     *
44
     * @return BlockEntity[]
45
     */
46
    public function fetchBy(array $where = [])
47
    {
48
        $queryBuilder = $this->connection->createQueryBuilder()
49
            ->select('*')
50
            ->from(self::TABLE);
51
52 View Code Duplication
        if (count($where) > 0) {
53
            foreach ($where as $column => $value) {
54
                $queryBuilder->andWhere($column . ' = ' .  $queryBuilder->createNamedParameter($value));
55
            }
56
        }
57
58
        $statement = $queryBuilder->execute();
59
60
        $result = $statement->fetchAll();
61
62
        if ($result === false) {
63
            return [];
64
        }
65
66
        $blocks = [];
67
68
        foreach ($result as $item) {
69
            $blocks[] = (new BlockEntity())->fromDatabaseArray($item);
70
        }
71
72
        return $blocks;
73
    }
74
75
    /**
76
     * Creates a block in the database.
77
     *
78
     * @param BlockEntity $entity
79
     *
80
     * @return BlockEntity
81
     *
82
     * @throws RecordAlreadyExistsException
83
     */
84
    public function create(BlockEntity $entity)
85
    {
86
        if (!$entity->isNew()) {
87
            throw new RecordAlreadyExistsException('The entity does already exist.');
88
        }
89
90
        $this->connection->insert(
91
            self::TABLE,
92
            $entity->toDatabaseArray()
93
        );
94
95
        $entity->id = (int) $this->connection->lastInsertId();
96
    }
97
98
    /**
99
     * Update a page in the database.
100
     *
101
     * @param BlockEntity $entity
102
     *
103
     * @return BlockEntity
104
     *
105
     * @throws RecordNotPersistedException
106
     */
107 View Code Duplication
    public function update(BlockEntity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        if ($entity->isNew()) {
110
            throw new RecordNotPersistedException('The entity does not exist.');
111
        }
112
113
        $this->connection->update(
114
            self::TABLE,
115
            $entity->toDatabaseArray(),
116
            ['id' => $entity->id]
117
        );
118
119
        $entity->id = (int) $this->connection->lastInsertId();
120
121
        return $entity;
122
    }
123
124
    /**
125
     * Removes a page from the database.
126
     *
127
     * @param BlockEntity $entity
128
     *
129
     * @return BlockEntity
130
     *
131
     * @throws RecordNotPersistedException
132
     */
133 View Code Duplication
    public function remove(BlockEntity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
    {
135
        if ($entity->isNew()) {
136
            throw new RecordNotPersistedException('The entity does not exist.');
137
        }
138
139
        $this->connection->delete(
140
            self::TABLE,
141
            $entity->toDatabaseArray(),
142
            ['id' => $entity->id]
143
        );
144
145
        $entity->id = null;
146
147
        return $entity;
148
    }
149
}
150