ResponsePayloadTransformer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 51.43%

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 11
eloc 38
c 5
b 1
f 3
dl 0
loc 91
ccs 18
cts 35
cp 0.5143
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toViewResponse() 0 23 2
A make() 0 5 1
A __construct() 0 6 1
A toResponse() 0 17 6
A responseControllerMethod() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nip\Controllers\Response;
6
7
use Nip\Controllers\Controller;
8
use Nip\Controllers\Traits\HasResponseTrait;
9
use Nip\Controllers\Traits\HasViewTrait;
10
use Nip\Controllers\View\ControllerViewHydrator;
11
use Nip\Http\Response\JsonResponse;
12
use Nip\Http\Response\Response;
13
use Nip\Utility\Str;
14
15
/**
16
 * Class ResponsePayloadTransformer
17
 * @package Nip\Controllers\Response
18
 */
19
class ResponsePayloadTransformer
20
{
21
    /**
22
     * @var Controller|HasViewTrait
23
     */
24
    protected $controller;
25
26
    protected $payload;
27
    protected $factory;
28
29
    /**
30
     * @param Controller|HasResponseTrait $controller
31 1
     * @param ResponsePayload $payload
32
     * @return Response
33 1
     */
34
    public static function make($controller, ResponsePayload $payload)
35 1
    {
36
        $transformer = new static($controller, $payload);
37
38
        return $transformer->toResponse();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $transformer->toResponse() also could return the type Nip\Http\Response\JsonResponse which is incompatible with the documented return type Nip\Http\Response\Response.
Loading history...
39
    }
40
41
    /**
42
     * ResponsePayloadTransformer constructor.
43 1
     * @param Controller $controller
44
     * @param ResponsePayload $payload
45 1
     */
46 1
    protected function __construct($controller, ResponsePayload $payload)
47
    {
48 1
        $this->controller = $controller;
49 1
        $this->payload = $payload;
50
51
        $this->factory = new ResponseFactory();
52
    }
53
54
55 1
    /**
56
     * @return Response|JsonResponse
57 1
     */
58 1
    protected function toResponse()
59
    {
60
        $controllerMethod = $this->responseControllerMethod();
61 1
        if (method_exists($this->controller, $controllerMethod)) {
62 1
            return call_user_func_array([$this->controller, $controllerMethod], [$this->factory, $this->payload]);
63
        }
64
        $format = $this->payload->getFormat();
65
        switch ($format) {
66 1
            case 'view':
67
            case 'html':
68
            case 'modal':
69
                return $this->toViewResponse($format);
70
            case 'json':
71
                return $this->factory->json($this->payload->all());
72
        }
73
74
        return $this->factory->noContent();
75
    }
76
77
    /**
78
     * @return Response
79
     */
80
    protected function toViewResponse($format): Response
81
    {
82
        $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

82
        /** @scrutinizer ignore-call */ 
83
        $view = $this->controller->getView();
Loading history...
83
84
        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

84
        ControllerViewHydrator::populatePath($view, /** @scrutinizer ignore-type */ $this->controller);
Loading history...
85
        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

85
        ControllerViewHydrator::initVars($view, /** @scrutinizer ignore-type */ $this->controller);
Loading history...
86
        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

86
        ControllerViewHydrator::initContentBlocks(/** @scrutinizer ignore-type */ $view, $this->controller);
Loading history...
87
88
        $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

88
        $this->factory->setView(/** @scrutinizer ignore-type */ $view);
Loading history...
89
        $this->payload->headers->set('Content-Type', 'text/html');
90
91
        $viewPath = $format == 'modal' ? '/' . trim($view->getBlock('content'), '/') : $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

91
        $viewPath = $format == 'modal' ? '/' . trim($view->getBlock('content'), '/') : $this->controller->/** @scrutinizer ignore-call */ getLayoutPath(
Loading history...
Bug introduced by
It seems like $view->getBlock('content') can also be of type null; however, parameter $string of trim() does only seem to accept string, 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

91
        $viewPath = $format == 'modal' ? '/' . trim(/** @scrutinizer ignore-type */ $view->getBlock('content'), '/') : $this->controller->getLayoutPath(
Loading history...
92
        );
93
94
        $response = $this->factory->view(
95
            $viewPath,
96 1
            $this->payload->data->all(),
97
            Response::HTTP_OK,
98 1
            $this->payload->headers->all()
99 1
        );
100 1
101
        $response->setCharset('utf-8');
102
        return $response;
103
    }
104
105
    protected function responseControllerMethod(): string
106
    {
107
        return $this->controller->getAction()
108
            . Str::studly($this->payload->getFormat())
109
            . 'Response';
110
    }
111
}
112