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

CountryRepository   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 25.4 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 32
loc 126
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 0 21 3
A create() 0 13 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\Country;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\Repository\Exception\RecordAlreadyExistsException;
7
use Oc\Repository\Exception\RecordNotPersistedException;
8
9
/**
10
 * Class CountryRepository
11
 *
12
 * @package Oc\Country
13
 */
14
class CountryRepository
15
{
16
    /**
17
     * Database table name that this repository maintains.
18
     *
19
     * @var string
20
     */
21
    const TABLE = 'countries';
22
23
    /**
24
     * @var Connection
25
     */
26
    private $connection;
27
28
    /**
29
     * CountryRepository constructor.
30
     *
31
     * @param Connection $connection
32
     */
33
    public function __construct(Connection $connection)
34
    {
35
        $this->connection = $connection;
36
    }
37
38
    /**
39
     * Fetches all countries.
40
     *
41
     * @return CountryEntity[]
42
     */
43
    public function fetchAll()
44
    {
45
        $statement = $this->connection->createQueryBuilder()
46
            ->select('*')
47
            ->from(self::TABLE)
48
            ->execute();
49
50
        $result = $statement->fetchAll();
51
52
        if ($result === false) {
53
            return [];
54
        }
55
56
        $countries = [];
57
58
        foreach ($result as $item) {
59
            $countries[] = (new CountryEntity())->fromDatabaseArray($item);
60
        }
61
62
        return $countries;
63
    }
64
65
    /**
66
     * Creates a country in the database.
67
     *
68
     * @param CountryEntity $entity
69
     *
70
     * @return CountryEntity
71
     *
72
     * @throws RecordAlreadyExistsException
73
     */
74
    public function create(CountryEntity $entity)
75
    {
76
        if (!$entity->isNew()) {
77
            throw new RecordAlreadyExistsException('The entity does already exist.');
78
        }
79
80
        $this->connection->insert(
81
            self::TABLE,
82
            $entity->toDatabaseArray()
83
        );
84
85
        $entity->short = $this->connection->lastInsertId();
86
    }
87
88
    /**
89
     * Update a country in the database.
90
     *
91
     * @param CountryEntity $entity
92
     *
93
     * @return CountryEntity
94
     *
95
     * @throws RecordNotPersistedException
96
     */
97 View Code Duplication
    public function update(CountryEntity $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...
98
    {
99
        if ($entity->isNew()) {
100
            throw new RecordNotPersistedException('The entity does not exist.');
101
        }
102
103
        $this->connection->update(
104
            self::TABLE,
105
            $entity->toDatabaseArray(),
106
            ['short' => $entity->short]
107
        );
108
109
        $entity->short = $this->connection->lastInsertId();
110
111
        return $entity;
112
    }
113
114
    /**
115
     * Removes a country from the database.
116
     *
117
     * @param CountryEntity $entity
118
     *
119
     * @return CountryEntity
120
     *
121
     * @throws RecordNotPersistedException
122
     */
123 View Code Duplication
    public function remove(CountryEntity $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...
124
    {
125
        if ($entity->isNew()) {
126
            throw new RecordNotPersistedException('The entity does not exist.');
127
        }
128
129
        $this->connection->delete(
130
            self::TABLE,
131
            $entity->toDatabaseArray(),
132
            ['short' => $entity->short]
133
        );
134
135
        $entity->short = null;
136
137
        return $entity;
138
    }
139
}
140