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

ExpressionLanguageResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 53.62 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 7
dl 37
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getReferenceValue() 0 6 1
B getExpressionLanguage() 37 37 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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