Passed
Push — master ( 140a6e...efb89e )
by Kane
10:50
created

ImportProviders::generateProviders()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 66
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 66
rs 7.8682
cc 8
nc 10
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Cohensive\OEmbed;
3
4
use \Exception;
5
6
class ImportProviders
7
{
8
    public function generateProviders()
9
    {
10
        $response = file_get_contents('https://oembed.com/providers.json');
11
12
        if (!$response) {
13
            throw new Exception('Error reteriving providers list.');
14
        }
15
16
        $providers = json_decode($response, true);
17
18
        $providerFormat = <<<END
19
        [
20
            'provider_name' => '%s',
21
            'provider_url' => '%s',
22
            'endpoints' => [
23
                %s
24
            ]
25
        ],
26
27
        END;
28
29
        $endpointFormat = <<<END
30
        [
31
                    'schemes' => [
32
                %s
33
                    ],
34
                    'url' => '%s',
35
                ],
36
        END;
37
38
        $schemeFormat = <<<END
39
                '%s',
40
        END;
41
42
        $formattedProviders = '';
43
        foreach ($providers as $provider) {
44
            $endpoints = '';
45
            foreach ($provider['endpoints'] as $ekey => $endpoint) {
46
                if (!isset($endpoint['schemes'])) {
47
                    continue;
48
                }
49
50
                $schemes = '';
51
                $url = str_replace('.{format}', '.json', $endpoint['url']);
52
                $url = str_replace('?format={format}', '?format=json', $url);
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
53
                foreach ($endpoint['schemes'] as $skey => $url) {
54
                    $scheme = '|^' . $url;
55
                    $scheme = preg_replace('/([.=?#-])/i', '\\\\\\\\${1}', $scheme);
56
                    $scheme = str_replace('http:', 'https?:', $scheme);
57
                    $scheme = str_replace('https:', 'https?:', $scheme);
58
                    $scheme = str_replace('*', '.*', $scheme);
59
                    $scheme .= '$|i';
60
                    $schemes .= ($skey > 0 ? "\n        " : "") . sprintf($schemeFormat, $scheme);
61
                }
62
                $endpoints .= ($ekey > 0 ? "\n        " : "") . sprintf($endpointFormat, $schemes, $endpoint['url']);
63
            }
64
65
            $formattedProviders .= sprintf(
66
                $providerFormat,
67
                $provider['provider_name'],
68
                $provider['provider_url'],
69
                $endpoints
70
            );
71
        }
72
73
        file_put_contents('providers.txt', $formattedProviders);
74
    }
75
76
    public function generateSimpleProviders()
77
    {
78
        $response = file_get_contents('https://oembed.com/providers.json');
79
80
        if (!$response) {
81
            throw new Exception('Error reteriving providers list.');
82
        }
83
84
        $providers = json_decode($response, true);
85
86
        $providerFormat = <<<END
87
        '%s' => [
88
            'schemes' => [
89
                %s
90
            ]
91
        ],
92
93
        END;
94
95
        $schemeFormat = <<<END
96
        '%s',
97
        END;
98
99
        $formattedProviders = '';
100
        foreach ($providers as $provider) {
101
            foreach ($provider['endpoints'] as $endpoint) {
102
                if (!isset($endpoint['schemes'])) {
103
                    continue;
104
                }
105
106
                $schemes = '';
107
                $url = str_replace('.{format}', '.json', $endpoint['url']);
108
                $url = str_replace('?format={format}', '?format=json', $url);
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
109
                foreach ($endpoint['schemes'] as $skey => $url) {
110
                    $scheme = '|^' . $url;
111
                    $scheme = preg_replace('/([.=?#-])/i', '\\\\\\\\${1}', $scheme);
112
                    $scheme = str_replace('http:', 'https?:', $scheme);
113
                    $scheme = str_replace('https:', 'https?:', $scheme);
114
                    $scheme = str_replace('*', '.*', $scheme);
115
                    $scheme .= '$|i';
116
                    $schemes .= ($skey > 0 ? "\n        " : "") . sprintf($schemeFormat, $scheme);
117
                }
118
119
                $formattedProviders .= sprintf(
120
                    $providerFormat,
121
                    $endpoint['url'],
122
                    $schemes
123
                );
124
            }
125
        }
126
127
        file_put_contents('simple_providers.txt', $formattedProviders);
128
    }
129
}
130
131
(new ImportProviders)->generateProviders();
132
(new ImportProviders)->generateSimpleProviders();
133