Passed
Push — master ( 6d0788...5601f8 )
by Solo
02:15
created

UrlNormalizer::twitterSplitUrlAndParameters()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 4
nop 1
dl 0
loc 14
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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 = preg_split('@/++@', isset($e['path']) ? $e['path'] : '', -1, PREG_SPLIT_NO_EMPTY);
62 24
        if (!isset($e['host'])) {
63 23
            $segments = static::twitterFixPathSegments($segments);
0 ignored issues
show
Bug introduced by
It seems like $segments can also be of type false; however, parameter $segments of mpyw\Cowitter\Helpers\Ur...witterFixPathSegments() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
            $segments = static::twitterFixPathSegments(/** @scrutinizer ignore-type */ $segments);
Loading history...
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 = 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];
0 ignored issues
show
Bug introduced by
It seems like $segments can also be of type false; however, parameter $segments of mpyw\Cowitter\Helpers\UrlNormalizer::outBuildUrl() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        return [static::outBuildUrl($e, /** @scrutinizer ignore-type */ $segments), $params];
Loading history...
92
    }
93
}
94