MassEditAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4285
cc 1
eloc 13
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Pim\Bundle\CustomEntityBundle\Action;
4
5
use Pim\Bundle\CustomEntityBundle\Event\ActionEventManager;
6
use Pim\Bundle\CustomEntityBundle\Manager\Registry as ManagerRegistry;
7
use Pim\Bundle\CustomEntityBundle\MassAction\MassUpdater;
8
use Pim\Bundle\DataGridBundle\Extension\MassAction\MassActionDispatcher;
9
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
10
use Symfony\Component\Form\FormFactoryInterface;
11
use Symfony\Component\Form\FormInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
use Symfony\Component\Routing\RouterInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
17
/**
18
 * @author    Antoine Guigan <[email protected]>
19
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
20
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
21
 */
22
class MassEditAction extends CreateAction
23
{
24
    /**
25
     * @var MassActionDispatcher
26
     */
27
    protected $massActionDispatcher;
28
29
    /**
30
     * @var MassUpdater
31
     */
32
    protected $massUpdater;
33
34
    /**
35
     * @param ActionFactory          $actionFactory
36
     * @param ActionEventManager     $eventManager
37
     * @param ManagerRegistry        $manager
38
     * @param RouterInterface        $router
39
     * @param TranslatorInterface    $translator
40
     * @param EngineInterface        $templating
41
     * @param FormFactoryInterface   $formFactory
42
     * @param MassActionDispatcher   $massActionDispatcher
43
     * @param MassUpdater            $massUpdater
44
     */
45
    public function __construct(
46
        ActionFactory $actionFactory,
47
        ActionEventManager $eventManager,
48
        ManagerRegistry $manager,
49
        RouterInterface $router,
50
        TranslatorInterface $translator,
51
        EngineInterface $templating,
52
        FormFactoryInterface $formFactory,
53
        MassActionDispatcher $massActionDispatcher,
54
        MassUpdater $massUpdater
55
    ) {
56
        parent::__construct($actionFactory, $eventManager, $manager, $router, $translator, $templating, $formFactory);
57
58
        $this->massActionDispatcher = $massActionDispatcher;
59
        $this->massUpdater          = $massUpdater;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getType()
66
    {
67
        return 'mass_edit';
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function saveForm(Request $request, FormInterface $form)
74
    {
75
        $this->massUpdater->updateEntities(
76
            $this->configuration->getEntityClass(),
77
            $this->getFormData($form),
78
            $this->massActionDispatcher->dispatch($request)
0 ignored issues
show
Documentation introduced by
$this->massActionDispatcher->dispatch($request) is of type object<Oro\Bundle\DataGr...ctionResponseInterface>, but the function expects a array.

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...
79
        );
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function getTemplateVars(Request $request, FormInterface $form)
86
    {
87
        $entityIds = $this->massActionDispatcher->dispatch($request);
88
89
        return [
90
            'objectCount' => count($entityIds),
91
            'formAction' => $this->getActionUrl(
92
                $this->getType(),
93
                $form->getData(),
94
                $this->getGridUrlParameters($request)
95
            )
96
        ] + parent::getTemplateVars($request, $form);
97
    }
98
99
    /**
100
     * Returns an array containing the grid url parameters
101
     *
102
     * @param Request $request
103
     *
104
     * @return array
105
     */
106
    protected function getGridUrlParameters(Request $request)
107
    {
108
        $parameters = [];
109
        foreach (['inset', 'filters', 'values', 'gridName', 'actionName'] as $key) {
110
            $parameters[$key] = $request->get($key);
111
        }
112
113
        return $parameters;
114
    }
115
116
    /**
117
     * Returns the data of the form
118
     *
119
     * @param FormInterface $form
120
     *
121
     * @return array
122
     */
123
    protected function getFormData(FormInterface $form)
124
    {
125
        $data = [];
126
        foreach ($form as $key => $field) {
127
            if ($field->getConfig()->getMapped()) {
128
                $data[$key] = $field->getData();
129
            }
130
        }
131
132
        return $data;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    protected function setDefaultOptions(OptionsResolver $resolver)
139
    {
140
        parent::setDefaultOptions($resolver);
141
        $resolver->setDefaults(
142
            [
143
                'route'               => 'pim_customentity_massedit',
144
                'template'            => 'PimCustomEntityBundle:CustomEntity:massEdit.html.twig',
145
                'success_message'     => sprintf('flash.%s.mass_updated', $this->configuration->getName())
146
            ]
147
        );
148
    }
149
}
150