RiakStoreGenerator::generateNewInstance()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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