Completed
Push — dev-master ( c11256...d5c375 )
by Derek Stephen
02:03
created

PlatesStrategy::setI18nEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bone\Mvc\Router;
4
5
use Bone\Mvc\Router\Decorator\NotAllowedDecorator;
6
use Bone\Mvc\Router\Decorator\NotFoundDecorator;
7
use Bone\Mvc\View\PlatesEngine;
8
use Bone\Traits\LayoutAwareTrait;
9
use Exception;
10
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException};
11
use League\Route\Route;
12
use League\Route\Strategy\ApplicationStrategy;
13
use League\Route\Strategy\StrategyInterface;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Server\MiddlewareInterface;
17
use Zend\Diactoros\Response;
18
use Zend\Diactoros\Response\HtmlResponse;
19
use Zend\Diactoros\Response\JsonResponse;
20
use Zend\Diactoros\Stream;
21
22
class PlatesStrategy extends ApplicationStrategy implements StrategyInterface
23
{
24
    use LayoutAwareTrait;
25
26
    /** @var PlatesEngine $viewEngine */
27
    private $viewEngine;
28
29
    /** @var NotFoundDecorator $notFoundDecorator */
30
    private $notFoundDecorator;
31
32
    /** @var NotAllowedDecorator $notAllowedDecorator */
33
    private $notAllowedDecorator;
34
35
    /** @var bool  */
36
    private $i18nEnabled = false;
37
38
    /** @var array  */
39
    private $supportedLocales = [];
40
41
    /**
42
     * PlatesStrategy constructor.
43
     * @param PlatesEngine $viewEngine
44
     * @param NotFoundDecorator $notFound
45
     * @param NotAllowedDecorator $notAllowed
46
     * @param string $layout
47
     */
48 9
    public function __construct(PlatesEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout)
49
    {
50 9
        $this->viewEngine = $viewEngine;
51 9
        $this->notFoundDecorator = $notFound;
52 9
        $this->notAllowedDecorator = $notAllowed;
53 9
        $this->setLayout($layout);
54 9
    }
55
56
    /**
57
     * @param bool $i18nEnabled
58
     */
59 8
    public function setI18nEnabled(bool $i18nEnabled): void
60
    {
61 8
        $this->i18nEnabled = $i18nEnabled;
62 8
    }
63
64
    /**
65
     * @param array $locales
66
     */
67 8
    public function setSupportedLocales(array $locales): void
68
    {
69 8
        $this->supportedLocales = $locales;
70 8
    }
71
72
73
    /**
74
     * Invoke the route callable based on the strategy.
75
     *
76
     * @param \League\Route\Route $route
77
     * @param \Psr\Http\Message\ServerRequestInterface $request
78
     *
79
     * @return \Psr\Http\Message\ResponseInterface
80
     */
81 4
    public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface
82
    {
83
        try {
84
85 4
            $response = parent::invokeRouteCallable($route, $request);
86 3
            if ($response instanceof JsonResponse) {
87 1
                return $response;
88
            }
89
90 2
            $body = ['content' => $response->getBody()->getContents()];
91 2
            $body = $this->viewEngine->render($this->layout, $body);
92
93 2
            return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode());
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<Zend\Diactoros\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
94
95 1
        } catch (Exception $e) {
96 1
            $body = $this->viewEngine->render('error/error', [
97 1
                'message' => $e->getMessage(),
98 1
                'code' => $e->getCode(),
99 1
                'trace' => $e->getTrace(),
100
            ]);
101 1
            $body = $this->viewEngine->render($this->layout, [
102 1
                'content' => $body,
103
            ]);
104 1
            $status = ($e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500;
105
106 1
            return $this->getResponseWithBodyAndStatus(new HtmlResponse($body), $body, $status);
107
        }
108
109
    }
110
111
    /**
112
     * @param ResponseInterface $response
113
     * @param string $body
114
     * @param int $status
115
     * @return \Psr\Http\Message\MessageInterface|Response
116
     */
117 3
    private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200)
118
    {
119 3
        $stream = new Stream('php://memory', 'r+');
120 3
        $stream->write($body);
121 3
        $response = $response->withStatus($status)->withBody($stream);
122
123 3
        return $response;
124
    }
125
126
    /**
127
     * Get a middleware that will decorate a NotFoundException
128
     *
129
     * @param \League\Route\Http\Exception\NotFoundException $exception
0 ignored issues
show
Bug introduced by
There is no parameter named $exception. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
130
     *
131
     * @return \Psr\Http\Server\MiddlewareInterface
132
     */
133 1
    public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface
134
    {
135 1
        return $this->notFoundDecorator;
136
    }
137
138
    /**
139
     * Get a middleware that will decorate a NotAllowedException
140
     *
141
     * @param \League\Route\Http\Exception\NotFoundException $e
142
     *
143
     * @return \Psr\Http\Server\MiddlewareInterface
144
     */
145 1
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface
146
    {
147 1
        return $this->notAllowedDecorator;
148
    }
149
}