Completed
Push — master ( c20ba1...70ca3b )
by Mario
03:39
created

AbstractService::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
crap 3
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 49
    public function __construct(
32
        \Marek\Toggable\Http\Manager\RequestManagerInterface $requestManager,
33
        \Marek\Toggable\Hydrator\HydratorInterface $hydrator
34
    )
35
    {
36 49
        $this->requestManager = $requestManager;
37 49
        $this->hydrator = $hydrator;
38 49
    }
39
40
    /**
41
     * Extract helper method
42
     *
43
     * @param object $object
44
     *
45
     * @return array
46
     */
47 9
    protected function extractDataFromObject($object)
48
    {
49 9
        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 14
    protected function hydrateDataFromArrayToObject(\Marek\Toggable\API\Http\Response\ResponseInterface $response, $object)
61
    {
62 14
        $data = $response->getBody()['data'];
63
64 14
        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 23
    protected function delegate(\Marek\Toggable\API\Http\Request\RequestInterface $request)
75
    {
76
        try {
77
78 23
            $response = $this->requestManager->request($request);
79
80 23
        } catch(NotFoundException $e) {
81
82
            return new Response();
83
        }
84
85 23
        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 25
    protected function validate($id)
110
    {
111 25
        if (empty($id) || !is_int($id)) {
112 12
            throw new InvalidArgumentException(
113 12
                sprintf('$id argument not provided in %s', get_class($this))
114 12
            );
115
        }
116
117 13
        return $id;
118
    }
119
}
120