DebugExtension::definition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
crap 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Behat\DebugExtension\ServiceContainer;
6
7
use Behat\Testwork\ServiceContainer\Extension;
8
use Behat\Testwork\ServiceContainer\ExtensionManager;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
12
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
13
14
/**
15
 * Class DebugExtension.
16
 *
17
 * @package Behat\DebugExtension\ServiceContainer
18
 */
19
class DebugExtension implements Extension
20
{
21
    const TAG = 'debug';
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 20
    public function getConfigKey()
27
    {
28 20
        return static::TAG;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 4
    public function initialize(ExtensionManager $extensionManager)
35
    {
36 4
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 4
    public function load(ContainerBuilder $container, array $config)
42
    {
43 4
        $this->definition($container, 'EventSubscriber', EventDispatcherExtension::SUBSCRIBER_TAG);
44 4
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 4
    public function process(ContainerBuilder $container)
50
    {
51 4
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 4
    public function configure(ArrayNodeDefinition $builder)
57
    {
58 4
    }
59
60
    /**
61
     * Define a new service in DI container.
62
     *
63
     * @param ContainerBuilder $container
64
     *   DI container.
65
     * @param string $class
66
     *   Class in namespace of extension.
67
     * @param string $tag
68
     *   Tag of the definition.
69
     * @param array $arguments
70
     *   Dependency arguments.
71
     *
72
     * @return Definition
73
     *   Tagged definition.
74
     */
75 4
    private function definition(ContainerBuilder $container, $class, $tag, array $arguments = [])
76
    {
77 4
        $definition = new Definition(strtr(__NAMESPACE__, ['ServiceContainer' => $class]), $arguments);
78
79
        return $container
80 4
            ->setDefinition($tag . '.' . self::id($class), $definition)
81 4
            ->addTag($tag);
82
    }
83
84
    /**
85
     * Transform name of class to ID of service.
86
     *
87
     * @example
88
     * Behat\DebugExtension\Debugger => behat.debug.extension.debugger
89
     *
90
     * @param string $className
91
     *   Name of class to transform.
92
     *
93
     * @return string
94
     *   Transformed string.
95
     */
96 4
    private static function id($className)
97
    {
98
        // 1. Remove all backslashes from a string.
99
        // 2. Split string to array by a capital letter.
100
        // 3. Transform an array to string. Elements are separated by dot.
101
        // 4. Remove first dot from a left side.
102
        // 5. Transform string to a lowercase.
103 4
        return strtolower(ltrim(implode('.', preg_split('/(?=[A-Z])/', str_replace('\\', '', $className))), '.'));
104
    }
105
}
106