RegisterFactory::createServiceRegister()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 5
nop 0
crap 4
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\DependencyInjection\ContainerBuilder;
15
16
/**
17
 * RegisterFactory creates register objects.
18
 */
19
class RegisterFactory
20
{
21
    protected $container;
22
    protected $idBuilder;
23
    protected $configuration;
24
    protected $serviceRegister;
25
    protected $serviceRegisterClass = 'YahooJapan\ConfigCacheBundle\ConfigCache\Register\ServiceRegister';
26
27
    /**
28
     * Sets a ContainerBuilder.
29
     *
30
     * @param ContainerBuilder $container
31
     *
32
     * @return RegisterFactory
33
     */
34 15
    public function setContainer(ContainerBuilder $container)
35
    {
36 15
        $this->container = $container;
37
38 15
        return $this;
39
    }
40
41
    /**
42
     * Creates a service ID builder.
43
     *
44
     * @return ServiceIdBuilder
45
     */
46 18
    public function createIdBuilder()
47
    {
48 18
        return $this->idBuilder = new ServiceIdBuilder();
49
    }
50
51
    /**
52
     * Creates a ConfigurationRegister.
53
     *
54
     * @return ConfigurationRegister
55
     */
56 18
    public function createConfigurationRegister()
57
    {
58 18
        return $this->configuration = new ConfigurationRegister();
59
    }
60
61
    /**
62
     * Creates a ServiceRegister.
63
     *
64
     * @return ServiceRegister
65
     *
66
     * @throws \RuntimeException if $this->container is not set
67
     */
68 17
    public function createServiceRegister()
69
    {
70 17
        if (is_null($this->container)) {
71 2
            throw new \RuntimeException('ContainerBuilder must be set.');
72
        }
73 15
        $idBuilder     = $this->idBuilder ?: $this->createIdBuilder();
74 15
        $configuration = $this->configuration ?: $this->createConfigurationRegister();
75
76 15
        return $this->serviceRegister = new $this->serviceRegisterClass($this->container, $idBuilder, $configuration);
77
    }
78
79
    /**
80
     * Creates a FileRegister.
81
     *
82
     * @return FileRegister
83
     */
84 13
    public function createFileRegister()
85
    {
86 13
        return new FileRegister($this->serviceRegister ?: $this->createServiceRegister());
87
    }
88
89
    /**
90
     * Creates a DirectoryRegister.
91
     *
92
     * @return DirectoryRegister
93
     */
94 13
    public function createDirectoryRegister()
95
    {
96 13
        return new DirectoryRegister($this->serviceRegister ?: $this->createServiceRegister());
97
    }
98
}
99