Code Duplication    Length = 34-36 lines in 2 locations

src/Factory/Generator/KeyValueStore/PhpRedisStoreGenerator.php 1 location

@@ 27-60 (lines=34) @@
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class PhpRedisStoreGenerator implements ServiceGenerator
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('Redis'),
49
            new Import('Webmozart\KeyValueStore\PhpRedisStore'),
50
        ));
51
52
        $targetMethod->addBody(
53
<<<EOF
54
\$client = new Redis();
55
\$client->connect($escHost, $escPort);
56
\$$varName = new PhpRedisStore(\$client);
57
EOF
58
        );
59
    }
60
}
61

src/Factory/Generator/KeyValueStore/PredisStoreGenerator.php 1 location

@@ 27-62 (lines=36) @@
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class PredisStoreGenerator implements ServiceGenerator
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