1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Marek\Toggable\Service; |
4
|
|
|
|
5
|
|
|
use Marek\Toggable\API\Exception\NotFoundException; |
6
|
|
|
use Marek\Toggable\API\Http\Response\Response; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractService |
11
|
|
|
* @package Marek\Toggable\Service |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Marek\Toggable\Http\Manager\RequestManagerInterface |
17
|
|
|
*/ |
18
|
|
|
protected $requestManager; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Marek\Toggable\Hydrator\HydratorInterface |
22
|
|
|
*/ |
23
|
|
|
protected $hydrator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AbstractService constructor. |
27
|
|
|
* |
28
|
|
|
* @param \Marek\Toggable\Http\Manager\RequestManagerInterface $requestManager |
29
|
|
|
* @param \Marek\Toggable\Hydrator\HydratorInterface $hydrator |
30
|
|
|
*/ |
31
|
99 |
|
public function __construct( |
32
|
|
|
\Marek\Toggable\Http\Manager\RequestManagerInterface $requestManager, |
33
|
|
|
\Marek\Toggable\Hydrator\HydratorInterface $hydrator |
34
|
|
|
) |
35
|
|
|
{ |
36
|
99 |
|
$this->requestManager = $requestManager; |
37
|
99 |
|
$this->hydrator = $hydrator; |
38
|
99 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Extract helper method |
42
|
|
|
* |
43
|
|
|
* @param object $object |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
20 |
|
protected function extractDataFromObject($object) |
48
|
|
|
{ |
49
|
20 |
|
return $this->hydrator->extract($object); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Hydrate helper method |
54
|
|
|
* |
55
|
|
|
* @param \Marek\Toggable\API\Http\Response\ResponseInterface $response |
56
|
|
|
* @param object $object |
57
|
|
|
* |
58
|
|
|
* @return object |
59
|
|
|
*/ |
60
|
23 |
|
protected function hydrateDataFromArrayToObject(\Marek\Toggable\API\Http\Response\ResponseInterface $response, $object) |
61
|
|
|
{ |
62
|
23 |
|
$data = $response->getBody()['data']; |
63
|
|
|
|
64
|
23 |
|
return $this->hydrator->hydrate($data, $object); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Delegates request/response management |
69
|
|
|
* |
70
|
|
|
* @param \Marek\Toggable\API\Http\Request\RequestInterface $request |
71
|
|
|
* |
72
|
|
|
* @return \Marek\Toggable\API\Http\Response\Response |
73
|
|
|
*/ |
74
|
53 |
|
protected function delegate(\Marek\Toggable\API\Http\Request\RequestInterface $request) |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
|
78
|
53 |
|
$response = $this->requestManager->request($request); |
79
|
|
|
|
80
|
53 |
|
} catch(NotFoundException $e) { |
81
|
|
|
|
82
|
|
|
return new Response(); |
83
|
|
|
} |
84
|
|
|
|
85
|
53 |
|
return $response; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Response helper method |
90
|
|
|
* |
91
|
|
|
* @param \Marek\Toggable\API\Http\Request\RequestInterface $request |
92
|
|
|
* |
93
|
|
|
* @return \Marek\Toggable\API\Toggl\Values\ValueObject |
94
|
|
|
*/ |
95
|
|
|
protected function delegateHydrateAndReturnResponse(\Marek\Toggable\API\Http\Request\RequestInterface $request) |
96
|
|
|
{ |
97
|
|
|
throw new \RuntimeException('Method not implented'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Validates input |
102
|
|
|
* |
103
|
|
|
* @param mixed $id |
104
|
|
|
* |
105
|
|
|
* @return mixed |
106
|
|
|
* |
107
|
|
|
* @throws InvalidArgumentException |
108
|
|
|
*/ |
109
|
61 |
|
protected function validate($id) |
110
|
|
|
{ |
111
|
61 |
|
if (empty($id) || !is_int($id)) { |
112
|
30 |
|
throw new InvalidArgumentException( |
113
|
30 |
|
sprintf('$id argument not provided in %s', get_class($this)) |
114
|
30 |
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
31 |
|
return $id; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|