JsonDriver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 61
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
C getAllClassNames() 0 32 7
1
<?php
2
3
namespace Drest\Mapping\Driver;
4
5
use Doctrine\Common\Annotations;
6
use Drest\DrestException;
7
use Drest\Mapping\Annotation;
8
use Drest\Mapping;
9
10
/**
11
 * The JsonDriver reads a configuration file (config.json) rather than utilizing annotations.
12
 */
13
class JsonDriver extends PhpDriver
14
{
15
    protected $paths = [];
16
17 5
    public function __construct($paths)
18
    {
19 5
        parent::__construct($paths);
20 5
    }
21
22
    /**
23
     * Factory method for the Annotation Driver
24
     *
25
     * @param  array|string $paths
26
     * @return self
27
     */
28 5
    public static function create($paths = [])
29
    {
30 5
        return new self($paths);
31
    }
32
33
34
    /**
35
     * Get all the metadata class names known to this driver.
36
     * @return array
37
     * @throws DrestException
38
     * @throws DriverException
39
     */
40 4
    public function getAllClassNames()
41
    {
42 4
        if (empty($this->classes)) {
43 4
            if (empty($this->paths)) {
44
                throw DrestException::pathToConfigFilesRequired();
45
            }
46
47 4
            foreach ($this->paths as $path)
48
            {
49 4
                if(!file_exists($path)) {
50 2
                    throw DriverException::configurationFileDoesntExist($path);
51
                }
52
53 2
                $resources = json_decode(file_get_contents($path), true);
54
55 2
                if($resources === null) {
56 1
                    throw DriverException::configurationFileIsInvalid('Json');
57
                }
58
59 1
                $entities = [];
60 1
                foreach($resources['resources'] as $resource) {
61 1
                    $entity = $resource['entity'];
62 1
                    $entities[$entity] = $resource;
63 1
                    unset($entities[$entity]['entity']);
64 1
                }
65
66 1
                $this->classes = array_merge($this->classes, $entities);
67 1
            }
68 1
        }
69
70 1
        return array_keys($this->classes);
71
    }
72
73
}