Completed
Push — master ( 90ccc1...22512f )
by Kamil
35:43
created

TwigMetadataProcessor::processProperty()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 8.6737
c 1
b 0
f 0
cc 5
eloc 11
nc 5
nop 2
1
<?php
2
3
namespace Sylius\Component\Metadata\Processor;
4
5
use Sylius\Component\Metadata\Model\MetadataInterface;
6
use Twig_Environment;
7
8
/**
9
 * @author Kamil Kokot <[email protected]>
10
 */
11
final class TwigMetadataProcessor implements MetadataProcessorInterface
12
{
13
    /**
14
     * @var Twig_Environment
15
     */
16
    private $twig;
17
18
    /**
19
     * @param Twig_Environment $twig
20
     */
21
    public function __construct(Twig_Environment $twig)
22
    {
23
        $this->twig = $twig;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function process(MetadataInterface $metadata, array $options = [])
30
    {
31
        $metadata->forAll(function ($property) use ($options) {
32
            return $this->processProperty($property, $options);
33
        });
34
35
        return $metadata;
36
    }
37
38
    /**
39
     * @param mixed $property
40
     * @param array $options
41
     *
42
     * @return mixed
43
     */
44
    private function processProperty($property, array $options)
45
    {
46
        if (null === $property) {
47
            return null;
48
        }
49
50
        if ($property instanceof MetadataInterface) {
51
            $this->process($property, $options);
52
53
            return $property;
54
        }
55
56
        if (is_array($property)) {
57
            foreach ($property as $key => &$value) {
58
                $value = $this->processProperty($value, $options);
59
            }
60
61
            return $property;
62
        }
63
64
        return $this->twig->createTemplate((string) $property)->render($options);
65
    }
66
}
67