JsonConverter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 24
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 8 2
A contentType() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of Cors
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Cors\Infrastructure\Converter;
13
14
use Slick\Cors\Infrastructure\Converter;
15
use Slick\ErrorHandler\Exception\ExceptionInspector;
16
use Throwable;
17
18
/**
19
 * JsonConverter
20
 *
21
 * @package Slick\Cors\Infrastructure\Converter
22
 */
23
final class JsonConverter implements Converter
24
{
25
26
    use JsonMethods;
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function convert(Throwable $throwable): string
32
    {
33
        $inspector = new ExceptionInspector($throwable);
34
        $errorDetails = json_encode([
35
            "error" => $this->clearTitle($throwable),
36
            "details" => $this->details($throwable, $inspector)
37
        ]);
38
        return $errorDetails ?: '';
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function contentType(): string
45
    {
46
        return 'application/json';
47
    }
48
}
49