AbstractController   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 17 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Std;
6
7
use Psr\Http\Message\ResponseInterface as Response;
8
use Psr\Http\Message\ServerRequestInterface as Request;
9
10
/**
11
 * Class AbstractController.
12
 * Intended to be extended by any newly created controller.
13
 */
14
abstract class AbstractController
15
{
16
    /**
17
     * @var Request
18
     */
19
    protected $request;
20
21
    /**
22
     * @var Response
23
     */
24
    protected $response;
25
26
    /**
27
     * @var callable
28
     */
29
    protected $next;
30
31
    /**
32
     * Executed whenever controller is instantiated.
33
     * Determines which action to invoke based on request.
34
     *
35
     * @param Request       $request  request
36
     * @param Response      $response response
37
     * @param callable|null $next     next middleware
38
     *
39
     * @return mixed
40
     */
41
    public function __invoke(Request $request, Response $response, callable $next = null)
42
    {
43
        $action = $request->getAttribute('action', 'index');
44
45
        if (!method_exists($this, $action)) {
46
            // clone new response with 404 status code set
47
            $response = $response->withStatus(404);
48
49
            return $next($request, $response, new \Exception("Function '$action' is not defined!", 404));
50
        }
51
52
        $this->request = $request;
53
        $this->response = $response;
54
        $this->next = $next;
55
56
        return $this->$action();
57
    }
58
}
59