1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oc\Country; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CountryRepository |
10
|
|
|
* |
11
|
|
|
* @package Oc\Country |
12
|
|
|
* @author Nick Lubisch <[email protected]> |
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
|
|
|
public function create(CountryEntity $entity) |
73
|
|
|
{ |
74
|
|
|
if (!$entity->isNew()) { |
75
|
|
|
throw new InvalidArgumentException('The entity does already exist.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->connection->insert( |
79
|
|
|
self::TABLE, |
80
|
|
|
$entity->toDatabaseArray() |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$entity->short = $this->connection->lastInsertId(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Update a country in the database. |
88
|
|
|
* |
89
|
|
|
* @param CountryEntity $entity |
90
|
|
|
* |
91
|
|
|
* @return CountryEntity |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function update(CountryEntity $entity) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
if ($entity->isNew()) { |
96
|
|
|
throw new InvalidArgumentException('The entity does not exist.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->connection->update( |
100
|
|
|
self::TABLE, |
101
|
|
|
$entity->toDatabaseArray(), |
102
|
|
|
['short' => $entity->short] |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$entity->short = $this->connection->lastInsertId(); |
106
|
|
|
|
107
|
|
|
return $entity; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Removes a country from the database. |
112
|
|
|
* |
113
|
|
|
* @param CountryEntity $entity |
114
|
|
|
* |
115
|
|
|
* @return CountryEntity |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function remove(CountryEntity $entity) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if ($entity->isNew()) { |
120
|
|
|
throw new InvalidArgumentException('The entity does not exist.'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->connection->delete( |
124
|
|
|
self::TABLE, |
125
|
|
|
$entity->toDatabaseArray(), |
126
|
|
|
['short' => $entity->short] |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$entity->short = null; |
130
|
|
|
|
131
|
|
|
return $entity; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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.