Controller   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 3 1
A redirect_exit() 0 3 1
A offsetUnset() 0 3 1
A __construct() 0 5 1
A head() 0 3 1
A put() 0 3 1
A delete() 0 3 1
A __call() 0 3 1
A offsetGet() 0 3 1
A offsetSet() 0 3 1
A offsetExists() 0 3 1
A get() 0 3 1
1
<?php
2
3
namespace Helix\Site;
4
5
use ArrayAccess;
6
use Helix\Site;
7
8
/**
9
 * A controller.
10
 */
11
class Controller implements ArrayAccess
12
{
13
14
    /**
15
     * Extra arguments from the router.
16
     *
17
     * @var array
18
     */
19
    protected array $extra;
20
21
    /**
22
     * The path's regex match from routing.
23
     *
24
     * `ArrayAccess` forwards to this.
25
     *
26
     * @var array
27
     */
28
    protected $path;
29
30
    /**
31
     * @var Site
32
     */
33
    protected $site;
34
35
    /**
36
     * @param Site $site
37
     * @param string[] $path
38
     * @param array $extra
39
     */
40
    public function __construct(Site $site, array $path, array $extra = [])
41
    {
42
        $this->site = $site;
43
        $this->path = $path;
44
        $this->extra = $extra;
45
    }
46
47
    /**
48
     * Throws `HTTP 501 Not Implemented` as a catch-all for any unimplemented non-standard request methods.
49
     *
50
     * @param string $method
51
     * @param array $args
52
     */
53
    final public function __call(string $method, array $args)
54
    {
55
        throw new HttpError(501);
56
    }
57
58
    /**
59
     * Handles `DELETE`
60
     *
61
     * This stub throws `HTTP 501 Not Implemented`
62
     *
63
     * @return void|string|View
64
     */
65
    public function delete()
66
    {
67
        throw new HttpError(501);
68
    }
69
70
    /**
71
     * Handles `GET`
72
     *
73
     * This stub throws `HTTP 501 Not Implemented`
74
     *
75
     * @return void|string|View
76
     */
77
    public function get()
78
    {
79
        throw new HttpError(501);
80
    }
81
82
    /**
83
     * Handles `HEAD`
84
     *
85
     * This stub returns from {@link Controller::get()}
86
     */
87
    public function head()
88
    {
89
        return $this->get();
90
    }
91
92
    /**
93
     * @param mixed $offset
94
     * @return bool
95
     */
96
    public function offsetExists($offset): bool
97
    {
98
        return isset($this->path[$offset]);
99
    }
100
101
    /**
102
     * @param mixed $offset
103
     * @return null|mixed Coalesces to `null`
104
     */
105
    public function offsetGet($offset)
106
    {
107
        return $this->path[$offset] ?? null;
108
    }
109
110
    /**
111
     * @param mixed $offset
112
     * @param mixed $value
113
     */
114
    public function offsetSet($offset, $value): void
115
    {
116
        $this->path[$offset] = $value;
117
    }
118
119
    /**
120
     * @param mixed $offset
121
     */
122
    public function offsetUnset($offset): void
123
    {
124
        unset($this->path[$offset]);
125
    }
126
127
    /**
128
     * Handles `POST`
129
     *
130
     * This stub throws `HTTP 501 Not Implemented`
131
     *
132
     * @return void|string|View
133
     */
134
    public function post()
135
    {
136
        throw new HttpError(501);
137
    }
138
139
    /**
140
     * Handles `PUT`
141
     *
142
     * This stub throws `HTTP 501 Not Implemented`
143
     *
144
     * @return void|string|View
145
     */
146
    public function put()
147
    {
148
        throw new HttpError(501);
149
    }
150
151
    /**
152
     * Forwards to {@link Response::redirect_exit()}
153
     *
154
     * @param string $path
155
     * @param int $code
156
     */
157
    protected function redirect_exit(string $path, int $code = 302)
158
    {
159
        $this->site->getResponse()->redirect_exit($path, $code);
160
    }
161
}
162