Completed
Push — master ( 4fd434...e1a522 )
by Bohuslav
03:35
created

UriFactory::parseUrl()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 6.4615
cc 9
eloc 11
nc 256
nop 1
crap 9
1
<?php
2
namespace Kambo\Http\Message\Factories\String;
3
4
// \Http\Message
5
use Kambo\Http\Message\Uri;
6
7
/**
8
 * Create instances of Uri object from the string
9
 *
10
 * @package Kambo\Http\Message\Factories\String
11
 * @author  Bohuslav Simek <[email protected]>
12
 * @license MIT
13
 */
14
class UriFactory
15
{
16
    /**
17
     * Create new Uri from provided string.
18
     *
19
     * @param string $uri uri that will be parsed into URI object
20
     *
21
     * @return Uri Instance of Uri based on provided string
22
     */
23 12
    public function create($uri)
24
    {
25 12
        list($scheme, $host, $port, $path, $query, $fragment, $user, $pass) = $this->parseUrl($uri);
26
27 12
        return new Uri($scheme, $host, $port, $path, $query, $fragment, $user, $pass);
28
    }
29
30
    /**
31
     * Parse uri to array with individual parts
32
     *
33
     * @param string $uri url that will be parsed into array with individual parts
34
     *
35
     * @return Array Individual parts of uri in array
36
     */
37 12
    protected function parseUrl($uri)
38
    {
39 12
        $parsedUrl = parse_url($uri);
40
41
        return [
42 12
            isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] : '',
43 12
            isset($parsedUrl['host']) ? $parsedUrl['host'] : '',
44 12
            isset($parsedUrl['port']) ? $parsedUrl['port'] : null,
45 12
            isset($parsedUrl['path']) ? $parsedUrl['path'] : '/',
46 12
            isset($parsedUrl['query']) ? $parsedUrl['query'] : '',
47 12
            isset($parsedUrl['fragment']) ? $parsedUrl['fragment'] : '',
48 12
            isset($parsedUrl['user']) ? $parsedUrl['user'] : '',
49 12
            isset($parsedUrl['pass']) ? $parsedUrl['pass'] : ''
50 12
        ];
51
    }
52
}
53