Completed
Push — master ( 7f3713...076dd4 )
by Bernhard
10:39
created

generateNewInstance()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 26
loc 26
ccs 18
cts 18
cp 1
rs 8.8571
cc 2
eloc 16
nc 2
nop 4
crap 2
1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
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 Puli\Manager\Factory\Generator\Discovery;
13
14
use Puli\Manager\Api\Factory\Generator\GeneratorRegistry;
15
use Puli\Manager\Api\Factory\Generator\ServiceGenerator;
16
use Puli\Manager\Api\Php\Import;
17
use Puli\Manager\Api\Php\Method;
18
use Puli\Manager\Assert\Assert;
19
20
/**
21
 * Generates the setup code for a {@link KeyValueStoreDiscovery}.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27 View Code Duplication
class KeyValueStoreDiscoveryGenerator implements ServiceGenerator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
{
29
    private static $defaultOptions = array(
30
        'store' => array(
31
            'type' => 'json',
32
        ),
33
    );
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 6
    public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
39
    {
40 6
        Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
41
42 5
        $options = array_replace_recursive(self::$defaultOptions, $options);
43
44 5
        Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
45 4
        Assert::isArray($options['store'], 'The "store" option should be an array. Got: %s');
46
47 3
        if (!isset($options['store']['path'])) {
48 3
            $options['store']['path'] = $targetMethod->getClass()->getDirectory().'/bindings.json';
49 3
        }
50
51 3
        $kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']);
52 3
        $kvsOptions = $options['store'];
53 3
        $kvsOptions['root-dir'] = $options['root-dir'];
54 3
        $kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions);
55
56 3
        $targetMethod->getClass()->addImport(new Import('Puli\Discovery\KeyValueStoreDiscovery'));
57 3
        $targetMethod->getClass()->addImport(new Import('Puli\Discovery\Binding\Initializer\ResourceBindingInitializer'));
58
59 3
        $targetMethod->addBody(sprintf(
60 3
            "$%s = new KeyValueStoreDiscovery(\$store, array(\n    new ResourceBindingInitializer(\$repo),\n));",
61
            $varName
62 3
        ));
63 3
    }
64
}
65