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

PageRepository   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 27.41 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 37
loc 135
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B fetchOneBy() 5 26 4
A create() 0 15 2
A update() 16 16 2
A remove() 16 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 PageRepository
12
 *
13
 * @package Oc\Page
14
 */
15
class PageRepository
16
{
17
    /**
18
     * Database table name that this repository maintains.
19
     *
20
     * @var string
21
     */
22
    const TABLE = 'page';
23
24
    /**
25
     * @var Connection
26
     */
27
    private $connection;
28
29
    /**
30
     * PageRepository constructor.
31
     *
32
     * @param Connection $connection
33
     */
34
    public function __construct(Connection $connection)
35
    {
36
        $this->connection = $connection;
37
    }
38
39
    /**
40
     * Fetches a page by slug.
41
     *
42
     * @param array $where
43
     *
44
     * @return null|PageEntity
45
     */
46
    public function fetchOneBy(array $where = [])
47
    {
48
        $queryBuilder = $this->connection->createQueryBuilder()
49
            ->select('*')
50
            ->from(self::TABLE)
51
            ->setMaxResults(1);
52
53
54 View Code Duplication
        if (count($where) > 0) {
55
            foreach ($where as $column => $value) {
56
                $queryBuilder->andWhere($column . ' = ' .  $queryBuilder->createNamedParameter($value));
57
            }
58
        }
59
60
        $statement = $queryBuilder->execute();
61
62
        $result = $statement->fetch();
63
64
        if ($result === false) {
65
            return null;
66
        }
67
68
        $entity = new PageEntity();
69
70
        return $entity->fromDatabaseArray($result);
71
    }
72
73
    /**
74
     * Creates a page in the database.
75
     *
76
     * @param PageEntity $entity
77
     *
78
     * @return PageEntity
79
     *
80
     * @throws RecordAlreadyExistsException
81
     */
82
    public function create(PageEntity $entity)
83
    {
84
        if (!$entity->isNew()) {
85
            throw new RecordAlreadyExistsException('The entity does already exist.');
86
        }
87
88
        $this->connection->insert(
89
            self::TABLE,
90
            $entity->toDatabaseArray()
91
        );
92
93
        $entity->id = (int) $this->connection->lastInsertId();
94
95
        return $entity;
96
    }
97
98
    /**
99
     * Update a page in the database.
100
     *
101
     * @param PageEntity $entity
102
     *
103
     * @return PageEntity
104
     *
105
     * @throws RecordNotPersistedException
106
     */
107 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...
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 PageEntity $entity
128
     *
129
     * @return PageEntity
130
     *
131
     * @throws RecordNotPersistedException
132
     */
133 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...
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