Completed
Push — master ( 7a257e...017c35 )
by Leny
109:58 queued 102:36
created

ViewReferenceRepository   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A findReferenceByView() 0 7 1
A hasReference() 0 8 2
A getReferenceByUrl() 0 18 2
B getReferencesByParameters() 0 27 5
A getOneReferenceByParameters() 0 7 2
B getChoices() 0 40 6
1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Connector;
4
5
use Victoire\Bundle\CoreBundle\Entity\View;
6
use Victoire\Bundle\ViewReferenceBundle\Builder\Chain\ViewReferenceTransformerChain;
7
use Victoire\Bundle\ViewReferenceBundle\Helper\ViewReferenceHelper;
8
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
9
10
/**
11
 * Class ViewReferenceRepository.
12
 */
13
class ViewReferenceRepository
14
{
15
    private $repository;
16
    private $transformer;
17
18
    /**
19
     * ViewReferenceManager constructor.
20
     *
21
     * @param ViewReferenceConnectorRepositoryInterface $repository
22
     * @param ViewReferenceTransformerChain             $transformer
23
     */
24
    public function __construct(ViewReferenceConnectorRepositoryInterface $repository, ViewReferenceTransformerChain $transformer)
25
    {
26
        $this->repository = $repository;
27
        $this->transformer = $transformer;
28
    }
29
30
    /**
31
     * This method return a ViewReference for a View.
32
     *
33
     * @param View $view
34
     *
35
     * @throws \Exception
36
     *
37
     * @return ViewReference
38
     */
39
    public function findReferenceByView(View $view)
40
    {
41
        $referenceId = ViewReferenceHelper::generateViewReferenceId($view);
42
        $reference = $this->getOneReferenceByParameters($referenceId, false);
43
44
        return $reference;
45
    }
46
47
    /**
48
     * This method return a ViewReference fo an url/locale.
49
     *
50
     * @param $url
51
     * @param $locale
52
     *
53
     * @return mixed|null
54
     */
55
    public function getReferenceByUrl($url, $locale)
56
    {
57
        // Find the ref id for an url/locale
58
        $refId = $this->repository->findRefIdByUrl($url, $locale);
59
        if (!$refId) {
60
            return;
61
        }
62
        // Get the reference
63
        $ref = $this->repository->findById($refId);
64
        // Transform the reference into a viewReference
65
        $transformer = $this->transformer->getViewReferenceTransformer(
66
            (string) $ref['viewNamespace'], 'array'
67
        );
68
69
        $viewReference = $transformer->transform($ref);
70
71
        return $viewReference;
72
    }
73
74
    /**
75
     * Get references matching with parameters.
76
     *
77
     * @param $parameters
78
     * @param bool|true  $transform
79
     * @param bool|false $keepChildren
80
     *
81
     * @return array
82
     */
83
    public function getReferencesByParameters($parameters, $transform = true, $keepChildren = false)
84
    {
85
        $viewsReferences = [];
86
        $refsId = $this->repository->getAllBy($parameters);
87
88
        $references = $this->repository->getResults($refsId);
89
        foreach ($references as $reference) {
90
            if ($transform === true) {
91
                $transformViewReferenceFn = function ($parentViewReference) use (&$transformViewReferenceFn, $keepChildren) {
92
                    $transformer = ViewReferenceManager::findTransformerFromElement($parentViewReference);
93
                    $reference = $transformer->transform($parentViewReference);
94
                    if ($keepChildren) {
95
                        foreach ($this->repository->getChildren($parentViewReference->getId()) as $child) {
96
                            $reference->addChild($transformViewReferenceFn($this->repository->findById($child)));
97
                        }
98
                    }
99
100
                    return $reference;
101
                };
102
103
                $reference = $transformViewReferenceFn($reference);
104
            }
105
            $viewsReferences[] = $reference;
106
        }
107
108
        return $viewsReferences;
109
    }
110
111
    /**
112
     * Get first reference matching with parameters.
113
     *
114
     * @param $parameters
115
     * @param bool|true  $transform
116
     * @param bool|false $keepChildren
117
     *
118
     * @return mixed
119
     */
120
    public function getOneReferenceByParameters($parameters, $transform = true, $keepChildren = false)
121
    {
122
        $result = $this->getReferencesByParameters($parameters, $transform, $keepChildren);
123
        if (count($result)) {
124
            return $result[0];
125
        }
126
    }
127
128
    /**
129
     * Check if redis has a reference.
130
     *
131
     * @return bool
132
     */
133
    public function hasReference()
134
    {
135
        if (count($this->repository->getAll())) {
136
            return true;
137
        }
138
139
        return false;
140
    }
141
142
    /**
143
     * get ViewsReferences ordered byhierarchy with some prefix.
144
     *
145
     * @param null $refId
146
     * @param int  $depth
147
     *
148
     * @return array
149
     */
150
    public function getChoices($refId = null, $depth = 0)
151
    {
152
        $viewsReferences = [];
153
154
        $prefixFn = function ($depth, $char0 = '└', $char = '─') {
155
            $prefix = $char0;
156
            for ($i = 0; $i <= $depth; $i++) {
157
                $prefix .= $char;
158
            }
159
160
            return $prefix;
161
        };
162
163
        if (null === $refId) {
164
            $refsId = $this->repository->getAllBy([
165
                'slug' => '',
166
            ]);
167
        } else {
168
            $refsId = $this->repository->getAllBy([
169
                'parent' => $refId,
170
            ]);
171
        }
172
        $refs = $this->repository->getResults($refsId);
173
174
        foreach ($refs as $reference) {
175
            $viewReferenceTransformer = ViewReferenceManager::findTransformerFromElement($reference);
176
            $viewReference = $viewReferenceTransformer->transform($reference);
177
            if ($viewReference->getName() != '') {
178
                $prefix = '';
179
                if ($depth > 0) {
180
                    $prefix = $prefixFn($depth).' ';
181
                }
182
                $viewsReferences[$viewReference->getId()] = $prefix.$viewReference->getName();
183
            }
184
185
            $viewsReferences = array_merge($viewsReferences, $this->getChoices($viewReference->getId(), $depth + 1));
1 ignored issue
show
Documentation introduced by
$viewReference->getId() is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
186
        }
187
188
        return $viewsReferences;
189
    }
190
}
191