PredisStoreGenerator::generateNewInstance()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 25
loc 25
ccs 0
cts 14
cp 0
rs 8.8571
cc 1
eloc 16
nc 1
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\KeyValueStore;
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 PredisStore}.
22
 *
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27 View Code Duplication
class PredisStoreGenerator 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
        'host' => '127.0.0.1',
31
        'port' => 6379,
32
    );
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
38
    {
39
        $options = array_replace(self::$defaultOptions, $options);
40
41
        Assert::stringNotEmpty($options['host'], 'The "host" option must be a non-empty string. Got: %s');
42
        Assert::integer($options['port'], 'The "port" option must be an integer. Got: %s');
43
44
        $escHost = var_export($options['host'], true);
45
        $escPort = var_export($options['port'], true);
46
47
        $targetMethod->getClass()->addImports(array(
48
            new Import('Predis\Client'),
49
            new Import('Webmozart\KeyValueStore\PredisStore'),
50
        ));
51
52
        $targetMethod->addBody(
53
<<<EOF
54
\$client = new Client(array(
55
    'host' => $escHost,
56
    'port' => $escPort,
57
));
58
\$$varName = new PredisStore(\$client);
59
EOF
60
        );
61
    }
62
}
63