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

ExpressionLanguageResolver::getReferenceValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\ExpressionLanguage\ExpressionFunction;
11
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
12
use eZ\Publish\Core\FieldType;
13
14
class ExpressionLanguageResolver extends AbstractResolver
15
{
16
    protected $referencePrefixes = ['@='];
17
18
    /**
19
     * @var ReferenceResolverInterface
20
     */
21
    private $referenceResolver;
22
    /**
23
     * @var ExpressionLanguage
24
     */
25
    private $expressionLanguage;
26
27
    public function __construct(ReferenceResolverInterface $referenceResolver, ExpressionLanguage $expressionLanguage)
28
    {
29
        parent::__construct();
30
        $this->referenceResolver = $referenceResolver;
31
        $this->expressionLanguage = $expressionLanguage;
32
    }
33
34
    /**
35
     * @param string $stringIdentifier
36
     * @return mixed
37
     */
38
    public function getReferenceValue($stringIdentifier)
39
    {
40
        return $this->expressionLanguage->evaluate(
41
            $this->getReferenceIdentifier($stringIdentifier)
42
        );
43
    }
44
45 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...
46
    {
47
        {
48
            $language = new ExpressionLanguage();
49
            $language->addFunction(
50
                new ExpressionFunction(
51
                    'relatedContentIds',
52
                    function() {
53
                        throw new \Exception('ref function does not support compilation');
54
                    },
55
                    function($foo, Content $content, $fieldDefinitionIdentifier) {
56
                        $fieldValue = $content->getFieldValue($fieldDefinitionIdentifier);
57
                        if ($fieldValue instanceof FieldType\Relation\Value) {
58
                            return [$fieldValue->destinationContentId];
59
                        } elseif ($fieldValue instanceof FieldType\RelationList\Value) {
60
                            return $fieldValue->destinationContentIds;
61
                        } else {
62
                            throw new \Exception("Expected a Relation or RelationList field value");
63
                        }
64
                    }
65
                )
66
            );
67
            $language->addFunction(
68
                new ExpressionFunction(
69
                    'collection_item',
70
                    function() {
71
                        throw new \Exception('ref function does not support compilation');
72
                    },
73
                    function($foo, $identifier) {
74
                        return $this->referenceResolver->getReferenceValue('reference:collection_item_' . $identifier);
75
                    }
76
                )
77
            );
78
79
            return $language;
80
        }
81
    }
82
}
83