Passed
Push — master ( d42ef0...b74a1f )
by MoshiMoshi
02:18
created

ServiceIdBuilder::setPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the ConfigCacheBundle package.
5
 *
6
 * Copyright (c) 2015-2016 Yahoo Japan Corporation
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace YahooJapan\ConfigCacheBundle\ConfigCache\Register;
13
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
use Symfony\Component\DependencyInjection\Container;
16
17
/**
18
 * ServiceIdBuilder handles service IDs to build.
19
 */
20
class ServiceIdBuilder
21
{
22
    // bundle ID (underscored string)
23
    protected $bundleId;
24
    // ConfigCache service ID prefix
25
    protected $prefix = 'config';
26
27
    /**
28
     * Builds a cache service ID.
29
     *
30
     * @param array $suffixes ex) array("yahoo_japan_config_cache")
31
     *
32
     * @return string ex) "config.yahoo_japan_config_cache"
33
     */
34 29
    public function buildId(array $suffixes)
35
    {
36 29
        return implode('.', array_merge(array($this->prefix), $suffixes));
37
    }
38
39
    /**
40
     * Builds a cache service ID with bundleId.
41
     *
42
     * @param array $suffixes ex) $suffixes = array("suffix"), $this->bundleId = "yahoo_japan_config_cache"
43
     *
44
     * @return string ex) "config.yahoo_japan_config_cache.suffix"
45
     */
46 24
    public function buildCacheId(array $suffixes = array())
47
    {
48 24
        return $this->buildId(array_merge(array($this->bundleId), $suffixes));
49
    }
50
51
    /**
52
     * Builds a configuration private service ID.
53
     *
54
     * @param ConfigurationInterface $configuration
55
     *
56
     * @return string
57
     *
58
     * @note before : Acme\DemoBundle\DependencyInjection\Configuration
59
     *       after  : acme.demo_bundle.dependency_injection.configuration
60
     */
61 20
    public function buildConfigurationId(ConfigurationInterface $configuration)
62
    {
63 20
        $reflection = new \ReflectionClass($configuration);
64 20
        $configId   = Container::underscore(strtr($reflection->getName(), '\\', '_'));
65
66 20
        return $this->buildId(array('configuration', $configId));
67
    }
68
69
    /**
70
     * Gets a bundleId.
71
     *
72
     * @return string
73
     */
74 26
    public function getBundleId()
75
    {
76 26
        return $this->bundleId;
77
    }
78
79
    /**
80
     * Sets a bundleId.
81
     *
82
     * @param string $bundleId
83
     *
84
     * @return ServiceIdBuilder
85
     */
86 34
    public function setBundleId($bundleId)
87
    {
88 34
        $this->bundleId = $bundleId;
89
90 34
        return $this;
91
    }
92
93
    /**
94
     * Gets a prefix.
95
     *
96
     * @return string
97
     */
98 25
    public function getPrefix()
99
    {
100 25
        return $this->prefix;
101
    }
102
103
    /**
104
     * Sets a prefix.
105
     *
106
     * @param string $prefix
107
     *
108
     * @return ServiceIdBuilder
109
     */
110 1
    public function setPrefix($prefix)
111
    {
112 1
        $this->prefix = $prefix;
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * Parses a service ID based on bundle name.
119
     *
120
     * @param string $name
121
     *
122
     * @return string
123
     */
124 14
    public static function parseServiceId($name)
125
    {
126 14
        return Container::underscore(preg_replace('/Bundle$/', '', $name));
127
    }
128
}
129