Completed
Push — master ( f4a000...5b66b0 )
by Mario
10:06
created

Base::getResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Marek\OpenWeatherMap\Core\Weather;
4
5
use Marek\OpenWeatherMap\API\Cache\HandlerInterface;
6
use Marek\OpenWeatherMap\API\Exception\BadRequestException;
7
use Marek\OpenWeatherMap\API\Exception\ExceptionThrower;
8
use Marek\OpenWeatherMap\API\Exception\ForbiddenException;
9
use Marek\OpenWeatherMap\API\Exception\NotFoundException;
10
use Marek\OpenWeatherMap\API\Exception\APIException;
11
use Marek\OpenWeatherMap\Factory\UrlFactory;
12
use Marek\OpenWeatherMap\Http\Client\HttpClientInterface;
13
use Marek\OpenWeatherMap\Hydrator\HydratorInterface;
14
15
abstract class Base
16
{
17
    /**
18
     * @var HttpClientInterface
19
     */
20
    protected $client;
21
22
    /**
23
     * @var UrlFactory
24
     */
25
    protected $factory;
26
27
    /**
28
     * @var HandlerInterface
29
     */
30
    protected $handler;
31
32
    /**
33
     * @var HydratorInterface
34
     */
35
    protected $hydrator;
36
37
    /**
38
     * Base constructor.
39
     *
40
     * @param HttpClientInterface $client
41
     * @param UrlFactory $factory
42
     * @param HandlerInterface $handler
43
     * @param HydratorInterface $hydrator
44
     */
45
    public function __construct(HttpClientInterface $client, UrlFactory $factory, HandlerInterface $handler, HydratorInterface $hydrator)
46
    {
47
        $this->client = $client;
48
        $this->factory = $factory;
49
        $this->handler = $handler;
50
        $this->hydrator = $hydrator;
51
    }
52
53
    /**
54
     * @param string $url
55
     *
56
     * @return string
57
     *
58
     * @throws APIException
59
     */
60
    protected function getResult($url)
61
    {
62
        $hash = md5($url);
63
64
        if ($this->handler->has($hash)) {
65
            return $this->handler->get($hash);
66
        }
67
68
        $response = $this->client->get($url);
69
70
        ExceptionThrower::throwException($response->getStatusCode(), $response->getMessage());
71
72
        return (string)$response;
73
    }
74
}
75