Passed
Push — master ( 1eae8d...41733b )
by Paweł
08:28
created

Endpoint::accountInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * User: jakim <[email protected]>
4
 * Date: 15.10.2017
5
 */
6
7
namespace jakim\ig;
8
9
10
class Endpoint
11
{
12
    public static $baseUrl = 'https://www.instagram.com';
13
    public static $accountMediaQueryHash = '472f257a40c653c64c666ce877d59d2b';
14
    public static $tagMediaQueryHash = '298b92c8d7cad703f7565aa892ede943';
15
16
    public static function accountInfo($accountId, array $params = []): string
17
    {
18
        $params['id'] = $accountId;
19
20
        return static::createUrl('https://i.instagram.com/api/v1/users/{id}/info', $params);
21
    }
22
23
    public static function accountDetails($username, array $params = []): string
24
    {
25
        $params['username'] = $username;
26
        $params['__a'] = 1;
27
28
        return static::createUrl('/{username}/', $params);
29
    }
30
31
    public static function accountMedia($accountId, int $first = 12, array $params = []): string
32
    {
33
        $params['query_hash'] = static::$accountMediaQueryHash;
34
        $params['variables']['id'] = $accountId;
35
        $params['variables']['first'] = $first;
36
37
        return static::createUrl('/graphql/query/', $params);
38
    }
39
40
    public static function mediaDetails($code, array $params = []): string
41
    {
42
        $params['code'] = $code;
43
        $params['__a'] = 1;
44
45
        return static::createUrl('/p/{code}/', $params);
46
    }
47
48
    public static function exploreLocations($id, array $params = []): string
49
    {
50
        $params['id'] = $id;
51
        $params['__a'] = 1;
52
53
        return static::createUrl('/explore/locations/{id}/', $params);
54
    }
55
56
    public static function exploreTags($tag, array $params = []): string
57
    {
58
        $params['tag'] = $tag;
59
        $params['__a'] = 1;
60
61
        return static::createUrl('/explore/tags/{tag}/', $params);
62
    }
63
64
    public static function tagMedia($tag, int $first = 8, array $params = [])
65
    {
66
        $params['query_hash'] = static::$tagMediaQueryHash;
67
        $params['variables']['tag_name'] = $tag;
68
        $params['variables']['first'] = $first;
69
70
        return static::createUrl('/graphql/query/', $params);
71
    }
72
73
    public static function createUrl($endpoint, array $params = []): string
74
    {
75
        preg_match_all('/{(.+?)}/', $endpoint, $matches);
76
        $placeholders = array_combine($matches['0'], $matches['1']);
77
78
        array_walk($params, function ($value, $key) use (&$params) {
79
            $params[$key] = is_array($value) ? json_encode($value) : $value;
80
        });
81
82
        foreach ($placeholders as $placeholder => $key) {
83
            if (array_key_exists($key, $params)) {
84
                $value = $params[$key];
85
                unset($params[$key]);
86
                $endpoint = str_replace($placeholder, $value, $endpoint);
87
            }
88
        }
89
90
        $query = http_build_query($params);
91
        $query = $query ? "?{$query}" : '';
92
        $baseUrl = substr($endpoint, 0, 4) !== 'http' ? static::$baseUrl : '';
93
94
        return $baseUrl . $endpoint . $query;
95
    }
96
}