1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ContactUs\Service; |
4
|
|
|
|
5
|
|
|
use ContactUs\Filter\ContactUsFilter; |
6
|
|
|
use ContactUs\Mapper\ContactUsMapper; |
7
|
|
|
use MysqlUuid\Formats\Binary; |
8
|
|
|
use Ramsey\Uuid\Uuid; |
9
|
|
|
use Std\FilterException; |
10
|
|
|
use Zend\Paginator\Paginator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ContactUsService |
14
|
|
|
* |
15
|
|
|
* @package ContactUs\Service |
16
|
|
|
* @author Djordje Stojiljkovic <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ContactUsService |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ContactUsFilter $contactUsFilter |
22
|
|
|
*/ |
23
|
|
|
private $contactUsFilter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ContactUsMapper $contactUsMapper |
27
|
|
|
*/ |
28
|
|
|
private $contactUsMapper; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Paginator $pagination |
32
|
|
|
*/ |
33
|
|
|
private $pagination; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ContactUsService constructor. |
37
|
|
|
* |
38
|
|
|
* @param ContactUsFilter $contactUsFilter |
39
|
|
|
* @param ContactUsMapper $contactUsMapper |
40
|
|
|
* @param Paginator $pagination |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
ContactUsFilter $contactUsFilter, |
44
|
|
|
ContactUsMapper $contactUsMapper, |
45
|
|
|
Paginator $pagination) |
46
|
|
|
{ |
47
|
|
|
$this->contactUsFilter = $contactUsFilter; |
48
|
|
|
$this->contactUsMapper = $contactUsMapper; |
49
|
|
|
$this->pagination = $pagination; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $page |
54
|
|
|
* @param int $limit |
55
|
|
|
* |
56
|
|
|
* @return Paginator |
57
|
|
|
*/ |
58
|
|
|
public function getPagination($page = 1, $limit = 10) |
59
|
|
|
{ |
60
|
|
|
$this->pagination->setCurrentPageNumber($page); |
61
|
|
|
$this->pagination->setItemCountPerPage($limit); |
62
|
|
|
|
63
|
|
|
return $this->pagination; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return all records from db. |
68
|
|
|
* |
69
|
|
|
* @return \Zend\Db\ResultSet\ResultSet |
70
|
|
|
*/ |
71
|
|
|
public function getAll() |
72
|
|
|
{ |
73
|
|
|
return $this->contactUsMapper->select(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param mixed $contactId |
78
|
|
|
* |
79
|
|
|
* @return array|\ArrayObject|null |
80
|
|
|
*/ |
81
|
|
|
public function getById($contactId) |
82
|
|
|
{ |
83
|
|
|
return $this->contactUsMapper->select(['contact_id' => $contactId])->current(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $email |
88
|
|
|
* |
89
|
|
|
* @return array|\ArrayObject|null |
90
|
|
|
*/ |
91
|
|
|
public function getByEmail($email) |
92
|
|
|
{ |
93
|
|
|
return $this->contactUsMapper->select(['email' => (string) $email])->current(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $data |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function create(array $data) |
102
|
|
|
{ |
103
|
|
|
$data = $this->filterData($data); |
104
|
|
|
$data['contact_id'] = Uuid::uuid1()->toString(); |
105
|
|
|
$data['contact_uuid'] = (new \MysqlUuid\Uuid($data['contact_id']))->toFormat(new Binary()); |
106
|
|
|
|
107
|
|
|
return $this->contactUsMapper->insert($data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $data |
112
|
|
|
* @param mixed $contactId |
113
|
|
|
* |
114
|
|
|
* @return int |
115
|
|
|
* |
116
|
|
|
* @throws \Exception |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function update($data, $contactId) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
if (empty($this->getById($contactId))) { |
121
|
|
|
$ex = new \Exception('Contact message object not found. Contact Message ID: ' . $contactId); |
122
|
|
|
throw $ex; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->contactUsMapper->update( |
126
|
|
|
$this->filterData($data), ['contact_id' => $contactId] |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param mixed $contactId |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
* |
135
|
|
|
* @throws \Exception |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function delete($contactId) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
if (empty($this->getById($contactId))) { |
140
|
|
|
$ex = new \Exception('Contact Message with ID: ' .$contactId. ' not found'); |
141
|
|
|
throw $ex; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return (bool) $this->contactUsMapper->delete(['contact_id' => $contactId]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return \Zend\Db\ResultSet\ResultSet |
149
|
|
|
*/ |
150
|
|
|
public function getForSelect() |
151
|
|
|
{ |
152
|
|
|
return $this->contactUsMapper->select(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $data |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
* |
160
|
|
|
* @throws FilterException |
161
|
|
|
*/ |
162
|
|
|
private function filterData($data) |
163
|
|
|
{ |
164
|
|
|
$filter = $this->contactUsFilter->getInputFilter(); |
165
|
|
|
$filter->setData($data); |
166
|
|
|
|
167
|
|
|
if (!$filter->isValid()) { |
168
|
|
|
$ex = new FilterException($filter->getMessages()); |
169
|
|
|
throw $ex; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $filter->getValues(); |
173
|
|
|
} |
174
|
|
|
} |
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.