Passed
Push — master ( 00b6ca...20dd3c )
by Koldo
03:01
created

MetadataConfigurationFactory::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.3142
cc 3
eloc 12
nc 3
nop 5
crap 3
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
        if (false === MappingConfigValidator::validate($mappings)) {
61 1
            throw new \InvalidArgumentException(
62 1
                'Mapping type should have at least an array with following keys ' . implode(', ', self::MAPPING_KEYS)
63 1
            );
64
        }
65
66 5
        $this->mappings = $mappings;
67
68 5
        if (!in_array($type, self::VALID_MAPPING_TYPES, true)) {
69 1
            throw new \InvalidArgumentException(
70 1
                'Mapping type should be one of ' . implode(', ', self::VALID_MAPPING_TYPES)
71 1
            );
72
        }
73
74 4
        $this->type = $type;
75 4
        $this->isDevMode = $isDevMode;
76 4
        $this->proxyDir = $proxyDir;
77 4
        $this->cache = $cache;
78 4
    }
79
80
    /**
81
     * @return mixed
82
     */
83 4
    public function make()
84
    {
85 4
        $metadataConfigMethod = sprintf(
86 4
            'create%sMetadataConfiguration',
87 4
            'annotation' === $this->type ? ucfirst($this->type) : strtoupper($this->type)
88 4
        );
89
90
        $mappings = array_map(function ($mapping) {
91 4
            return $mapping['path'];
92 4
        }, array_filter($this->mappings, function ($mapping) {
93 4
            return $this->type === $mapping['type'];
94 4
        }));
95
96 4
        return Setup::$metadataConfigMethod(
97 4
            $mappings,
98 4
            $this->isDevMode,
99 4
            $this->proxyDir,
100 4
            $this->cache
101 4
        );
102
    }
103
104
}
105