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

UrlNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 27
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeComponentValues() 0 16 3
A __construct() 0 3 1
A assureHttpAccessibility() 0 23 2
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