AccountQuery   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
eloc 26
c 2
b 0
f 1
dl 0
loc 73
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findLatestMedia() 0 5 1
A findOneByUsername() 0 5 1
A findOneById() 0 11 1
A __construct() 0 11 1
A fetchContentAsArray() 0 16 5
1
<?php
2
/**
3
 * Created for IG Client.
4
 * User: jakim <[email protected]>
5
 * Date: 14.03.2018
6
 */
7
8
namespace Jakim\Query;
9
10
11
use Jakim\Base\Query;
12
use Jakim\Exception\LoginAndSignupPageException;
13
use Jakim\Exception\RestrictedProfileException;
14
use Jakim\Helper\JsonHelper;
15
use jakim\ig\Endpoint;
16
use jakim\ig\Url;
17
use Jakim\Mapper\AccountDetails;
18
use Jakim\Mapper\AccountInfo;
19
use Jakim\Mapper\EdgeMedia;
20
use Jakim\Model\Account;
21
use Jakim\Model\MediaCollection;
22
23
class AccountQuery extends Query
24
{
25
    /**
26
     * @var \Jakim\Base\Mapper|\Jakim\Mapper\AccountDetails
27
     */
28
    protected $findOneByUsername;
29
30
    /**
31
     * @var \Jakim\Base\Mapper|\Jakim\Mapper\AccountInfo
32
     */
33
    protected $findOneByOne;
34
35
    /**
36
     * @var \Jakim\Base\Mapper|\Jakim\Mapper\EdgeMedia
37
     */
38
    protected $findLatestMedia;
39
40
    public function __construct(
41
        $httpClient,
42
        AccountDetails $findOneByUsername = null,
43
        AccountInfo $findOneById = null,
44
        EdgeMedia $findLatestMedia = null
45
    )
46
    {
47
        parent::__construct($httpClient);
48
        $this->findOneByUsername = $findOneByUsername;
49
        $this->findOneByOne = $findOneById;
50
        $this->findLatestMedia = $findLatestMedia;
51
    }
52
53
    public function findOneByUsername(string $username): Account
54
    {
55
        $url = Url::account($username);
56
57
        return $this->createResult($url, $this->findOneByUsername, false);
58
    }
59
60
    public function findOneById($accountId): Account
61
    {
62
        $url = Endpoint::accountInfo($accountId);
63
        $content = parent::fetchContentAsArray($url); // from api, not sharedData :)
64
65
        $this->throwEmptyContentExceptionIfEmpty($content);
66
67
        $config = $this->findOneByOne->config();
68
        $data = $this->findOneByOne->getData($content, $config);
69
70
        return $this->findOneByOne->createModel($data, $config);
71
    }
72
73
    public function findLatestMedia(string $username, bool $relations = false): MediaCollection
74
    {
75
        $url = Url::account($username);
76
77
        return $this->createResult($url, $this->findLatestMedia, $relations);
78
    }
79
80
    protected function fetchContentAsArray(string $url): ?array
81
    {
82
        $res = $this->httpClient->get($url);
83
        $content = $res->getBody()->getContents();
84
85
        preg_match('/\_sharedData \= (.*?)\;\<\/script\>/s', $content, $matches);
86
87
        if (isset($matches['1']) && strpos($matches['1'], 'LoginAndSignupPage') !== false) {
88
            throw new LoginAndSignupPageException();
89
        }
90
91
        if (empty($matches) && strpos($content, 'Restricted profile') !== false) {
92
            throw new RestrictedProfileException();
93
        }
94
95
        return JsonHelper::decode($matches['1']);
96
    }
97
}