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

ViewReferenceManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 13
c 2
b 1
f 0
lcom 1
cbo 7
dl 16
loc 120
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A removeReference() 9 9 1
A removeUrlForViewReference() 7 7 2
B saveReferences() 0 17 5
A saveReference() 0 20 2
A findTransformerFromElement() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Connector;
4
5
use Victoire\Bundle\ViewReferenceBundle\Builder\Chain\ViewReferenceTransformerChain;
6
use Victoire\Bundle\ViewReferenceBundle\Transformer\ArrayToBusinessPageReferenceTransformer;
7
use Victoire\Bundle\ViewReferenceBundle\Transformer\ArrayToViewReferenceTransformer;
8
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
9
10
/**
11
 * Class ViewReferenceManager.
12
 */
13
class ViewReferenceManager
14
{
15
    private $manager;
16
    private $repository;
17
    private $transformer;
18
19
    /**
20
     * ViewReferenceManager constructor.
21
     *
22
     * @param ViewReferenceConnectorManagerInterface    $manager
23
     * @param ViewReferenceConnectorRepositoryInterface $repository
24
     * @param ViewReferenceTransformerChain             $transformer
25
     */
26
    public function __construct(ViewReferenceConnectorManagerInterface $manager, ViewReferenceConnectorRepositoryInterface $repository, ViewReferenceTransformerChain $transformer)
27
    {
28
        $this->manager = $manager;
29
        $this->transformer = $transformer;
30
        $this->repository = $repository;
31
    }
32
33
    /**
34
     * This method save a tree of viewReferences.
35
     *
36
     * @param array     $viewReferences
37
     * @param null      $parentId
38
     * @param bool|true $reset
39
     */
40
    public function saveReferences(array $viewReferences, $parentId = null, $reset = true)
41
    {
42
        // Reset redis if wanted
43
        if ($reset) {
44
            $this->manager->reset();
45
        }
46
        // Parse the viewReferences
47
        foreach ($viewReferences as $viewReference) {
48
            $reference = $viewReference['view']->getReference();
49
            // save the viewReference
50
            $id = $this->saveReference($reference, $parentId);
51
            // if children save them
52
            if (array_key_exists('children', $viewReference) && !empty($children = $viewReference['children'])) {
53
                $this->saveReferences($children, $id, false);
54
            }
55
        }
56
    }
57
58
    /**
59
     * This method save a Reference.
60
     *
61
     * @param ViewReference $viewReference
62
     * @param null          $parentId
63
     *
64
     * @return mixed
65
     */
66
    public function saveReference(ViewReference $viewReference, $parentId = null)
67
    {
68
        // Transform the viewReference in array
69
        $arrayTransformer = $this->transformer->getViewReferenceTransformer(
70
            $viewReference->getViewNamespace(), 'array'
71
        );
72
        $referenceArray = $arrayTransformer->reverseTransform($viewReference);
73
        // Remove old url if exist
74
        $this->removeUrlForViewReference($viewReference);
75
        // Update/create the viewReference
76
        $this->manager->update($referenceArray['id'], $referenceArray);
77
        // Build the url for reference
78
        $this->manager->buildUrl($viewReference->getId());
79
        // Set parent if exist
80
        if ($parentId) {
81
            $this->manager->addChild($parentId, $referenceArray['id']);
82
        }
83
84
        return $referenceArray['id'];
85
    }
86
87
    /**
88
     * This method remove reference for a ViewReference.
89
     *
90
     * @param ViewReference $viewReference
91
     */
92 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...
93
    {
94
        $referenceId = $viewReference->getId();
95
        $url = $this->repository->findValueForId('url', $referenceId);
96
        // Remove url
97
        $this->manager->removeUrl($url, $viewReference->getLocale());
98
        // Remove reference
99
        $this->manager->remove($referenceId);
100
    }
101
102
    /**
103
     * Find the transformer for an element.
104
     *
105
     * @param $element
106
     *
107
     * @return ArrayToBusinessPageReferenceTransformer|ArrayToViewReferenceTransformer
108
     */
109
    public static function findTransformerFromElement($element)
110
    {
111
        if (isset($element['entityId'])) {
112
            $viewRefTransformer = new ArrayToBusinessPageReferenceTransformer();
113
        } else {
114
            $viewRefTransformer = new ArrayToViewReferenceTransformer();
115
        }
116
117
        return $viewRefTransformer;
118
    }
119
120
    /**
121
     * Remove an url for a viewReference with his reference in redis.
122
     *
123
     * @param ViewReference $viewReference
124
     */
125 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...
126
    {
127
        $id = $viewReference->getId();
128
        if ($url = $this->repository->findValueForId('url', $id)) {
129
            $this->manager->removeUrl($url, $this->repository->findValueForId('locale', $id));
130
        }
131
    }
132
}
133