Completed
Push — wip-platform ( 1b7118...2b724b )
by
unknown
03:32
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
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\Bundle\CoreBundle\CodeGenerator;
14
15
class CodeGeneratorRegistry
16
{
17
    private static $generators = [];
18
19
    /**
20
     * Registers an entity code generator service.
21
     *
22
     * @param CodeGeneratorInterface $codeGenerator the entity code generator service
23
     *
24
     * @throws \Exception
25
     */
26
    public static function register(CodeGeneratorInterface $codeGenerator)
27
    {
28
        $class = get_class($codeGenerator);
29
        if (!defined("$class::ENTITY_CLASS")) {
30
            throw new \Exception($class . ' must define a ENTITY_CLASS constant.');
31
        }
32
        if (!defined("$class::ENTITY_FIELD")) {
33
            throw new \Exception($class . ' must define a ENTITY_FIELD constant.');
34
        }
35
        self::$generators[$codeGenerator::ENTITY_CLASS][$codeGenerator::ENTITY_FIELD] = $codeGenerator;
36
    }
37
38
    /**
39
     * Returns the last registered entity code generator service id for a given entity class and field.
40
     *
41
     * @param string $entityClass
42
     * @param string $entityField
43
     *
44
     * @return CodeGeneratorInterface
45
     *
46
     * @throws \Exception
47
     */
48
    public static function getCodeGenerator($entityClass, $entityField = 'code')
49
    {
50
        if (!isset(self::$generators[$entityClass][$entityField])) {
51
            throw new \Exception("There is no registered entity code generator for class $entityClass and field $entityField");
52
        }
53
54
        return self::$generators[$entityClass][$entityField];
55
    }
56
57
    /**
58
     * Returns registred code generators for specifyed entity class.
59
     *
60
     * @param string $entityClass
61
     *
62
     * @return array
63
     */
64
    public static function getCodeGenerators($entityClass = null)
65
    {
66
        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...
67
            if (!isset(self::$generators[$entityClass])) {
68
                throw new \Exception("There is no registered entity code generator for class $entityClass");
69
            }
70
71
            return self::$generators[$entityClass];
72
        } else {
73
            return self::$generators;
74
        }
75
    }
76
77
    /**
78
     * @param string $entityClass
79
     *
80
     * @return bool
81
     */
82
    public static function hasGeneratorForClass($entityClass)
83
    {
84
        return !empty(self::$generators[$entityClass]);
85
    }
86
}
87