Completed
Push — master ( 6c0f1a...ae9b29 )
by Mario
02:55
created

AbstractService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 80.95%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 9
c 4
b 0
f 0
lcom 2
cbo 4
dl 0
loc 107
ccs 17
cts 21
cp 0.8095
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A extractDataFromObject() 0 4 1
A hydrateDataFromArrayToObject() 0 6 1
A delegateHydrateAndReturnResponse() 0 4 1
A delegate() 0 13 2
A validate() 0 10 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 88
    public function __construct(
32
        \Marek\Toggable\Http\Manager\RequestManagerInterface $requestManager,
33
        \Marek\Toggable\Hydrator\HydratorInterface $hydrator
34
    )
35
    {
36 88
        $this->requestManager = $requestManager;
37 88
        $this->hydrator = $hydrator;
38 88
    }
39
40
    /**
41
     * Extract helper method
42
     *
43
     * @param object $object
44
     *
45
     * @return array
46
     */
47 17
    protected function extractDataFromObject($object)
48
    {
49 17
        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 20
    protected function hydrateDataFromArrayToObject(\Marek\Toggable\API\Http\Response\ResponseInterface $response, $object)
61
    {
62 20
        $data = $response->getBody()['data'];
63
64 20
        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 47
    protected function delegate(\Marek\Toggable\API\Http\Request\RequestInterface $request)
75
    {
76
        try {
77
78 47
            $response = $this->requestManager->request($request);
79
80
        } catch(NotFoundException $e) {
81
82
            return new Response();
83
        }
84
85 47
        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 55
    protected function validate($id)
110
    {
111 55
        if (empty($id) || !is_int($id)) {
112 27
            throw new InvalidArgumentException(
113 27
                sprintf('$id argument not provided in %s', get_class($this))
114
            );
115
        }
116
117 28
        return $id;
118
    }
119
}
120