UrlNormalizer::twitterFixPathSegments()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace mpyw\Cowitter\Helpers;
4
5
class UrlNormalizer
6
{
7
    protected static $specialUrls = [
8
        'i/activity/about_me' =>
9
            'https://api.twitter.com/i/activity/about_me.json',
10
        'i/activity/by_friends' =>
11
            'https://api.twitter.com/i/activity/by_friends.json',
12
        'site' =>
13
            'https://sitestream.twitter.com/1.1/site.json',
14
        'statuses/filter' =>
15
            'https://stream.twitter.com/1.1/statuses/filter.json',
16
        'statuses/firehose' =>
17
            'https://stream.twitter.com/1.1/statuses/firehose.json',
18
        'statuses/sample' =>
19
            'https://stream.twitter.com/1.1/statuses/sample.json',
20
        'media/upload' =>
21
            'https://upload.twitter.com/1.1/media/upload.json',
22
        'user' =>
23
            'https://userstream.twitter.com/1.1/user.json',
24
    ];
25
26
    protected static $versions = ['1.1' => true, '1' => true, 'i' => true];
27
28 23
    protected static function twitterFixPathSegments(array $segments)
29 23
    {
30 23
        if (empty($segments)) {
31 1
            return $segments;
32
        }
33 23
        if (!isset(static::$versions[$segments[0]])) {
34 22
            array_unshift($segments, '1.1');
35
        }
36 23
        if (count($segments) > 1 && !preg_match('/[.:]/', end($segments))) {
37 23
            $segments[] = array_pop($segments) . '.json';
38
        }
39 23
        return $segments;
40
    }
41
42 24
    protected static function twitterBuildUrl(array $e, array $segments)
43 24
    {
44 24
        return (isset($e['scheme']) ? $e['scheme'] : 'https')
45 24
            . '://'
46 24
            . (isset($e['host']) ? $e['host'] : 'api.twitter.com')
47 24
            . (isset($e['port']) ? ':' . $e['port'] : '')
48 24
            . '/'
49 24
            . implode('/', $segments)
50
        ;
51
    }
52
53 46
    public static function twitterSplitUrlAndParameters($endpoint)
54 46
    {
55 46
        if (isset(static::$specialUrls[$endpoint])) {
56 22
            return [static::$specialUrls[$endpoint], []];
57
        }
58 25
        if (false === $e = parse_url($endpoint)) {
59 1
            throw new \DomainException('Invalid URL.');
60
        }
61 24
        $segments = (array) preg_split('@/++@', isset($e['path']) ? $e['path'] : '', -1, PREG_SPLIT_NO_EMPTY);
62 24
        if (!isset($e['host'])) {
63 23
            $segments = static::twitterFixPathSegments($segments);
64
        }
65 24
        parse_str(isset($e['query']) ? $e['query'] : '', $params);
66 24
        return [static::twitterBuildUrl($e, $segments), $params];
67
    }
68
69 9
    protected static function outBuildUrl(array $e, array $segments)
70 9
    {
71 9
        if (!isset($e['host'])) {
72 1
            throw new \DomainException('Invalid URL: Missing host.');
73
        }
74 8
        return (isset($e['scheme']) ? $e['scheme'] : 'https')
75 8
            . '://'
76 8
            . (isset($e['user']) ? $e['user'] . (isset($e['pass']) ? ':' . $e['pass'] : '') . '@' : '')
77 8
            . $e['host']
78 8
            . (isset($e['port']) ? ':' . $e['port'] : '')
79 8
            . '/'
80 8
            . implode('/', $segments)
81
        ;
82
    }
83
84 10
    public static function outSplitUrlAndParameters($endpoint)
85 10
    {
86 10
        if (false === $e = parse_url($endpoint)) {
87 1
            throw new \DomainException('Invalid URL.');
88
        }
89 9
        $segments = (array) preg_split('@/++@', isset($e['path']) ? $e['path'] : '', -1, PREG_SPLIT_NO_EMPTY);
90 9
        parse_str(isset($e['query']) ? $e['query'] : '', $params);
91 9
        return [static::outBuildUrl($e, $segments), $params];
92
    }
93
}
94