Completed
Push — wip-platform ( 03cba6...2999ab )
by
unknown
05:53 queued 02:49
created

CodeGeneratorRegistry   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 72
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 11 3
A getCodeGenerator() 0 8 2
A getCodeGenerators() 0 12 3
A hasGeneratorForClass() 0 4 1
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\CodeGenerator;
13
14
class CodeGeneratorRegistry
15
{
16
    private static $generators = [];
17
18
    /**
19
     * Registers an entity code generator service.
20
     *
21
     * @param CodeGeneratorInterface $codeGenerator the entity code generator service
22
     *
23
     * @throws \Exception
24
     */
25
    public static function register(CodeGeneratorInterface $codeGenerator)
26
    {
27
        $class = get_class($codeGenerator);
28
        if (!defined("$class::ENTITY_CLASS")) {
29
            throw new \Exception($class . ' must define a ENTITY_CLASS constant.');
30
        }
31
        if (!defined("$class::ENTITY_FIELD")) {
32
            throw new \Exception($class . ' must define a ENTITY_FIELD constant.');
33
        }
34
        self::$generators[$codeGenerator::ENTITY_CLASS][$codeGenerator::ENTITY_FIELD] = $codeGenerator;
35
    }
36
37
    /**
38
     * Returns the last registered entity code generator service id for a given entity class and field.
39
     *
40
     * @param string $entityClass
41
     * @param string $entityField
42
     *
43
     * @return CodeGeneratorInterface
44
     *
45
     * @throws \Exception
46
     */
47
    public static function getCodeGenerator($entityClass, $entityField = 'code')
48
    {
49
        if (!isset(self::$generators[$entityClass][$entityField])) {
50
            throw new \Exception("There is no registered entity code generator for class $entityClass and field $entityField");
51
        }
52
53
        return self::$generators[$entityClass][$entityField];
54
    }
55
56
    /**
57
     * Returns registred code generators for specifyed entity class.
58
     *
59
     * @param string $entityClass
60
     *
61
     * @return array
62
     */
63
    public static function getCodeGenerators($entityClass = null)
64
    {
65
        if ($entityClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $entityClass of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
66
            if (!isset(self::$generators[$entityClass])) {
67
                throw new \Exception("There is no registered entity code generator for class $entityClass");
68
            }
69
70
            return self::$generators[$entityClass];
71
        } else {
72
            return self::$generators;
73
        }
74
    }
75
76
    /**
77
     * @param string $entityClass
78
     *
79
     * @return bool
80
     */
81
    public static function hasGeneratorForClass($entityClass)
82
    {
83
        return !empty(self::$generators[$entityClass]);
84
    }
85
}
86