QuickExportAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.4285
cc 1
eloc 15
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 Akeneo\Bundle\BatchBundle\Launcher\JobLauncherInterface;
6
use Pim\Bundle\CustomEntityBundle\Event\ActionEventManager;
7
use Pim\Bundle\CustomEntityBundle\Manager\Registry as ManagerRegistry;
8
use Pim\Bundle\DataGridBundle\Extension\MassAction\MassActionDispatcher;
9
use Pim\Bundle\ImportExportBundle\Entity\Repository\JobInstanceRepository;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
use Symfony\Component\Routing\RouterInterface;
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
use Symfony\Component\Translation\TranslatorInterface;
18
19
/**
20
 * @author    Antoine Guigan <[email protected]>
21
 * @copyright 2014 Akeneo SAS (http://www.akeneo.com)
22
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
23
 */
24
class QuickExportAction extends AbstractAction
25
{
26
    /** @var MassActionDispatcher */
27
    protected $massActionDispatcher;
28
29
    /** @var JobInstanceRepository */
30
    protected $jobInstanceRepo;
31
32
    /** @var JobLauncherInterface */
33
    protected $jobLauncher;
34
35
    /** @var TokenStorageInterface */
36
    protected $tokenStorage;
37
38
    /**
39
     * @param ActionFactory         $actionFactory
40
     * @param ActionEventManager    $eventManager
41
     * @param ManagerRegistry       $managerRegistry
42
     * @param RouterInterface       $router
43
     * @param TranslatorInterface   $translator
44
     * @param MassActionDispatcher  $massActionDispatcher
45
     * @param JobInstanceRepository $jobInstanceRepo
46
     * @param JobLauncherInterface  $jobLauncher
47
     * @param TokenStorageInterface $tokenStorage
48
     */
49
    public function __construct(
50
        ActionFactory $actionFactory,
51
        ActionEventManager $eventManager,
52
        ManagerRegistry $managerRegistry,
53
        RouterInterface $router,
54
        TranslatorInterface $translator,
55
        MassActionDispatcher $massActionDispatcher,
56
        JobInstanceRepository $jobInstanceRepo,
57
        JobLauncherInterface $jobLauncher,
58
        TokenStorageInterface $tokenStorage
59
    ) {
60
        parent::__construct($actionFactory, $eventManager, $managerRegistry, $router, $translator);
61
62
        $this->massActionDispatcher = $massActionDispatcher;
63
        $this->jobInstanceRepo      = $jobInstanceRepo;
64
        $this->jobLauncher          = $jobLauncher;
65
        $this->tokenStorage         = $tokenStorage;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function doExecute(Request $request)
72
    {
73
        $jobInstance = $this->jobInstanceRepo->findOneBy(['code' => $this->getOption('job_profile')]);
74
        if (null === $jobInstance) {
75
            throw new \LogicException(
76
                sprintf(
77
                    'The job instance "%s" does not exist. Please contact your administrator',
78
                    $this->getOption('job_profile')
79
                )
80
            );
81
        }
82
83
        $rawConfiguration = addslashes(
84
            json_encode(
85
                [
86
                    'reference_data' => $this->configuration->getEntityClass(),
87
                    'ids'            => $this->massActionDispatcher->dispatch($request),
88
                ]
89
            )
90
        );
91
92
        $this->jobLauncher->launch($jobInstance, $this->getUser(), $rawConfiguration);
93
94
        return new Response();
95
    }
96
97
    /**
98
     * Get the authenticated user from the Security Context
99
     *
100
     * @return UserInterface
101
     *
102
     * @throws TokenNotFoundException
103
     *
104
     * @see Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getUser()
105
     */
106
    protected function getUser()
107
    {
108
        $token = $this->tokenStorage->getToken();
109
        if (null === $token || !is_object($user = $token->getUser())) {
110
            throw new TokenNotFoundException('You are no longer authenticated. Please log in and try again');
111
        }
112
113
        return $user;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    protected function setDefaultOptions(OptionsResolver $resolver)
120
    {
121
        $resolver->setDefined(['limit']);
122
        $resolver->setDefaults(
123
            [
124
                'route'               => 'pim_customentity_quickexport',
125
                'job_profile'         => 'csv_reference_data_quick_export',
126
                'serializer_context'  => [],
127
            ]
128
        );
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getType()
135
    {
136
        return 'quick_export';
137
    }
138
}
139