Failed Conditions
Pull Request — master (#7)
by Sergey
02:55
created

Controller::flash()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Jasny;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Controller
10
 */
11
abstract class Controller
12
{
13
    /**
14
     * Server request
15
     * @var ServerRequestInterface
16
     **/
17
    protected $request = null;
18
19
    /**
20
     * Response
21
     * @var ResponseInterface
22
     **/
23
    protected $response = null;
24
25
    /**
26
     * Flash
27
     * @var Flash
28
     **/
29
    protected $flash = null;
30
31
    /**
32
     * Run the controller
33
     *
34
     * @return ResponseInterface
35
     */
36
    abstract public function run();
37
38
    /**
39
     * Get request, set for controller
40
     *
41
     * @return ServerRequestInterface
42
     */
43 1
    public function getRequest()
44
    {
45 1
        return $this->request;
46
    }
47
48
    /**
49
     * Get response. set for controller
50
     *
51
     * @return ResponseInterface
52
     */
53 15
    public function getResponse()
54
    {
55 15
        return $this->response;
56
    }
57
58
    /**
59
     * Run the controller as function
60
     *
61
     * @param ServerRequestInterface $request
62
     * @param ResponseInterface      $response
63
     * @return ResponseInterface
64
     */
65 14
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
66
    {
67 14
        $this->request = $request;
68 14
        $this->response = $response;
69
70 14
        return $this->run();
71
    }
72
73
    /**
74
     * Set the flash message and/or return the flash object.
75
     * 
76
     * @param mixed $type     flash type, eg. 'error', 'notice' or 'success'
77
     * @param mixed $message  flash message
78
     * @return Flash
79
     */
80 1
    public function flash($type = null, $message = null)
81
    {
82 1
        if (!isset($this->flash)) $this->flash = new Flash();        
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
83 1
        if ($type && $message) $this->flash->set($type, $message);
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
84
85 1
        return $this->flash;
86
    }
87
88
    /**
89
     * Check if response is 2xx succesful, or empty
90
     * 
91
     * @return boolean
92
     */
93 14
    public function isSuccessful()
94
    {
95 14
        $code = $this->getResponseStatusCode();
96
97 14
        return !$code || ($code >= 200 && $code < 300);
98
    }
99
    
100
    /**
101
     * Check if response is a 3xx redirect
102
     * 
103
     * @return boolean
104
     */
105 14
    public function isRedirection()
106
    {
107 14
        $code = $this->getResponseStatusCode();
108
109 14
        return $code >= 300 && $code < 400;
110
    }
111
    
112
    /**
113
     * Check if response is a 4xx client error
114
     * 
115
     * @return boolean
116
     */
117 14
    public function isClientError()
118
    {
119 14
        $code = $this->getResponseStatusCode();
120
121 14
        return $code >= 400 && $code < 500;
122
    }
123
    
124
    /**
125
     * Check if response is a 5xx redirect
126
     * 
127
     * @return boolean
128
     */
129 14
    public function isServerError()
130
    {
131 14
        return $this->getResponseStatusCode() >= 500;
132
    }   
133
134
    /**
135
      * Check if response is 4xx or 5xx error
136
      *
137
      * @return boolean
138
      */
139 13
     public function isError()
140
     {
141 13
         return $this->isClientError() || $this->isServerError();
142
     } 
143
144
     /**
145
      * Get status code of response
146
      *
147
      * @return int
148
      */
149 14
     protected function getResponseStatusCode()
150
     {
151 14
        $response = $this->getResponse();
152
153 14
        return $response ? $response->getStatusCode() : 0;
154
     }
155
}
156
157