Completed
Push — master ( 1e2cfa...28995d )
by Eric
04:38
created

FileClassMetadataLoader::__construct()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 17
cts 17
cp 1
rs 8.5125
c 1
b 0
f 0
cc 6
eloc 15
nc 9
nop 2
crap 6
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\Mapping\ClassMetadataInterface;
15
use Ivory\Serializer\Type\Parser\TypeParserInterface;
16
17
/**
18
 * @author GeLo <[email protected]>
19
 */
20
class FileClassMetadataLoader implements MappedClassMetadataLoaderInterface
21
{
22
    const EXTENSION_JSON = 'json';
23
    const EXTENSION_XML = 'xml';
24
    const EXTENSION_YAML = 'yml';
25
26
    /**
27
     * @var MappedClassMetadataLoaderInterface
28
     */
29
    private $loader;
30
31
    /**
32
     * @param string                   $file
33
     * @param TypeParserInterface|null $typeParser
34
     */
35 784
    public function __construct($file, TypeParserInterface $typeParser = null)
36
    {
37 784
        if (!file_exists($file)) {
38 24
            throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
39
        }
40
41 784
        switch (pathinfo($file, PATHINFO_EXTENSION)) {
42 784
            case self::EXTENSION_JSON:
43 364
                $this->loader = new JsonClassMetadataLoader($file, $typeParser);
44 364
                break;
45
46 580
            case self::EXTENSION_XML:
47 368
                $this->loader = new XmlClassMetadataLoader($file, $typeParser);
48 368
                break;
49
50 372
            case self::EXTENSION_YAML:
51 364
                $this->loader = new YamlClassMetadataLoader($file, $typeParser);
52 364
                break;
53 392
        }
54
55 784
        if ($this->loader === null) {
56 12
            throw new \InvalidArgumentException(sprintf('The file "%s" is not supported.', $file));
57
        }
58 784
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 688
    public function loadClassMetadata(ClassMetadataInterface $classMetadata)
64
    {
65 688
        return $this->loader->loadClassMetadata($classMetadata);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 320
    public function getMappedClasses()
72
    {
73 320
        return $this->loader->getMappedClasses();
74
    }
75
}
76