AbstractAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
dl 0
loc 38
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A throwResponse() 0 3 1
A attachView() 0 5 1
A json() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Voltage;
6
7
use Lit\Nimo\Handlers\AbstractHandler;
8
use Lit\Voltage\Interfaces\ViewInterface;
9
use Psr\Http\Message\ResponseFactoryInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * Base class for actions.
14
 *
15
 * It's strongly recommended to have your own action base class extending this one
16
 */
17
abstract class AbstractAction extends AbstractHandler
18
{
19
    /**
20
     * @var ResponseFactoryInterface
21
     */
22
    protected $responseFactory;
23
24
    /**
25
     * Halt execution and return the given response.
26
     *
27
     * @param ResponseInterface $response The response to be thrown.
28
     * @throws ThrowableResponse Throws a standard exception containing given response.
29
     * @return void
30
     */
31
    protected static function throwResponse(ResponseInterface $response): void
32
    {
33
        throw ThrowableResponse::of($response);
34
    }
35
36
    /**
37
     * @param ViewInterface $view The view instance to be used with this action.
38
     * @return ViewInterface
39
     */
40
    protected function attachView(ViewInterface $view)
41
    {
42
        $view->setResponse($this->responseFactory->createResponse());
43
44
        return $view;
45
    }
46
47
    /**
48
     * @return JsonView
49
     */
50
    protected function json(): JsonView
51
    {
52
        /** @var JsonView $view */
53
        $view = $this->attachView(new JsonView());
54
        return $view;
55
    }
56
}
57