Completed
Push — master ( 2c86a7...690ed0 )
by
unknown
36:26 queued 28:50
created

ViewReferenceManager::saveReferences()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 10
nc 10
nop 4
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\Transformer\ArrayToBusinessPageReferenceTransformer;
8
use Victoire\Bundle\ViewReferenceBundle\Transformer\ArrayToViewReferenceTransformer;
9
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
10
11
/**
12
 * Class ViewReferenceManager.
13
 */
14
class ViewReferenceManager
15
{
16
    private $manager;
17
    private $repository;
18
    private $transformer;
19
20
    /**
21
     * ViewReferenceManager constructor.
22
     *
23
     * @param ViewReferenceConnectorManagerInterface    $manager
24
     * @param ViewReferenceConnectorRepositoryInterface $repository
25
     * @param ViewReferenceTransformerChain             $transformer
26
     */
27
    public function __construct(ViewReferenceConnectorManagerInterface $manager, ViewReferenceConnectorRepositoryInterface $repository, ViewReferenceTransformerChain $transformer)
28
    {
29
        $this->manager = $manager;
30
        $this->transformer = $transformer;
31
        $this->repository = $repository;
32
    }
33
34
    /**
35
     * This method save a tree of viewReferences.
36
     *
37
     * @param array  $viewReferences
38
     * @param string $parentId
39
     * @param string $parentLocale
40
     * @param bool   $reset
41
     */
42
    public function saveReferences(array $viewReferences, $parentId = null, $parentLocale = null, $reset = true)
43
    {
44
        // Reset redis if wanted
45
        if ($reset) {
46
            $this->manager->reset();
47
        }
48
49
        // Parse the viewReferences
50
        foreach ($viewReferences as $viewReference) {
51
            /** @var View $view */
52
            $view = $viewReference['view'];
53
            foreach ($view->getReferences() as $locale => $reference) {
54
                if ($reference !== null) {
55
                    // save the viewReference
56
                    $id = $this->saveReference($reference, $parentId, $parentLocale);
57
                    // if children, save them
58
                    if (array_key_exists('children', $viewReference) && !empty($children = $viewReference['children'])) {
59
                        $this->saveReferences($children, $id, $reference->getLocale(), false);
60
                    }
61
                }
62
            }
63
        }
64
    }
65
66
    /**
67
     * This method save a Reference.
68
     *
69
     * @param ViewReference $viewReference
70
     * @param string        $parentId
71
     * @param string        $parentLocale
72
     *
73
     * @return mixed
74
     */
75
    public function saveReference(ViewReference $viewReference, $parentId = null, $parentLocale = null)
76
    {
77
        // Transform the viewReference in array
78
        $arrayTransformer = $this->transformer->getViewReferenceTransformer(
79
            $viewReference->getViewNamespace(), 'array'
80
        );
81
        $referenceArray = $arrayTransformer->reverseTransform($viewReference);
82
        // Remove old url if exist
83
        $this->removeUrlForViewReference($viewReference);
84
        // Update/create the viewReference
85
        $this->manager->update($referenceArray['id'], $referenceArray);
86
        // Build the url for reference
87
        $this->manager->buildUrl($viewReference->getId());
88
        // Set parent if exist
89
        if ($parentId && $parentLocale === $viewReference->getLocale()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parentId of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
90
            $this->manager->addChild($parentId, $referenceArray['id']);
91
        }
92
93
        return $referenceArray['id'];
94
    }
95
96
    /**
97
     * This method remove reference for a ViewReference.
98
     *
99
     * @param ViewReference $viewReference
100
     */
101 View Code Duplication
    public function removeReference(ViewReference $viewReference)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $referenceId = $viewReference->getId();
104
        if ($url = $this->repository->findValueForId('url', $referenceId)) {
105
            // Remove url
106
            $this->manager->removeUrl($url, $viewReference->getLocale());
107
        }
108
        // Remove reference
109
        $this->manager->remove($referenceId);
110
    }
111
112
    /**
113
     * Find the transformer for an element.
114
     *
115
     * @param $element
116
     *
117
     * @return ArrayToBusinessPageReferenceTransformer|ArrayToViewReferenceTransformer
118
     */
119
    public static function findTransformerFromElement($element)
120
    {
121
        if (isset($element['entityId'])) {
122
            $viewRefTransformer = new ArrayToBusinessPageReferenceTransformer();
123
        } else {
124
            $viewRefTransformer = new ArrayToViewReferenceTransformer();
125
        }
126
127
        return $viewRefTransformer;
128
    }
129
130
    /**
131
     * Remove an url for a viewReference with his reference in redis.
132
     *
133
     * @param ViewReference $viewReference
134
     */
135 View Code Duplication
    public function removeUrlForViewReference(ViewReference $viewReference)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $id = $viewReference->getId();
138
        if ($url = $this->repository->findValueForId('url', $id)) {
139
            $this->manager->removeUrl($url, $this->repository->findValueForId('locale', $id));
140
        }
141
    }
142
}
143