AbstractAction::throwResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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