Passed
Push — master ( c757f0...00b6ca )
by Koldo
02:49
created

MetadataConfigurationFactory::make()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 8.7972
cc 4
eloc 14
nc 3
nop 0
crap 4
1
<?php
2
3
namespace DRKP\ZF3Doctrine;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\ORM\Tools\Setup;
7
8
class MetadataConfigurationFactory
9
{
10
    /**
11
     * Valid mapping types.
12
     */
13
    const VALID_MAPPING_TYPES = [
14
        'yaml',
15
        'xml',
16
        'annotation'
17
    ];
18
19
    /**
20
     * Minimum required mapping keys.
21
     */
22
    const MAPPING_KEYS = [
23
        'path',
24
        'namespace',
25
        'type'
26
    ];
27
28
    /**
29
     * @var array
30
     */
31
    private $mappings;
32
33
    /**
34
     * @var string
35
     */
36
    private $type;
37
38
    /**
39
     * @var bool
40
     */
41
    private $isDevMode;
42
43
    /**
44
     * @var null
45
     */
46
    private $proxyDir;
47
48
    /**
49
     * @var Cache
50
     */
51
    private $cache;
52
53
    /**
54
     * MetadataConfigurationFactory constructor.
55
     * @param $mappings
56
     * @param $type
57
     */
58 6
    public function __construct(array $mappings, $type, $isDevMode = false, $proxyDir = null, Cache $cache = null)
59
    {
60 6
        foreach ($mappings as $mapping) {
61 6
            if (false === MappingConfigValidator::validate($mapping)) {
62 1
                throw new \InvalidArgumentException(
63 1
                    'Mapping type should have at least an array with following keys ' . implode(', ', self::MAPPING_KEYS)
64 1
                );
65
            }
66 5
        }
67
68 5
        $this->mappings = $mappings;
69
70 5
        if (!in_array($type, self::VALID_MAPPING_TYPES, true)) {
71 1
            throw new \InvalidArgumentException(
72 1
                'Mapping type should be one of ' . implode(', ', self::VALID_MAPPING_TYPES)
73 1
            );
74
        }
75
76 4
        $this->type = $type;
77 4
        $this->isDevMode = $isDevMode;
78 4
        $this->proxyDir = $proxyDir;
79 4
        $this->cache = $cache;
80 4
    }
81
82
    /**
83
     * @return mixed
84
     */
85 4
    public function make()
86
    {
87 4
        $metadataConfigMethod = sprintf(
88 4
            'create%sMetadataConfiguration',
89 4
            'annotation' === $this->type ? ucfirst($this->type) : strtoupper($this->type)
90 4
        );
91
92 4
        $mappings = [];
93
94 4
        foreach ($this->mappings as $mapping) {
95 4
            if ($this->type !== $mapping['type']) {
96 1
                continue;
97
            }
98 4
            $mappings[] = $mapping['path'];
99 4
        }
100
101 4
        return Setup::$metadataConfigMethod(
102 4
            $mappings,
103 4
            $this->isDevMode,
104 4
            $this->proxyDir,
105 4
            $this->cache
106 4
        );
107
    }
108
109
}
110