Completed
Pull Request — master (#2)
by René
04:42
created

Request   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 9
c 5
b 0
f 0
lcom 1
cbo 0
dl 0
loc 87
rs 10
ccs 20
cts 20
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPath() 0 10 2
A getPost() 0 4 1
A getCookie() 0 4 1
A createUrlFromServerArray() 0 10 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace Zortje\MVC\Network;
5
6
use Zortje\MVC\Storage\Cookie\Cookie;
7
8
/**
9
 * Class Request
10
 *
11
 * @package Zortje\MVC\Network
12
 */
13
class Request
14
{
15
16
    /**
17
     * @var Cookie Cookie
18
     */
19
    protected $cookie;
20
21
    /**
22
     * @var string Full URL
23
     */
24
    protected $url;
25
26
    /**
27
     * @var array POST data
28
     */
29
    public $post;
30
31
    /**
32
     * Request constructor.
33
     *
34
     * @param array  $server
35
     * @param array  $post
36
     * @param Cookie $cookie
37
     */
38 1
    public function __construct(Cookie $cookie, array $server = [], array $post = [])
39
    {
40 1
        $this->cookie = $cookie;
41 1
        $this->url    = $this->createUrlFromServerArray($server, !empty($server['HTTPS']));
42 1
        $this->post   = $post;
43 1
    }
44
45
    /**
46
     * Get request URL path
47
     *
48
     * @return string URL path
49
     */
50 3
    public function getPath(): string
51
    {
52 3
        $path = parse_url($this->url, PHP_URL_PATH);
53
54 3
        if (is_string($path) === false) {
55 2
            $path = '';
56
        }
57
58 3
        return $path;
59
    }
60
61
    /**
62
     * Get request POST data
63
     *
64
     * @return array POST data
65
     */
66 2
    public function getPost(): array
67
    {
68 2
        return $this->post;
69
    }
70
71
    /**
72
     * Get cookie
73
     *
74
     * @return Cookie
75
     */
76 2
    public function getCookie(): Cookie
77
    {
78 2
        return $this->cookie;
79
    }
80
81
    /**
82
     * Create URL from _SERVER array
83
     *
84
     * @param array $server _SERVER array
85
     * @param bool  $secure
86
     *
87
     * @return string URL
88
     */
89 1
    protected function createUrlFromServerArray(array $server, bool $secure = true): string
90
    {
91 1
        $protocol = $secure ? 'https' : 'http';
92 1
        $host     = !empty($server['HTTP_HOST']) ? $server['HTTP_HOST'] : 'www.example.com';
93 1
        $path     = !empty($server['REQUEST_URI']) ? $server['REQUEST_URI'] : '';
94
95 1
        $url = "$protocol://$host$path";
96
97 1
        return rtrim($url, '/');
98
    }
99
}
100