Passed
Push — master ( 960a24...ce8b6f )
by Gerhard
20:20 queued 10:00
created

UpdateViewer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 9.57 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 9
loc 94
ccs 0
cts 72
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 4 1
A buildTemplateParameters() 0 37 2
A createActionsSecondary() 0 27 1
A getResourceData() 9 9 3
A configureOptions() 0 9 1

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
 * UpdateViewer.php
4
 *
5
 * @since 29/05/15
6
 * @author gseidel
7
 */
8
9
namespace Enhavo\Bundle\AppBundle\Viewer\Viewer;
10
11
use Enhavo\Bundle\AppBundle\Controller\RequestConfiguration;
12
use Sylius\Component\Resource\Metadata\MetadataInterface;
13
use Sylius\Component\Resource\Model\ResourceInterface;
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
class UpdateViewer extends CreateViewer
18
{
19
    public function getType()
20
    {
21
        return 'update';
22
    }
23
24
    protected function buildTemplateParameters(ParameterBag $parameters, RequestConfiguration $requestConfiguration, array $options)
25
    {
26
        parent::buildTemplateParameters($parameters, $requestConfiguration, $options);
27
28
        /** @var MetadataInterface $metadata */
29
        $metadata = $options['metadata'];
30
        /** @var ResourceInterface $resource */
31
        $resource = $options['resource'];
32
33
        $parameters->set('form_action', $this->mergeConfig([
34
            sprintf('%s_%s_update', $metadata->getApplicationName(), $this->getUnderscoreName($metadata)),
35
            $options['form_action'],
36
            $this->getViewerOption('form.action', $requestConfiguration)
37
        ]));
38
39
        $parameters->set('form_action_parameters', $this->mergeConfigArray([
40
            [ 'id' => $resource->getId() ],
41
            $options['form_action_parameters'],
42
            $this->getViewerOption('form.action_parameters', $requestConfiguration)
43
        ]));
44
45
        $actionsSecondary = $this->mergeConfigArray([
46
            $this->createActionsSecondary($options, $requestConfiguration, $resource),
47
            $options['actions_secondary'],
48
            $this->getViewerOption('actions_secondary', $requestConfiguration)
49
        ]);
50
51
        $data = $parameters->get('data');
52
        $data['actionsSecondary'] = $this->actionManager->createActionsViewData($actionsSecondary, $options['resource']);
53
54
        $resourceData = $this->getResourceData($requestConfiguration, $options);
55
        if ($resourceData) {
56
            $data['resource'] = $resourceData;
57
        }
58
59
        $parameters->set('data', $data);
60
    }
61
62
    private function createActionsSecondary($options, $requestConfiguration, $resource)
0 ignored issues
show
Unused Code introduced by
The parameter $resource is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        /** @var MetadataInterface $metadata */
65
        $metadata = $options['metadata'];
66
67
        $formDelete = $this->mergeConfig([
68
            sprintf('%s_%s_delete', $metadata->getApplicationName(), $this->getUnderscoreName($metadata)),
69
            $options['form_delete'],
70
            $this->getViewerOption('form.delete', $requestConfiguration)
71
        ]);
72
73
        $formDeleteParameters = $this->mergeConfigArray([
74
            $options['form_delete_parameters'],
75
            $this->getViewerOption('form.delete_parameters', $requestConfiguration)
76
        ]);
77
78
        $default = [
79
            'delete' => [
80
                'type' => 'delete',
81
                'route' => $formDelete,
82
                'route_parameters' => $formDeleteParameters,
83
                'permission' => $this->getRoleNameByResourceName($metadata->getApplicationName(), $this->getUnderscoreName($metadata), 'delete')
84
            ]
85
        ];
86
87
        return $default;
88
    }
89
90 View Code Duplication
    private function getResourceData(RequestConfiguration $requestConfiguration, array $options)
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...
91
    {
92
        $resourceData = null;
93
        $serializationGroups = $requestConfiguration->getSerializationGroups();
94
        if($serializationGroups && $options['resource']) {
95
            $resourceData = $this->container->get('serializer')->normalize($options['resource'], null, ['groups' => $serializationGroups]);
96
        }
97
        return $resourceData;
98
    }
99
100
101
    public function configureOptions(OptionsResolver $optionsResolver)
102
    {
103
        parent::configureOptions($optionsResolver);
104
        $optionsResolver->setDefaults([
105
            'form_delete' => null,
106
            'form_delete_parameters' => [],
107
            'label' => 'label.edit'
108
        ]);
109
    }
110
}
111