Completed
Push — master ( 54f12d...4e1d72 )
by Paweł
02:18
created

Endpoint   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 76
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A mediaDetails() 0 6 1
A tagMedia() 0 7 1
A exploreTags() 0 6 1
A accountDetails() 0 6 1
B createUrl() 0 20 5
A exploreLocations() 0 6 1
A accountMedia() 0 7 1
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 accountDetails($username, array $params = []): string
17
    {
18
        $params['username'] = $username;
19
        $params['__a'] = 1;
20
21
        return static::createUrl('/{username}/', $params);
22
    }
23
24
    public static function accountMedia($accountId, int $first = 12, array $params = []): string
25
    {
26
        $params['query_hash'] = static::$accountMediaQueryHash;
27
        $params['variables']['id'] = $accountId;
28
        $params['variables']['first'] = $first;
29
30
        return static::createUrl('/graphql/query/', $params);
31
    }
32
33
    public static function mediaDetails($code, array $params = []): string
34
    {
35
        $params['code'] = $code;
36
        $params['__a'] = 1;
37
38
        return static::createUrl('/p/{code}/', $params);
39
    }
40
41
    public static function exploreLocations($id, array $params = []): string
42
    {
43
        $params['id'] = $id;
44
        $params['__a'] = 1;
45
46
        return static::createUrl('/explore/locations/{id}/', $params);
47
    }
48
49
    public static function exploreTags($tag, array $params = []): string
50
    {
51
        $params['tag'] = $tag;
52
        $params['__a'] = 1;
53
54
        return static::createUrl('/explore/tags/{tag}/', $params);
55
    }
56
57
    public static function tagMedia($tag, int $first = 8, array $params = [])
58
    {
59
        $params['query_hash'] = static::$tagMediaQueryHash;
60
        $params['variables']['tag_name'] = $tag;
61
        $params['variables']['first'] = $first;
62
63
        return static::createUrl('/graphql/query/', $params);
64
    }
65
66
    public static function createUrl($endpoint, array $params = []): string
67
    {
68
        preg_match_all('/{(.+?)}/', $endpoint, $matches);
69
        $placeholders = array_combine($matches['0'], $matches['1']);
70
71
        array_walk($params, function ($value, $key) use (&$params) {
72
            $params[$key] = is_array($value) ? json_encode($value) : $value;
73
        });
74
75
        foreach ($placeholders as $placeholder => $key) {
76
            if (array_key_exists($key, $params)) {
77
                $value = $params[$key];
78
                unset($params[$key]);
79
                $endpoint = str_replace($placeholder, $value, $endpoint);
80
            }
81
        }
82
83
        $query = http_build_query($params);
84
85
        return static::$baseUrl . $endpoint . ($query ? "?{$query}" : '');
86
    }
87
}