Passed
Push — master ( 148695...692a71 )
by Gabriel
04:05 queued 10s
created

ResponsePayloadTransformer::toViewResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 14
c 3
b 1
f 1
dl 0
loc 22
rs 9.7998
ccs 0
cts 15
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Nip\Controllers\Response;
4
5
use Nip\Controllers\Controller;
6
use Nip\Controllers\Traits\HasResponseTrait;
7
use Nip\Controllers\Traits\HasViewTrait;
8
use Nip\Controllers\View\ControllerViewHydrator;
9
use Nip\Http\Response\Response;
10
use Nip\Utility\Str;
11
12
/**
13
 * Class ResponsePayloadTransformer
14
 * @package Nip\Controllers\Response
15
 */
16
class ResponsePayloadTransformer
17
{
18
    /**
19
     * @var Controller|HasViewTrait
20
     */
21
    protected $controller;
22
23
    protected $payload;
24
    protected $factory;
25
26
    /**
27
     * @param Controller|HasResponseTrait $controller
28
     * @param ResponsePayload $payload
29
     * @return \Nip\Http\Response\Response
30
     */
31 1
    public static function make($controller, ResponsePayload $payload)
32
    {
33 1
        $transformer = new static($controller, $payload);
34
35 1
        return $transformer->toResponse();
36
    }
37
38
    /**
39
     * ResponsePayloadTransformer constructor.
40
     * @param Controller $controller
41
     * @param ResponsePayload $payload
42
     */
43 1
    protected function __construct($controller, ResponsePayload $payload)
44
    {
45 1
        $this->controller = $controller;
46 1
        $this->payload = $payload;
47
48 1
        $this->factory = new ResponseFactory();
49 1
    }
50
51
52
    /**
53
     * @return \Nip\Http\Response\Response
54
     */
55 1
    protected function toResponse()
56
    {
57 1
        $controllerMethod = $this->responseControllerMethod();
58 1
        if (method_exists($this->controller, $controllerMethod)) {
59
            return call_user_func_array([$this->controller, $controllerMethod], [$this->factory, $this->payload]);
60
        }
61 1
        $format = $this->payload->getFormat();
62 1
        if (in_array($format, ['view', 'html'])) {
63
            return $this->toViewResponse();
64
        }
65
66 1
        return $this->factory->noContent();
67
    }
68
69
    /**
70
     * @return \Nip\Http\Response\Response
71
     */
72
    protected function toViewResponse()
73
    {
74
        $view = $this->controller->getView();
0 ignored issues
show
Bug introduced by
The method getView() does not exist on Nip\Controllers\Controller. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        /** @scrutinizer ignore-call */ 
75
        $view = $this->controller->getView();
Loading history...
75
76
        ControllerViewHydrator::populatePath($view, $this->controller);
0 ignored issues
show
Bug introduced by
It seems like $this->controller can also be of type Nip\Controllers\Traits\HasViewTrait; however, parameter $controller of Nip\Controllers\View\Con...ydrator::populatePath() does only seem to accept Nip\Controllers\Controller, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        ControllerViewHydrator::populatePath($view, /** @scrutinizer ignore-type */ $this->controller);
Loading history...
77
        ControllerViewHydrator::initVars($view, $this->controller);
0 ignored issues
show
Bug introduced by
It seems like $this->controller can also be of type Nip\Controllers\Traits\HasViewTrait; however, parameter $controller of Nip\Controllers\View\Con...iewHydrator::initVars() does only seem to accept Nip\Controllers\Controller|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        ControllerViewHydrator::initVars($view, /** @scrutinizer ignore-type */ $this->controller);
Loading history...
78
        ControllerViewHydrator::initContentBlocks($view, $this->controller);
0 ignored issues
show
Bug introduced by
It seems like $view can also be of type boolean; however, parameter $view of Nip\Controllers\View\Con...or::initContentBlocks() does only seem to accept Nip\View\View, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
        ControllerViewHydrator::initContentBlocks(/** @scrutinizer ignore-type */ $view, $this->controller);
Loading history...
79
80
        $this->factory->setView($view);
0 ignored issues
show
Bug introduced by
It seems like $view can also be of type boolean; however, parameter $view of Nip\Controllers\Response...ponseFactory::setView() does only seem to accept Nip\View\View|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        $this->factory->setView(/** @scrutinizer ignore-type */ $view);
Loading history...
81
        $this->payload->headers->set('Content-Type', 'text/html');
82
83
        $viewPath = $this->controller->getLayoutPath();
0 ignored issues
show
Bug introduced by
The method getLayoutPath() does not exist on Nip\Controllers\Controller. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        /** @scrutinizer ignore-call */ 
84
        $viewPath = $this->controller->getLayoutPath();
Loading history...
84
85
        $response = $this->factory->view(
86
            $viewPath,
87
            $this->payload->data->all(),
88
            Response::HTTP_OK,
89
            $this->payload->headers->all()
90
        );
91
92
        $response->setCharset('utf-8');
93
        return $response;
94
    }
95
96 1
    protected function responseControllerMethod(): string
97
    {
98 1
        return $this->controller->getAction()
99 1
            .Str::studly($this->payload->getFormat())
100 1
            .'Response';
101
    }
102
}
103