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

MetadataExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 71
rs 10
c 4
b 0
f 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFunctions() 0 7 1
A getProperty() 0 4 2
A renderProperty() 0 10 2
A getName() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Metadata\Twig;
13
14
use Sylius\Component\Metadata\Accessor\MetadataAccessorInterface;
15
use Sylius\Component\Metadata\Model\MetadataSubjectInterface;
16
use Sylius\Component\Metadata\Renderer\MetadataRendererInterface;
17
18
/**
19
 * @author Kamil Kokot <[email protected]>
20
 */
21
final class MetadataExtension extends \Twig_Extension
22
{
23
    /**
24
     * @var MetadataAccessorInterface
25
     */
26
    private $metadataAccessor;
27
28
    /**
29
     * @var MetadataRendererInterface
30
     */
31
    private $metadataRenderer;
32
33
    /**
34
     * @param MetadataAccessorInterface $metadataAccessor
35
     * @param MetadataRendererInterface $metadataRenderer
36
     */
37
    public function __construct(MetadataAccessorInterface $metadataAccessor, MetadataRendererInterface $metadataRenderer)
38
    {
39
        $this->metadataAccessor = $metadataAccessor;
40
        $this->metadataRenderer = $metadataRenderer;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getFunctions()
47
    {
48
        return [
49
            new \Twig_SimpleFunction('sylius_metadata_get', [$this, 'getProperty']),
50
            new \Twig_SimpleFunction('sylius_metadata_render', [$this, 'renderProperty']),
51
        ];
52
    }
53
54
    /**
55
     * @param MetadataSubjectInterface $metadataSubject
56
     * @param string|null $propertyPath
57
     * @param mixed|null $defaultValue
58
     *
59
     * @return mixed
60
     */
61
    public function getProperty(MetadataSubjectInterface $metadataSubject, $propertyPath = null, $defaultValue = null)
62
    {
63
        return $this->metadataAccessor->getProperty($metadataSubject, $propertyPath) ?: $defaultValue;
64
    }
65
66
    /**
67
     * @param MetadataSubjectInterface $metadataSubject
68
     * @param string|null $propertyPath
69
     * @param array $options
70
     *
71
     * @return string
72
     */
73
    public function renderProperty(MetadataSubjectInterface $metadataSubject, $propertyPath = null, array $options = [])
74
    {
75
        $metadataProperty = $this->metadataAccessor->getProperty($metadataSubject, $propertyPath);
76
77
        if (null === $metadataProperty) {
78
            return null;
79
        }
80
81
        return $this->metadataRenderer->render($metadataProperty, $options);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getName()
88
    {
89
        return 'sylius_metadata';
90
    }
91
}
92