DefinitionBuilder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 3
cbo 5
dl 0
loc 114
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A defineParameter() 0 4 1
A defineImplementors() 0 6 1
A addDefinition() 0 19 3
A hasDefinition() 0 4 1
A getDefinitionCollection() 0 4 1
A getParameterCollection() 0 4 1
A getImplementors() 0 4 1
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 02.08.16
6
 * Time: 0:46.
7
 */
8
namespace samsonframework\container\definition\builder;
9
10
use samsonframework\container\definition\AbstractDefinition;
11
use samsonframework\container\definition\ClassBuilderInterface;
12
use samsonframework\container\definition\ClassDefinition;
13
use samsonframework\container\definition\exception\ClassDefinitionAlreadyExistsException;
14
use samsonframework\container\definition\parameter\exception\ParameterAlreadyExistsException;
15
use samsonframework\container\definition\parameter\ParameterBuilder;
16
use samsonframework\container\definition\parameter\ParameterBuilderInterface;
17
use samsonframework\container\definition\reference\ClassReference;
18
use samsonframework\container\definition\reference\ReferenceDependencyInterface;
19
use samsonframework\container\definition\reference\ReferenceInterface;
20
21
/**
22
 * Class DefinitionBuilder
23
 *
24
 * @author Ruslan Molodyko <[email protected]>
25
 */
26
class DefinitionBuilder extends AbstractDefinition
27
{
28
    /** @var  ClassDefinition[] Definition collection */
29
    protected $definitionCollection = [];
30
    /** @var  ParameterBuilder */
31
    protected $parameterBuilder;
32
    /** @var array Collection of interface => [classes]*/
33
    protected $implementors = [];
34
35
    /**
36
     * DefinitionBuilder constructor.
37
     *
38
     * @param ParameterBuilder $parameterBuilder
39
     * @param AbstractDefinition|null $parentDefinition
40
     */
41 18
    public function __construct(ParameterBuilder $parameterBuilder, AbstractDefinition $parentDefinition = null)
42
    {
43 18
        parent::__construct($parentDefinition);
44
45 18
        $this->parameterBuilder = $parameterBuilder;
46 18
        $this->parameterBuilder->setParentDefinition($this);
47 18
    }
48
49
    /**
50
     * Define parameters
51
     *
52
     * @param string $name
53
     * @param ReferenceInterface $reference
54
     * @return ParameterBuilderInterface
55
     * @throws ParameterAlreadyExistsException
56
     */
57 3
    public function defineParameter(string $name, ReferenceInterface $reference): ParameterBuilderInterface
58
    {
59 3
        return $this->parameterBuilder->defineParameter($name, $reference);
60
    }
61
62
    /**
63
     * Define interface implementors
64
     *
65
     * @param string $interfaceName
66
     * @param ReferenceInterface $class
67
     * @return DefinitionBuilder
68
     */
69
    public function defineImplementors(string $interfaceName, ReferenceInterface $class): DefinitionBuilder
70
    {
71
        $this->implementors[$interfaceName] = $class;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Add new class definition
78
     *
79
     * @param $className
80
     * @param string $serviceName
81
     * @return ClassBuilderInterface
82
     * @throws \InvalidArgumentException
83
     * @throws ClassDefinitionAlreadyExistsException
84
     */
85 16
    public function addDefinition($className, string $serviceName = null): ClassBuilderInterface
86
    {
87
        // Check if class already exists
88 16
        if ($this->hasDefinition($className)) {
89 1
            throw new ClassDefinitionAlreadyExistsException();
90
        }
91
92
        // Create new definition
93 16
        $classDefinition = new ClassDefinition($this);
94 16
        $classDefinition->setClassName(new ClassReference($className));
95 16
        if ($serviceName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $serviceName of type null|string 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...
96 2
            $classDefinition->setServiceName($serviceName);
97
        }
98
99
        // Register definition
100 16
        $this->definitionCollection[$className] = $classDefinition;
101
102 16
        return $classDefinition;
103
    }
104
105
    /**
106
     * When definition for class name is exists in collection
107
     *
108
     * @param $className
109
     * @return bool
110
     */
111 16
    public function hasDefinition($className): bool
112
    {
113 16
        return array_key_exists($className, $this->definitionCollection);
114
    }
115
116
    /**
117
     * @return ClassDefinition[]
118
     */
119 10
    public function getDefinitionCollection(): array
120
    {
121 10
        return $this->definitionCollection;
122
    }
123
124
    /**
125
     * @return ReferenceInterface[]
126
     */
127 4
    public function getParameterCollection(): array
128
    {
129 4
        return $this->parameterBuilder->getParameterCollection();
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getImplementors(): array
136
    {
137
        return $this->implementors;
138
    }
139
}
140