Completed
Pull Request — master (#81)
by
unknown
28:56
created

getExpressionLanguage()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 37
Code Lines 23

Duplication

Lines 37
Ratio 100 %

Importance

Changes 0
Metric Value
dl 37
loc 37
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver;
7
8
use eZ\Publish\API\Repository\Values\Content\Content;
9
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
12
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
13
use eZ\Publish\Core\FieldType;
14
15
class ContainerParameterResolver extends AbstractResolver
16
{
17
    protected $referencePrefixes = ['%'];
18
19
    /**
20
     * @var ContainerInterface
21
     */
22
    private $container;
23
24
    public function __construct(ContainerInterface $container)
25
    {
26
        parent::__construct();
27
        $this->container = $container;
28
    }
29
30
    /**
31
     * @param string $stringIdentifier
32
     * @return mixed
33
     */
34
    public function getReferenceValue($stringIdentifier)
35
    {
36
        return $this->container->getParameter(trim($stringIdentifier, '%'));
37
    }
38
39 View Code Duplication
    private function getExpressionLanguage()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method 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...
40
    {
41
        {
42
            $language = new ExpressionLanguage();
43
            $language->addFunction(
44
                new ExpressionFunction(
45
                    'relatedContentIds',
46
                    function() {
47
                        throw new \Exception('ref function does not support compilation');
48
                    },
49
                    function($foo, Content $content, $fieldDefinitionIdentifier) {
50
                        $fieldValue = $content->getFieldValue($fieldDefinitionIdentifier);
51
                        if ($fieldValue instanceof FieldType\Relation\Value) {
52
                            return [$fieldValue->destinationContentId];
53
                        } elseif ($fieldValue instanceof FieldType\RelationList\Value) {
54
                            return $fieldValue->destinationContentIds;
55
                        } else {
56
                            throw new \Exception("Expected a Relation or RelationList field value");
57
                        }
58
                    }
59
                )
60
            );
61
            $language->addFunction(
62
                new ExpressionFunction(
63
                    'collection_item',
64
                    function() {
65
                        throw new \Exception('ref function does not support compilation');
66
                    },
67
                    function($foo, $identifier) {
68
                        return $this->referenceResolver->getReferenceValue('reference:collection_item_' . $identifier);
0 ignored issues
show
Bug introduced by
The property referenceResolver does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
69
                    }
70
                )
71
            );
72
73
            return $language;
74
        }
75
    }
76
}
77