Query   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 47
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchContentAsArray() 0 6 1
A __construct() 0 3 1
A throwEmptyContentExceptionIfEmpty() 0 4 2
A createResult() 0 10 1
1
<?php
2
/**
3
 * Created for IG Client.
4
 * User: jakim <[email protected]>
5
 * Date: 16.03.2018
6
 */
7
8
namespace Jakim\Base;
9
10
11
use Jakim\Exception\EmptyContentException;
12
use Jakim\Helper\JsonHelper;
13
14
abstract class Query
15
{
16
    /**
17
     * Psr7 compatible client.
18
     *
19
     * @var \GuzzleHttp\Client
20
     */
21
    protected $httpClient;
22
23
    public function __construct($httpClient)
24
    {
25
        $this->httpClient = $httpClient;
26
    }
27
28
    protected function fetchContentAsArray(string $url): ?array
29
    {
30
        $res = $this->httpClient->get($url);
31
        $content = $res->getBody()->getContents();
32
33
        return JsonHelper::decode($content);
34
    }
35
36
    /**
37
     * @param string $url
38
     * @param \Jakim\Base\Mapper $mapper
39
     * @param bool $relations
40
     * @return mixed
41
     * @throws \Jakim\Exception\EmptyContentException
42
     * @throws \Jakim\Exception\LoginAndSignupPageException
43
     * @throws \Jakim\Exception\RestrictedProfileException
44
     */
45
    protected function createResult(string $url, Mapper $mapper, bool $relations)
46
    {
47
        $content = $this->fetchContentAsArray($url);
48
49
        $this->throwEmptyContentExceptionIfEmpty($content);
50
51
        $config = $mapper->config();
52
        $data = $mapper->getData($content, $config);
53
54
        return $mapper->createModel($data, $config, $relations);
55
    }
56
57
    protected function throwEmptyContentExceptionIfEmpty($content)
58
    {
59
        if (empty($content)) {
60
            throw new EmptyContentException();
61
        }
62
    }
63
}