Passed
Branch master (a0cc06)
by Dāvis
17:06
created

ObjectPositionExtension   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A __construct() 0 3 1
A getFunctions() 0 7 1
1
<?php
2
3
namespace Sludio\HelperBundle\Position\Twig;
4
5
use Sludio\HelperBundle\Position\Service\PositionHandler;
6
use Twig_Extension;
7
use Twig_SimpleFunction;
8
9
class ObjectPositionExtension extends Twig_Extension
10
{
11
    const NAME = 'sludio_position_object';
12
13
    /**
14
     * PositionHandler.
15
     */
16
    private $positionService;
17
18
    public function __construct(PositionHandler $positionService)
19
    {
20
        $this->positionService = $positionService;
21
    }
22
23
    /**
24
     * Returns the name of the extension.
25
     *
26
     * @return string The extension name
27
     */
28
    public function getName()
29
    {
30
        return self::NAME;
31
    }
32
33
    public function getFunctions()
34
    {
35
        return [
36
            new Twig_SimpleFunction(self::NAME, function ($entity) {
37
                $getter = sprintf('get%s', ucfirst($this->positionService->getPositionFieldByEntity($entity)));
38
39
                return $entity->{$getter}();
40
            }),
41
        ];
42
    }
43
}
44