Completed
Pull Request — master (#17)
by Eric
116:50 queued 51:51
created

AbstractFileClassMetadataLoader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 66
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A getMappedClasses() 0 8 2
loadFile() 0 1 ?
A loadData() 0 10 3
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping\Loader;
13
14
use Ivory\Serializer\Type\Parser\TypeParserInterface;
15
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
abstract class AbstractFileClassMetadataLoader
20
    extends AbstractClassMetadataLoader
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "AbstractClassMetadataLoader" and comma; 1 found
Loading history...
21
    implements MappedClassMetadataLoaderInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
22
{
23
    /**
24
     * @var string
25
     */
26
    private $file;
27
28
    /**
29
     * @var mixed[]
30
     */
31
    private $data;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
33
    /**
34
     * @param string                   $file
35 1396
     * @param TypeParserInterface|null $typeParser
36
     */
37 1396
    public function __construct($file, TypeParserInterface $typeParser = null)
38
    {
39 1396
        parent::__construct($typeParser);
40 24
41
        if (!is_file($file)) {
42
            throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
43 1396
        }
44
45
        if (!is_readable($file)) {
46
            throw new \InvalidArgumentException(sprintf('The file "%s" is not readable.', $file));
47 1396
        }
48 1396
49
        $this->file = $file;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getMappedClasses()
56
    {
57
        if ($this->data === null) {
58
            $this->data = $this->loadFile($this->file);
59
        }
60 1236
61
        return array_keys($this->data);
62 1236
    }
63 1236
64 556
    /**
65
     * @param string $file
66 1112
     *
67 1084
     * @return mixed[]
68
     */
69 28
    abstract protected function loadFile($file);
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function loadData($class)
75
    {
76
        if ($this->data === null) {
77
            $this->data = $this->loadFile($this->file);
78
        }
79
80
        if (isset($this->data[$class])) {
81
            return $this->data[$class];
82
        }
83
    }
84
}
85