Completed
Pull Request — master (#44)
by Korotkov
01:42
created

Request   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 3 1
A delete() 0 3 1
A instantiate() 0 7 2
A put() 0 3 1
A files() 0 3 1
A server() 0 3 1
A get() 0 3 1
A patch() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Jagepard <[email protected]">
7
 * @copyright Copyright (c) 2019, Jagepard
8
 * @license   https://mit-license.org/ MIT
9
 */
10
11
namespace Rudra\Container;
12
13
use Rudra\Container\Interfaces\{ContainerInterface, RequestInterface};
14
15
class Request implements RequestInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    private $instances = [];
21
22
    /**
23
     * @return ContainerInterface
24
     */
25
    public function get(): ContainerInterface
26
    {
27
        return $this->instantiate('get', Container::class, $_GET);
28
    }
29
30
    /**
31
     * @return ContainerInterface
32
     */
33
    public function post(): ContainerInterface
34
    {
35
        return $this->instantiate('post', Container::class, $_POST);
36
    }
37
38
    /**
39
     * @return ContainerInterface
40
     */
41
    public function put(): ContainerInterface
42
    {
43
        return $this->instantiate('put', Container::class);
44
    }
45
46
    /**
47
     * @return ContainerInterface
48
     */
49
    public function patch(): ContainerInterface
50
    {
51
        return $this->instantiate('patch', Container::class);
52
    }
53
54
    /**
55
     * @return ContainerInterface
56
     */
57
    public function delete(): ContainerInterface
58
    {
59
        return $this->instantiate('delete', Container::class);
60
    }
61
62
    /**
63
     * @return ContainerInterface
64
     */
65
    public function server(): ContainerInterface
66
    {
67
        return $this->instantiate('server', Container::class, $_SERVER);
68
    }
69
70
    /**
71
     * @return Files
72
     */
73
    public function files(): Files
74
    {
75
        return $this->instantiate('files', Files::class, $_FILES);
76
    }
77
78
    /**
79
     * @param $varName
80
     * @param $instance
81
     * @param $data
82
     * @return mixed
83
     */
84
    private function instantiate($varName, $instance, $data = null)
85
    {
86
        if (!array_key_exists($varName, $this->instances)) {
87
            $this->instances[$varName] = new $instance($data);
88
        }
89
90
        return $this->instances[$varName];
91
    }
92
}
93