Completed
Push — master ( 5d8ce5...74f5f8 )
by Paweł
03:08
created

Endpoint   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 34
c 1
b 0
f 0
dl 0
loc 71
rs 10

6 Methods

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