Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 34
PlatesStrategy
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
90.00
0.00% covered (danger)
0.00%
0 / 34
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 7
 invokeRouteCallable
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 12
 getResponseWithBodyAndStatus
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 6
 getNotFoundDecorator
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 getMethodNotAllowedDecorator
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 getExceptionHandler
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
<?php
namespace Bone\Router;
use Bone\Router\Decorator\ExceptionDecorator;
use Bone\Router\Decorator\NotAllowedDecorator;
use Bone\Router\Decorator\NotFoundDecorator;
use Bone\View\ViewEngine;
use Bone\Router\Traits\HasLayoutTrait;
use Exception;
use League\Route\Http\Exception\{MethodNotAllowedException, NotFoundException};
use League\Route\Route;
use League\Route\Strategy\ApplicationStrategy;
use League\Route\Strategy\StrategyInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\Diactoros\Stream;
class PlatesStrategy extends ApplicationStrategy implements StrategyInterface
{
    use HasLayoutTrait;
    /** @var ViewEngine $viewEngine */
    private $viewEngine;
    /** @var NotFoundDecorator $notFoundDecorator */
    private $notFoundDecorator;
    /** @var NotAllowedDecorator $notAllowedDecorator */
    private $notAllowedDecorator;
    /** @var ExceptionDecorator $exceptionDecorator */
    private $exceptionDecorator;
    /**
     * PlatesStrategy constructor.
     * @param ViewEngine $viewEngine
     * @param NotFoundDecorator $notFound
     * @param NotAllowedDecorator $notAllowed
     * @param string $layout
     */
    public function __construct(ViewEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout, ExceptionDecorator $exceptionDecorator)
    {
        $this->viewEngine = $viewEngine;
        $this->notFoundDecorator = $notFound;
        $this->notAllowedDecorator = $notAllowed;
        $this->exceptionDecorator = $exceptionDecorator;
        $this->setLayout($layout);
    }
    /**
     * Invoke the route callable based on the strategy.
     *
     * @param \League\Route\Route $route
     * @param \Psr\Http\Message\ServerRequestInterface $request
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface
    {
        $response = parent::invokeRouteCallable($route, $request);
        $contentType = $response->getHeader('Content-Type');
        $isHtmlResponse = $response instanceof HtmlResponse;
        $hasHtmlContent = count($contentType) ? strstr($contentType[0], 'text/html') : true;
        if (!$isHtmlResponse || !$hasHtmlContent) {
            return $response;
        }
        $body = ['content' => $response->getBody()->getContents()];
        $body = $this->viewEngine->render($this->layout, $body);
        return $this->getResponseWithBodyAndStatus($response, $body, $response->getStatusCode());
    }
    /**
     * @param ResponseInterface $response
     * @param string $body
     * @param int $status
     * @return \Psr\Http\Message\MessageInterface|Response
     */
    private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200)
    {
        $stream = new Stream('php://memory', 'r+');
        $stream->write($body);
        $response = $response->withStatus($status)->withBody($stream);
        return $response;
    }
    /**
     * Get a middleware that will decorate a NotFoundException
     *
     * @param \League\Route\Http\Exception\NotFoundException $exception
     *
     * @return \Psr\Http\Server\MiddlewareInterface
     */
    public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface
    {
        return $this->notFoundDecorator;
    }
    /**
     * Get a middleware that will decorate a NotAllowedException
     *
     * @param \League\Route\Http\Exception\NotFoundException $e
     *
     * @return \Psr\Http\Server\MiddlewareInterface
     */
    public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface
    {
        return $this->notAllowedDecorator;
    }
    /**
     * @return MiddlewareInterface
     */
    public function getExceptionHandler(): MiddlewareInterface
    {
        return $this->exceptionDecorator;
    }
}