Completed
Push — master ( 56c365...b11c50 )
by Artem
12s
created

AbstractEnumTwigExtension   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 2
cbo 3
dl 0
loc 64
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A hasRegisteredEnumTypes() 0 4 2
A onlyOneOccurrenceFound() 0 4 1
A moreThanOneOccurrenceFound() 0 4 1
A createNoRegisteredEnumTypesException() 0 4 1
A throwExceptionIfEnumTypeIsNotRegistered() 0 6 2
1
<?php
2
/*
3
 * This file is part of the FreshDoctrineEnumBundle
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\DoctrineEnumBundle\Twig\Extension;
14
15
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
16
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException;
17
use Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException;
18
use Twig\Extension\AbstractExtension;
19
20
/**
21
 * AbstractEnumTwigExtension.
22
 *
23
 * @author Artem Henvald <[email protected]>
24
 */
25
abstract class AbstractEnumTwigExtension extends AbstractExtension
26
{
27
    /** @var AbstractEnumType[] */
28
    protected $registeredEnumTypes = [];
29
30
    /** @var array */
31
    protected $occurrences = [];
32
33
    /**
34
     * @param array $registeredTypes
35
     */
36
    public function __construct(array $registeredTypes)
37
    {
38
        foreach ($registeredTypes as $type => $details) {
39
            if (\is_subclass_of($details['class'], AbstractEnumType::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Fresh\DoctrineEnumBundl...AbstractEnumType::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
40
                $this->registeredEnumTypes[$type] = $details['class'];
41
            }
42
        }
43
    }
44
45
    /**
46
     * @return bool
47
     */
48
    protected function hasRegisteredEnumTypes(): bool
49
    {
50
        return !empty($this->registeredEnumTypes) && \is_array($this->registeredEnumTypes);
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    protected function onlyOneOccurrenceFound(): bool
57
    {
58
        return 1 === \count($this->occurrences);
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    protected function moreThanOneOccurrenceFound(): bool
65
    {
66
        return 1 < \count($this->occurrences);
67
    }
68
69
    /**
70
     * @return NoRegisteredEnumTypesException
71
     */
72
    protected function createNoRegisteredEnumTypesException(): NoRegisteredEnumTypesException
73
    {
74
        return new NoRegisteredEnumTypesException('There are no registered ENUM types.');
75
    }
76
77
    /**
78
     * @param string $enumType
79
     *
80
     * @throws EnumTypeIsNotRegisteredException
81
     */
82
    protected function throwExceptionIfEnumTypeIsNotRegistered(string $enumType): void
83
    {
84
        if (!isset($this->registeredEnumTypes[$enumType])) {
85
            throw new EnumTypeIsNotRegisteredException(\sprintf('ENUM type "%s" is not registered.', $enumType));
86
        }
87
    }
88
}
89