MetadataConfigurationFactory::make()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

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