Completed
Push — master ( f1b4f8...46f0a1 )
by Ryosuke
03:12
created

UrlNormalizer::twitterFixPathSegments()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 8
nc 5
nop 1
crap 5
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 2
    protected static function twitterFixPathSegments(array $segments)
29 2
    {
30 2
        if (!$segments) {
31 1
            return $segments;
32
        }
33 2
        if (!isset(static::$versions[$segments[0]])) {
34 1
            array_unshift($segments, '1.1');
35
        }
36 2
        if (count($segments) > 1 && !preg_match('/[.:]/', end($segments))) {
37 2
            $segments[] = array_pop($segments) . '.json';
38
        }
39 2
        return $segments;
40
    }
41
42 3
    protected static function twitterBuildUrl(array $e, array $segments)
43 3
    {
44 3
        return (isset($e['scheme']) ? $e['scheme'] : 'https')
45 3
            . '://'
46 3
            . (isset($e['host']) ? $e['host'] : 'api.twitter.com')
47 3
            . (isset($e['port']) ? ':' . $e['port'] : '')
48 3
            . '/'
49 3
            . implode('/', $segments)
50
        ;
51
    }
52
53 4
    public static function twitterSplitUrlAndParameters($endpoint)
54 4
    {
55 4
        if (isset(static::$specialUrls[$endpoint])) {
56 1
            return [static::$specialUrls[$endpoint], []];
57
        }
58 4
        if (false === $e = parse_url($endpoint)) {
59 1
            throw new \DomainException('Invalid URL.');
60
        }
61 3
        $segments = preg_split('@/++@', isset($e['path']) ? $e['path'] : '', -1, PREG_SPLIT_NO_EMPTY);
62 3
        if (!isset($e['host'])) {
63 2
            $segments = static::twitterFixPathSegments($segments);
64
        }
65 3
        parse_str(isset($e['query']) ? $e['query'] : '', $params);
66 3
        return [static::twitterBuildUrl($e, $segments), $params];
67
    }
68
69 3
    protected static function outBuildUrl(array $e, array $segments)
70 3
    {
71 3
        if (!isset($e['host'])) {
72 1
            throw new \DomainException('Invalid URL: Missing host.');
73
        }
74 2
        return (isset($e['scheme']) ? $e['scheme'] : 'https')
75 2
            . '://'
76 2
            . (isset($e['user']) ? $e['user'] . (isset($e['pass']) ? ':' . $e['pass'] : '') . '@' : '')
77 2
            . $e['host']
78 2
            . (isset($e['port']) ? ':' . $e['port'] : '')
79 2
            . '/'
80 2
            . implode('/', $segments)
81
        ;
82
    }
83
84 4
    public static function outSplitUrlAndParameters($endpoint)
85 4
    {
86 4
        if (false === $e = parse_url($endpoint)) {
87 1
            throw new \DomainException('Invalid URL.');
88
        }
89 3
        $segments = preg_split('@/++@', isset($e['path']) ? $e['path'] : '', -1, PREG_SPLIT_NO_EMPTY);
90 3
        parse_str(isset($e['query']) ? $e['query'] : '', $params);
91 3
        return [static::outBuildUrl($e, $segments), $params];
92
    }
93
}
94