Passed
Push — master ( ac7426...a8ebef )
by Sebastian
01:33
created

UriTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkStandardPortForCurretScheme() 0 3 3
A getNonStandardPort() 0 8 3
A createUriString() 0 15 4
A getPortForStandardScheme() 0 3 2
1
<?php
2
3
/**
4
 * Linna PSR7
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 *
10
 */
11
12
namespace Linna\Psr7;
13
14
/**
15
 * Uri trait.
16
 * Provide help methods for Uri class.
17
 */
18
trait UriTrait
19
{
20
    /**
21
     * Create uri string.
22
     * 
23
     * @param string $scheme
24
     * @param string $authority
25
     * @param string $path
26
     * @param string $query
27
     * @param string $fragment
28
     * 
29
     * @return string
30
     */
31 19
    private function createUriString(
32
            string $scheme, 
33
            string $authority, 
34
            string $path, 
35
            string $query, 
36
            string $fragment
37
            ) : string
38
    {
39 19
        $uri = $scheme.$authority;
40
        
41 19
        $uri .= ('/' !== substr($path, 0, 1) && $uri !== '' && $path !== '') ? '/'.$path : $path;
42
43 19
        $uri .= $query.$fragment;
44
        
45 19
        return $uri;
46
    }
47
    
48
    /**
49
     * Get non standard port.
50
     * 
51
     * @param int $port
52
     * @param string $scheme
53
     * @param string $standardScheme
54
     * @param array $supportedSchemes
55
     * 
56
     * @return int
57
     */
58 7
    private function getNonStandardPort(
59
            int $port, 
60
            string $scheme, 
61
            string $standardScheme, 
62
            array $supportedSchemes
63
            ) : int
64
    {
65 7
        return (!$port && $standardScheme) ? $supportedSchemes[$scheme] : $port;
66
    }
67
    
68
    /**
69
     * Get port for standard scheme.
70
     * 
71
     * @param int $standardPort
72
     * @param int $port
73
     * 
74
     * @return int
75
     */
76 1
    private function getPortForStandardScheme(int $standardPort, int $port) : int
77
    {
78 1
        return ($standardPort) ? 0 : $port;
79
    }
80
    
81
    /**
82
     * Check standard port for current scheme.
83
     * 
84
     * @param string $scheme
85
     * @param int $port
86
     * @param array $supportedSchemes
87
     * 
88
     * @return bool
89
     */
90 8
    private function checkStandardPortForCurretScheme(string $scheme, int $port, array $supportedSchemes) : bool
91
    {
92 8
        return (isset($supportedSchemes[$scheme]) && $port === $supportedSchemes[$scheme]) ? true : false;
93
    }
94
}
95