RendererContainer::createConvertContentRenderer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php namespace Neomerx\Limoncello\Errors;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \Closure;
20
use \Symfony\Component\HttpFoundation\Response;
21
use \Neomerx\Limoncello\Contracts\IntegrationInterface;
22
use \Symfony\Component\HttpKernel\Exception\HttpException;
23
use \Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface;
24
use \Neomerx\JsonApi\Contracts\Exceptions\RendererInterface;
25
use \Neomerx\JsonApi\Contracts\Responses\ResponsesInterface;
26
use \Symfony\Component\HttpKernel\Exception\GoneHttpException;
27
use \Symfony\Component\HttpKernel\Exception\ConflictHttpException;
28
use \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
use \Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
30
use \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
31
use \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
32
use \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
33
use \Symfony\Component\HttpKernel\Exception\LengthRequiredHttpException;
34
use \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
35
use \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
36
use \Neomerx\JsonApi\Exceptions\RendererContainer as BaseRendererContainer;
37
use \Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
38
use \Symfony\Component\HttpKernel\Exception\PreconditionFailedHttpException;
39
use \Symfony\Component\HttpKernel\Exception\PreconditionRequiredHttpException;
40
use \Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
41
42
/**
43
 * @package Neomerx\Limoncello
44
 *
45
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
46
 */
47
class RendererContainer extends BaseRendererContainer
48
{
49
    /**
50
     * @var IntegrationInterface
51
     */
52
    private $integration;
53
54
    /**
55
     * @var ResponsesInterface
56
     */
57
    private $responses;
58
59
    /**
60
     * @var CodecMatcherInterface
61
     */
62
    private $codecMatcher;
63
64
    /**
65
     * @param IntegrationInterface $integration
66
     * @param int                  $defaultStatusCode
67
     */
68 2
    public function __construct(
69
        IntegrationInterface $integration,
70
        $defaultStatusCode = Response::HTTP_INTERNAL_SERVER_ERROR
71
    ) {
72 2
        $this->integration = $integration;
73
74 2
        parent::__construct($this->createNoContentRenderer($defaultStatusCode));
75
76 2
        $this->registerHttpCodeMapping([
77 2
            HttpException::class                        => Response::HTTP_INTERNAL_SERVER_ERROR,
78 2
            GoneHttpException::class                    => Response::HTTP_GONE,
79 2
            ConflictHttpException::class                => Response::HTTP_CONFLICT,
80 2
            NotFoundHttpException::class                => Response::HTTP_NOT_FOUND,
81 2
            BadRequestHttpException::class              => Response::HTTP_BAD_REQUEST,
82 2
            AccessDeniedHttpException::class            => Response::HTTP_FORBIDDEN,
83 2
            UnauthorizedHttpException::class            => Response::HTTP_UNAUTHORIZED,
84 2
            NotAcceptableHttpException::class           => Response::HTTP_NOT_ACCEPTABLE,
85 2
            LengthRequiredHttpException::class          => Response::HTTP_LENGTH_REQUIRED,
86 2
            TooManyRequestsHttpException::class         => Response::HTTP_TOO_MANY_REQUESTS,
87 2
            MethodNotAllowedHttpException::class        => Response::HTTP_METHOD_NOT_ALLOWED,
88 2
            PreconditionFailedHttpException::class      => Response::HTTP_PRECONDITION_FAILED,
89 2
            ServiceUnavailableHttpException::class      => Response::HTTP_SERVICE_UNAVAILABLE,
90 2
            PreconditionRequiredHttpException::class    => Response::HTTP_PRECONDITION_REQUIRED,
91 2
            UnsupportedMediaTypeHttpException::class    => Response::HTTP_UNSUPPORTED_MEDIA_TYPE,
92 2
        ]);
93 2
    }
94
95
    /**
96
     * @return ResponsesInterface
97
     */
98 2
    protected function getResponses()
99
    {
100 2
        if ($this->responses === null) {
101 2
            $this->responses = $this->integration->getFromContainer(ResponsesInterface::class);
102 2
        }
103
104 2
        return $this->responses;
105
    }
106
107
    /**
108
     * @return CodecMatcherInterface
109
     */
110 1
    protected function getCodecMatcher()
111
    {
112 1
        if ($this->codecMatcher === null) {
113 1
            $this->codecMatcher = $this->integration->getFromContainer(CodecMatcherInterface::class);
114 1
        }
115
116 1
        return $this->codecMatcher;
117
    }
118
119
    /**
120
     * @param int $statusCode
121
     *
122
     * @return RendererInterface
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoContentRenderer.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
123
     */
124 2
    public function createNoContentRenderer($statusCode)
125
    {
126 2
        return new NoContentRenderer($this->getResponses(), $statusCode);
127
    }
128
129
    /**
130
     * @param int     $statusCode
131
     * @param Closure $converter
132
     *
133
     * @return RendererInterface
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use ConvertContentRenderer.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
134
     */
135 1
    public function createConvertContentRenderer($statusCode, Closure $converter)
136
    {
137 1
        return new ConvertContentRenderer($this->getCodecMatcher(), $this->getResponses(), $statusCode, $converter);
138
    }
139
140
    /**
141
     * @param array $exToCodeMapping
142
     */
143 2
    public function registerHttpCodeMapping(array $exToCodeMapping)
144
    {
145 2
        foreach ($exToCodeMapping as $exClass => $statusCode) {
146 2
            $renderer = $this->createNoContentRenderer($statusCode);
147 2
            $this->registerRenderer($exClass, $renderer);
148 2
        }
149 2
    }
150
}
151