Code Duplication    Length = 30-31 lines in 2 locations

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

@@ 28-57 (lines=30) @@
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class JsonChangeStreamGenerator implements ServiceGenerator
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
34
    {
35
        Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
36
37
        if (!isset($options['path'])) {
38
            $options['path'] = $targetMethod->getClass()->getDirectory().'/change-stream.json';
39
        }
40
41
        Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
42
        Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s');
43
44
        $path = Path::makeAbsolute($options['path'], $options['root-dir']);
45
        $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
46
47
        $escPath = '__DIR__.'.var_export('/'.$relPath, true);
48
49
        $targetMethod->getClass()->addImport(new Import('Puli\Repository\ChangeStream\JsonChangeStream'));
50
51
        $targetMethod->addBody(sprintf(
52
            '$%s = new JsonChangeStream(%s);',
53
            $varName,
54
            $escPath
55
        ));
56
    }
57
}
58

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

@@ 28-58 (lines=31) @@
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class JsonDiscoveryGenerator implements ServiceGenerator
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
34
    {
35
        Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
36
37
        if (!isset($options['path'])) {
38
            $options['path'] = $targetMethod->getClass()->getDirectory().'/bindings.json';
39
        }
40
41
        Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
42
        Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s');
43
44
        $path = Path::makeAbsolute($options['path'], $options['root-dir']);
45
        $relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
46
47
        $escPath = '__DIR__.'.var_export('/'.$relPath, true);
48
49
        $targetMethod->getClass()->addImport(new Import('Puli\Discovery\JsonDiscovery'));
50
        $targetMethod->getClass()->addImport(new Import('Puli\Discovery\Binding\Initializer\ResourceBindingInitializer'));
51
52
        $targetMethod->addBody(sprintf(
53
            "$%s = new JsonDiscovery(%s, array(\n    new ResourceBindingInitializer(\$repo),\n));",
54
            $varName,
55
            $escPath
56
        ));
57
    }
58
}
59