Passed
Push — master ( d5c5e0...724c44 )
by Allan
02:09
created

UrlNormalizer::normalizeComponentValues()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
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
    /**
11
     * @var \Vaimo\ComposerChangelogs\Utils\DataUtils 
12
     */
13
    private $dataUtils;
14
    
15
    public function __construct() 
16
    {
17
        $this->dataUtils = new \Vaimo\ComposerChangelogs\Utils\DataUtils();
18
    }
19
20
    public function assureHttpAccessibility($url)
21
    {
22
        $components = parse_url(
23
            trim($url)
24
        );
25
26
        if (!isset($components['host'])) {
27
            return '';
28
        }
29
30
        $components = $this->normalizeComponentValues($components);
31
        
32
        return
33
            $this->dataUtils->renderValue($components, 'scheme', '%s:') .
34
            $this->dataUtils->renderConstant($components, array('user', 'host'), '//') .
35
            $this->dataUtils->renderValue($components, 'user', '%s') .
36
            $this->dataUtils->renderValue($components, 'pass', ':%s') .
37
            $this->dataUtils->renderConstant($components, array('user'), '@') .
38
            $this->dataUtils->renderValue($components, 'host', '%s') .
39
            $this->dataUtils->renderValue($components, 'port', ':%s') .
40
            $this->dataUtils->renderValue($components, 'path', '%s') .
41
            $this->dataUtils->renderValue($components, 'query', '?%s') .
42
            $this->dataUtils->renderValue($components, 'fragment', '#%s');
43
    }
44
    
45
    private function normalizeComponentValues(array $components)
46
    {
47
        if (!isset($components['scheme'])) {
48
            $components['scheme'] = 'https';
49
        }
50
51
        if ($components['scheme'] === 'ssh') {
52
            unset($components['user']);
53
            unset($components['pass']);
54
55
            $components['scheme'] = 'https';
56
        }
57
58
        $components['path'] = strtok($components['path'], '.');
59
        
60
        return $components;
61
    }
62
}
63