Passed
Push — master ( 1cf0a9...56b73d )
by Ayan
01:37
created

JsonXml   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 66
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 29 6
A getContainer() 0 3 1
1
<?php
2
3
namespace Phprest\ErrorHandler\Formatter;
4
5
use League\BooBoo\Formatter\AbstractFormatter;
6
use Phprest\Application;
7
use Phprest\Config;
8
use Phprest\Entity;
9
use Phprest\Service;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class JsonXml extends AbstractFormatter
14
{
15
    use Service\Hateoas\Getter, Service\Hateoas\Util;
0 ignored issues
show
introduced by
The trait Phprest\Service\Hateoas\Util requires some properties which are not provided by Phprest\ErrorHandler\Formatter\JsonXml: $headers, $apiVersion, $vendor
Loading history...
16
17
    /**
18
     * @var Config
19
     */
20
    protected $config;
21
22
    /**
23
     * @var null|Request
24
     */
25
    protected $request;
26
27
    /**
28
     * @param Config $config
29
     * @param null|Request $request
30
     */
31 73
    public function __construct(Config $config, Request $request = null)
32
    {
33 73
        $this->config   = $config;
34 73
        $this->request  = $request;
35 73
    }
36
37
    /**
38
     * @param \Exception $exception
39
     *
40
     * @return string
41
     */
42 3
    public function format($exception)
43
    {
44 3
        $response = new Response();
45
46
        try {
47 3
            $response = $this->serialize(
48 3
                $this->config->isDebug() ? new Entity\DebugError($exception) : new Entity\Error($exception),
49 3
                is_null($this->request) ? Request::createFromGlobals() : $this->request,
50 3
                $response
51
            );
52 1
        } catch (\Exception $e) {
53 1
            $response->setContent(
54 1
                $this->serviceHateoas()->getSerializer()->serialize(
55 1
                    $this->config->isDebug() ? new Entity\DebugError($e) : new Entity\Error($e),
56 1
                    'json'
57
                )
58
            );
59
60 1
            $vendor = $this->getContainer()->get(Application::CONTAINER_ID_VENDOR);
61 1
            $apiVersion = $this->getContainer()->get(Application::CONTAINER_ID_API_VERSION);
62
63 1
            $response->headers->set('Content-Type', 'application/vnd.' . $vendor . '-v' . $apiVersion . '+json');
64
        }
65
66 3
        $response->setStatusCode(method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 500);
67
68 3
        $response->sendHeaders();
69
70 3
        return $response->getContent();
71
    }
72
73
    /**
74
     * @return \League\Container\ContainerInterface
75
     */
76 3
    protected function getContainer()
77
    {
78 3
        return $this->config->getContainer();
79
    }
80
}
81