Completed
Push — master ( bc91aa...cd6df5 )
by Eric
115:14 queued 112:36
created

getMappedClasses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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 extends AbstractClassMetadataLoader implements MappedClassMetadataLoaderInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $file;
25
26
    /**
27
     * @var mixed[]
28
     */
29
    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...
30
31
    /**
32
     * @param string                   $file
33
     * @param TypeParserInterface|null $typeParser
34
     */
35 1396
    public function __construct($file, TypeParserInterface $typeParser = null)
36
    {
37 1396
        parent::__construct($typeParser);
38
39 1396
        if (!is_file($file)) {
40 24
            throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
41
        }
42
43 1396
        if (!is_readable($file)) {
44
            throw new \InvalidArgumentException(sprintf('The file "%s" is not readable.', $file));
45
        }
46
47 1396
        $this->file = $file;
48 1396
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 560
    public function getMappedClasses()
54
    {
55 560
        if ($this->data === null) {
56 560
            $this->data = $this->loadFile($this->file);
57 280
        }
58
59 560
        return array_keys($this->data);
60
    }
61
62
    /**
63
     * @param string $file
64
     *
65
     * @return mixed[]
66
     */
67
    abstract protected function loadFile($file);
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1236
    protected function loadData($class)
73
    {
74 1236
        if ($this->data === null) {
75 676
            $this->data = $this->loadFile($this->file);
76 276
        }
77
78 1112
        if (isset($this->data[$class])) {
79 1084
            return $this->data[$class];
80
        }
81 28
    }
82
}
83