ViewLister   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 91
ccs 26
cts 26
cp 1
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A buildList() 0 9 2
A generateView() 0 3 1
A __construct() 0 4 1
A setOptions() 0 5 1
A getView() 0 3 1
A getOptions() 0 18 4
1
<?php
2
3
namespace Povs\ListerTwigBundle\Service;
4
5
use Povs\ListerBundle\Declaration\ListerInterface;
6
use Povs\ListerBundle\Service\ListManager;
7
use Povs\ListerTwigBundle\Declaration\ViewListerInterface;
8
use Symfony\Component\HttpFoundation\Response;
9
use Povs\ListerBundle\Service\Lister;
10
11
/**
12
 * @author Povilas Margaiatis <[email protected]>
13
 */
14
class ViewLister extends Lister implements ViewListerInterface
15
{
16
    /**
17
     * @var TypeResolver
18
     */
19
    private $typeResolver;
20
21
    /**
22
     * @var string
23
     */
24
    private $type;
25
26
    /**
27
     * @var array
28
     */
29
    private $typeOptions = [];
30
31
    /**
32
     * @param ListManager  $listManager
33
     * @param TypeResolver $typeResolver
34
     */
35 5
    public function __construct(ListManager $listManager, TypeResolver $typeResolver)
36
    {
37 5
        parent::__construct($listManager);
38 5
        $this->typeResolver = $typeResolver;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44 1
    public function setOptions(string $type, array $options): ViewListerInterface
45
    {
46 1
        $this->typeOptions[$type] = $options;
47
48 1
        return $this;
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54 4
    public function buildList(string $list, ?string $type = null, array $parameters = []): ListerInterface
55
    {
56 4
        if (!$type) {
57 2
            $type = $this->typeResolver->resolveType();
58
        }
59
60 4
        $this->type = $type;
61
62 4
        return parent::buildList($list, $type, $parameters);
63
    }
64
65
    /**
66
     * @inheritDoc
67
     */
68 4
    public function generateView(string $template, array $parameters = []): Response
69
    {
70 4
        return $this->generateResponse($this->getOptions($template, $parameters));
71
    }
72
73
    /**
74
     * @inheritDoc
75
     */
76 1
    public function getView(string $template, array $parameters = []): string
77
    {
78 1
        return $this->generateData($this->getOptions($template, $parameters));
79
    }
80
81
    /**
82
     * @param string $template
83
     * @param array  $parameters
84
     *
85
     * @return array
86
     */
87 5
    private function getOptions(string $template, array $parameters = []): array
88
    {
89 5
        if (!$this->type) {
90 1
            return [];
91
        }
92
93 4
        if (array_key_exists($this->type, $this->typeOptions)) {
94 1
            return $this->typeOptions[$this->type];
95
        }
96
97 3
        if ($this->typeResolver->isViewType($this->type)) {
98 2
            return [
99 2
                'template' => $template,
100 2
                'context' => $parameters
101 2
            ];
102
        }
103
104 1
        return [];
105
    }
106
}
107