EncoderService   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 68
ccs 37
cts 37
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C getEncoder() 0 36 11
B getEncoderOptions() 0 13 7
1
<?php
2
declare(strict_types=1);
3
4
namespace RealPage\JsonApi;
5
6
use Neomerx\JsonApi\Encoder\Encoder;
7
use Neomerx\JsonApi\Encoder\EncoderOptions;
8
9
/**
10
 * Class EncoderService
11
 */
12
class EncoderService
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $config;
18
19
    /**
20
     * @var array
21
     */
22
    protected $encoders = [];
23
24 21
    public function __construct(array $config = [])
25
    {
26 21
        $this->config = $config;
27 21
    }
28
29 15
    public function getEncoder(string $name = 'default')
30
    {
31 15
        if (!isset($this->encoders[$name])) {
32 15
            if ($name === 'default') {
33 9
                $config = $this->config;
34 6
            } elseif(isset($this->config['encoders'][$name])) {
35 3
                $config = $this->config['encoders'][$name];
36
            } else {
37 3
                throw new \Exception(sprintf('No configuration found for %s "%s"', Encoder::class, $name));
38
            }
39
40 12
            $encoder_options = isset($config['encoder-options']) && is_array($config['encoder-options']) ?
41 3
                $config['encoder-options'] :
42 12
                [];
43 12
            $options = $this->getEncoderOptions($encoder_options);
44
45 12
            $encoder = Encoder::instance(
46 12
                $this->config['schemas'],
47 12
                $options
48
            );
49
50 12
            if (isset($config['jsonapi'])) {
51 6
                if (is_array($config['jsonapi'])) {
52 6
                    $encoder->withJsonApiVersion($config['jsonapi']);
53 6
                } elseif ($config['jsonapi'] === true) {
54 6
                    $encoder->withJsonApiVersion();
55
                }
56
            }
57 12
            if (isset($config['meta']) && is_array($config['meta'])) {
58 6
                $encoder->withMeta($config['meta']);
59
            }
60
61 12
            $this->encoders[$name] = $encoder;
62
        }
63 12
        return $this->encoders[$name];
64
    }
65
66 18
    protected function getEncoderOptions(array $config)
67
    {
68 18
        $options = isset($config['options']) && is_int($config['options']) ?
69 6
            $config['options'] :
70 18
            0;
71 18
        $urlPrefix = isset($config['urlPrefix']) && is_string($config['urlPrefix']) ?
72 6
            $config['urlPrefix'] :
73 18
            null;
74 18
        $depth = isset($config['depth']) && is_int($config['depth']) ?
75 6
            $config['depth'] :
76 18
            512;
77 18
        return new EncoderOptions($options, $urlPrefix, $depth);
78
    }
79
}
80