Code Duplication    Length = 37-38 lines in 2 locations

src/Factory/Generator/ChangeStream/KeyValueStoreChangeStreamGenerator.php 1 location

@@ 27-63 (lines=37) @@
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class KeyValueStoreChangeStreamGenerator implements ServiceGenerator
28
{
29
    private static $defaultOptions = array(
30
        'store' => array(
31
            'type' => 'json',
32
        ),
33
    );
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
39
    {
40
        Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
41
42
        $options = array_replace_recursive(self::$defaultOptions, $options);
43
44
        Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
45
        Assert::isArray($options['store'], 'The "store" option should be an array. Got: %s');
46
47
        if (!isset($options['store']['path'])) {
48
            $options['store']['path'] = $targetMethod->getClass()->getDirectory().'/change-stream.json';
49
        }
50
51
        $kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']);
52
        $kvsOptions = $options['store'];
53
        $kvsOptions['root-dir'] = $options['root-dir'];
54
        $kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions);
55
56
        $targetMethod->getClass()->addImport(new Import('Puli\Repository\ChangeStream\KeyValueStoreChangeStream'));
57
58
        $targetMethod->addBody(sprintf(
59
            '$%s = new KeyValueStoreChangeStream($store);',
60
            $varName
61
        ));
62
    }
63
}
64

src/Factory/Generator/Discovery/KeyValueStoreDiscoveryGenerator.php 1 location

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