Completed
Pull Request — development (#546)
by Nick
07:54 queued 01:05
created

PageRepository::fetchOneBy()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 5
Ratio 19.23 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 1
dl 5
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\Page;
4
5
use Doctrine\DBAL\Connection;
6
use InvalidArgumentException;
7
8
/**
9
 * Class PageRepository
10
 *
11
 * @package Oc\Page
12
 * @author Nick Lubisch <[email protected]>
13
 */
14
class PageRepository
15
{
16
    /**
17
     * Database table name that this repository maintains.
18
     *
19
     * @var string
20
     */
21
    const TABLE = 'page';
22
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
28
    /**
29
     * PageRepository constructor.
30
     *
31
     * @param Connection $connection
32
     */
33
    public function __construct(Connection $connection)
34
    {
35
        $this->connection = $connection;
36
    }
37
38
    /**
39
     * Fetches a page by slug.
40
     *
41
     * @param array $where
42
     *
43
     * @return null|PageEntity
44
     */
45
    public function fetchOneBy(array $where = [])
46
    {
47
        $queryBuilder = $this->connection->createQueryBuilder()
48
            ->select('*')
49
            ->from(self::TABLE)
50
            ->setMaxResults(1);
51
52
53 View Code Duplication
        if (count($where) > 0) {
54
            foreach ($where as $column => $value) {
55
                $queryBuilder->andWhere($column . ' = ' .  $queryBuilder->createNamedParameter($value));
56
            }
57
        }
58
59
        $statement = $queryBuilder->execute();
60
61
        $result = $statement->fetch();
62
63
        if ($result === false) {
64
            return null;
65
        }
66
67
        $entity = new PageEntity();
68
69
        return $entity->fromDatabaseArray($result);
70
    }
71
72
    /**
73
     * Creates a page in the database.
74
     *
75
     * @param PageEntity $entity
76
     *
77
     * @return PageEntity
78
     */
79
    public function create(PageEntity $entity)
80
    {
81
        if (!$entity->isNew()) {
82
            throw new InvalidArgumentException('The entity does already exist.');
83
        }
84
85
        $this->connection->insert(
86
            self::TABLE,
87
            $entity->toDatabaseArray()
88
        );
89
90
        $entity->id = (int) $this->connection->lastInsertId();
91
92
        return $entity;
93
    }
94
95
    /**
96
     * Update a page in the database.
97
     *
98
     * @param PageEntity $entity
99
     *
100
     * @return PageEntity
101
     */
102 View Code Duplication
    public function update(PageEntity $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...
103
    {
104
        if ($entity->isNew()) {
105
            throw new \InvalidArgumentException('The entity does not exist.');
106
        }
107
108
        $this->connection->update(
109
            self::TABLE,
110
            $entity->toDatabaseArray(),
111
            ['id' => $entity->id]
112
        );
113
114
        $entity->id = (int) $this->connection->lastInsertId();
115
116
        return $entity;
117
    }
118
119
    /**
120
     * Removes a page from the database.
121
     *
122
     * @param PageEntity $entity
123
     *
124
     * @return PageEntity
125
     */
126 View Code Duplication
    public function remove(PageEntity $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...
127
    {
128
        if ($entity->isNew()) {
129
            throw new \InvalidArgumentException('The entity does not exist.');
130
        }
131
132
        $this->connection->delete(
133
            self::TABLE,
134
            $entity->toDatabaseArray(),
135
            ['id' => $entity->id]
136
        );
137
138
        $entity->id = null;
139
140
        return $entity;
141
    }
142
}
143