Completed
Push — master ( 4b4114...730efc )
by Gaetano
07:30
created

EzXmlText::createValue()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 11

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 0
Metric Value
dl 25
loc 25
ccs 5
cts 5
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 11
nc 4
nop 2
crap 5
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\ComplexField;
4
5
use Kaliop\eZMigrationBundle\API\ComplexFieldInterface;
6
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher;
7
use Kaliop\eZMigrationBundle\Core\Matcher\LocationMatcher;
8
9 View Code Duplication
class EzXmlText extends AbstractComplexField implements ComplexFieldInterface
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    protected $contentMatcher;
12
    protected $locationMatcher;
13
14
    public function __construct(ContentMatcher $contentMatcher, LocationMatcher $locationMatcher)
15
    {
16 1
        $this->contentMatcher = $contentMatcher;
17
        $this->locationMatcher = $locationMatcher;
18 1
    }
19
20
    /**
21
     * Replace any references in an xml string to be used as the input data for an ezxmltext field.
22
     *
23
     * @param string|array $fieldValue The definition of teh field value, structured in the yml file. Either a string, or an array with key 'content'
24
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
25 1
     * @return string
26
     *
27 1
     * @todo replace objects and location refs in eznode and ezobject links
28 1
     */
29 1
    public function createValue($fieldValue, array $context = array())
30
    {
31 1
        if (is_string($fieldValue)) {
32 1
            $xmlText = $fieldValue;
33 1
        } else {
34
            $xmlText = $fieldValue['content'];
35 1
        }
36
37
        /// @todo this regexp belongs to the resolver...
38
39
        //Check if there are any references in the xml text and replace them.
40
        // $result[0][] will have the matched full string eg.: [reference:example_reference]
41
        // $result[1][] will have the reference id eg.: reference:example_reference
42
        $count = preg_match_all('|\[(reference:[^\]\[]*)\]|', $xmlText, $result);
43
44
        if ($count !== false && count($result) > 1) {
45
            foreach ($result[1] as $index => $referenceIdentifier) {
46
                $reference = $this->referenceResolver->getReferenceValue($referenceIdentifier);
47
48
                $xmlText = str_replace($result[0][$index], $reference, $xmlText);
49
            }
50
        }
51
52
        return $xmlText;
53
    }
54
}
55