1 | <?php |
||
12 | abstract class BaseService implements ServiceInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var RequestHandler |
||
16 | */ |
||
17 | protected $request; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $fields = []; |
||
23 | |||
24 | /** |
||
25 | * @var mixed |
||
26 | */ |
||
27 | protected $response; |
||
28 | |||
29 | /** |
||
30 | * @var array EntityInterface |
||
31 | */ |
||
32 | protected $entities = []; |
||
33 | |||
34 | /** |
||
35 | * LeadService constructor. |
||
36 | * @param RequestHandler $requestHandler |
||
37 | */ |
||
38 | 37 | public function __construct(RequestHandler $requestHandler) |
|
42 | |||
43 | /** |
||
44 | * @return bool|mixed |
||
45 | */ |
||
46 | 29 | public function save() |
|
47 | { |
||
48 | 29 | $this->composeFields(); |
|
49 | 29 | $this->request->performRequest($this->getLink(), $this->fields); |
|
50 | 29 | $this->response = $this->request->getResponse(); |
|
51 | |||
52 | 29 | if ($this->checkResponse()) { |
|
53 | 21 | return $this->getResponse(); |
|
54 | } |
||
55 | |||
56 | 8 | return false; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return mixed |
||
61 | */ |
||
62 | 21 | public function getResponse() |
|
66 | |||
67 | /** |
||
68 | * @return array |
||
69 | */ |
||
70 | 16 | public function getEntities() |
|
74 | |||
75 | /** |
||
76 | * @return array|bool |
||
77 | */ |
||
78 | 29 | public function parseResponseToEntities() |
|
79 | { |
||
80 | 29 | if (!$this->checkResponse()) { |
|
81 | 8 | return false; |
|
82 | } |
||
83 | 21 | $this->entities = []; |
|
84 | |||
85 | 21 | foreach ($this->response['_embedded']['items'] as $item) { |
|
86 | 21 | $this->entities[] = $this->parseArrayToEntity($item); |
|
87 | 21 | } |
|
88 | |||
89 | 21 | return $this->entities; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param $array |
||
94 | * @return EntityInterface |
||
95 | */ |
||
96 | abstract public function parseArrayToEntity($array); |
||
97 | |||
98 | /** |
||
99 | * @return bool |
||
100 | */ |
||
101 | 29 | protected function checkResponse() |
|
109 | |||
110 | /** |
||
111 | * Fill fields for save request |
||
112 | */ |
||
113 | 29 | protected function composeFields() |
|
134 | |||
135 | /** |
||
136 | * @return string |
||
137 | */ |
||
138 | abstract protected function getLink(); |
||
139 | } |