Passed
Branch master (3e058b)
by Allan
03:49 queued 01:54
created

UrlNormalizer::renderValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerChangelogs\Normalizers;
7
8
class UrlNormalizer
9
{
10
    public function assureHttpAccessibility($url)
11
    {
12
        $urlComponents = parse_url(
13
            trim($url)
14
        );
15
16
        if (!isset($urlComponents['host'])) {
17
            return '';
18
        }
19
20
        if (!isset($urlComponents['scheme'])) {
21
            $urlComponents['scheme'] = 'https';
22
        }
23
24
        if ($urlComponents['scheme'] === 'ssh') {
25
            unset($urlComponents['user']);
26
            unset($urlComponents['pass']);
27
28
            $urlComponents['scheme'] = 'https';
29
        }
30
31
        $urlComponents['path'] = strtok($urlComponents['path'], '.');
32
33
        return
34
            $this->renderValue($urlComponents, 'scheme', '%s:') .
35
            ((isset($urlComponents['user']) || isset($urlComponents['host'])) ? '//' : '') .
36
            $this->renderValue($urlComponents, 'user', '%s') .
37
            $this->renderValue($urlComponents, 'pass', ':%s') .
38
            (isset($urlComponents['user']) ? '@' : '') .
39
            $this->renderValue($urlComponents, 'host', '%s') .
40
            $this->renderValue($urlComponents, 'port', ':%s') .
41
            $this->renderValue($urlComponents, 'path', '%s') .
42
            $this->renderValue($urlComponents, 'query', '?%s') .
43
            $this->renderValue($urlComponents, 'fragment', '#%s');
44
    }
45
    
46
    private function renderValue($data, $key, $format, $default = '')
47
    {
48
        if (!isset($data[$key])) {
49
            return $default;
50
        }
51
52
        return sprintf($format, $data[$key]);
53
    }
54
}
55