Completed
Push — feature-20rc1 ( 008ae2 )
by Rob
16:55
created

AbstractFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 93
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createReference() 0 4 1
A createChildReference() 0 4 1
A createChildDefinition() 0 4 1
A getFactoryServiceName() 0 9 4
A getPrototypeFactoryServiceName() 0 4 2
A registerFactoryDefinition() 0 15 1
getDefinitionNamePrefix() 0 1 ?
getDefinitionTagContext() 0 1 ?
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
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 Liip\ImagineBundle\DependencyInjection\Factory;
13
14
use Symfony\Component\DependencyInjection\ChildDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
abstract class AbstractFactory implements FactoryInterface
20
{
21
    /**
22
     * @param string|null $name
23
     * @param string      $type
24
     *
25
     * @return Reference
26
     */
27
    final protected function createReference(string $name = null, string $type = 'prototype'): Reference
28
    {
29
        return new Reference($this->getFactoryServiceName($name, $type));
30
    }
31
32
    /**
33
     * @param string|null $name
34
     *
35
     * @return Reference
36
     */
37
    final protected function createChildReference(string $name = null): Reference
38
    {
39
        return new Reference($this->getPrototypeFactoryServiceName($name));
40
    }
41
42
    /**
43
     * @param string|null $name
44
     *
45
     * @return ChildDefinition
46
     */
47
    final protected function createChildDefinition(string $name = null): ChildDefinition
48
    {
49
        return new ChildDefinition($this->getPrototypeFactoryServiceName($name));
50
    }
51
52
    /**
53
     * @param string|null $name
54
     * @param string|null $type
55
     * @param bool        $prefix
56
     *
57
     * @return string
58
     */
59
    final protected function getFactoryServiceName(string $name = null, string $type = null, bool $prefix = true): string
60
    {
61
        return vsprintf('%s%s%s%s', [
62
            static::getDefinitionNamePrefix(),
63
            $prefix ? sprintf('.%s', $this->getName()) : '',
64
            $name ? sprintf('.%s', $name) : '',
65
            $type ? sprintf('.%s', $type) : '',
66
        ]);
67
    }
68
69
    /**
70
     * @param string|null $name
71
     *
72
     * @return string
73
     */
74
    final protected function getPrototypeFactoryServiceName(string $name = null): string
75
    {
76
        return sprintf('%s.%s.prototype', static::getDefinitionNamePrefix(), $name ?: $this->getName());
77
    }
78
79
    /**
80
     * @param string           $name
81
     * @param Definition       $def
82
     * @param ContainerBuilder $container
83
     *
84
     * @return string
85
     */
86
    final protected function registerFactoryDefinition(string $name, Definition $def, ContainerBuilder $container): string
87
    {
88
        $def->addTag(static::getDefinitionNamePrefix(), [
89
            static::getDefinitionTagContext() => $name,
90
        ]);
91
92
        $def->setPublic(true);
93
94
        $container->setDefinition(
95
            $key = $this->getFactoryServiceName($name, null, false),
96
            $def
97
        );
98
99
        return $key;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    abstract protected static function getDefinitionNamePrefix(): string;
106
107
    /**
108
     * @return string
109
     */
110
    abstract protected static function getDefinitionTagContext(): string;
111
}
112