Passed
Pull Request — master (#70)
by Jan
11:26
created

CurrencyController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
c 0
b 0
f 0
nc 1
nop 12
dl 0
loc 29
rs 9.8333

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
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\Controller\AdminPages;
44
45
use App\Entity\Attachments\CurrencyAttachment;
46
use App\Entity\Base\AbstractNamedDBElement;
47
use App\Entity\Parameters\CurrencyParameter;
48
use App\Entity\PriceInformations\Currency;
49
use App\Form\AdminPages\CurrencyAdminForm;
50
use App\Services\Attachments\AttachmentSubmitHandler;
51
use App\Services\EntityExporter;
52
use App\Services\EntityImporter;
53
use App\Services\ExchangeRateUpdater;
54
use App\Services\LabelSystem\Barcodes\BarcodeExampleElementsGenerator;
55
use App\Services\LabelSystem\LabelGenerator;
56
use App\Services\LogSystem\EventCommentHelper;
57
use App\Services\LogSystem\HistoryHelper;
58
use App\Services\LogSystem\TimeTravel;
59
use App\Services\StructuralElementRecursionHelper;
60
use Doctrine\ORM\EntityManagerInterface;
61
use Exchanger\Exception\ChainException;
62
use Exchanger\Exception\Exception;
63
use Exchanger\Exception\UnsupportedCurrencyPairException;
64
use Omines\DataTablesBundle\DataTableFactory;
65
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
66
use Symfony\Component\Form\FormInterface;
67
use Symfony\Component\HttpFoundation\RedirectResponse;
68
use Symfony\Component\HttpFoundation\Request;
69
use Symfony\Component\HttpFoundation\Response;
70
use Symfony\Component\Routing\Annotation\Route;
71
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
72
use Symfony\Contracts\Translation\TranslatorInterface;
73
74
/**
75
 * @Route("/currency")
76
 *
77
 * Class CurrencyController
78
 */
79
class CurrencyController extends BaseAdminController
80
{
81
    protected $entity_class = Currency::class;
82
    protected $twig_template = 'AdminPages/CurrencyAdmin.html.twig';
83
    protected $form_class = CurrencyAdminForm::class;
84
    protected $route_base = 'currency';
85
    protected $attachment_class = CurrencyAttachment::class;
86
    protected $parameter_class = CurrencyParameter::class;
87
88
    protected $exchangeRateUpdater;
89
90
    public function __construct(
91
        TranslatorInterface $translator,
92
        UserPasswordEncoderInterface $passwordEncoder,
93
        AttachmentSubmitHandler $attachmentSubmitHandler,
94
        EventCommentHelper $commentHelper,
95
        HistoryHelper $historyHelper,
96
        TimeTravel $timeTravel,
97
        DataTableFactory $dataTableFactory,
98
        EventDispatcherInterface $eventDispatcher,
99
        BarcodeExampleElementsGenerator $barcodeExampleGenerator,
100
        LabelGenerator $labelGenerator,
101
        EntityManagerInterface $entityManager,
102
        ExchangeRateUpdater $exchangeRateUpdater
103
104
    ) {
105
        $this->exchangeRateUpdater = $exchangeRateUpdater;
106
107
        parent::__construct(
108
            $translator,
109
            $passwordEncoder,
110
            $attachmentSubmitHandler,
111
            $commentHelper,
112
            $historyHelper,
113
            $timeTravel,
114
            $dataTableFactory,
115
            $eventDispatcher,
116
            $barcodeExampleGenerator,
117
            $labelGenerator,
118
            $entityManager
119
        );
120
    }
121
122
    /**
123
     * @Route("/{id}", name="currency_delete", methods={"DELETE"})
124
     *
125
     * @return RedirectResponse
126
     */
127
    public function delete(Request $request, Currency $entity, StructuralElementRecursionHelper $recursionHelper): RedirectResponse
128
    {
129
        return $this->_delete($request, $entity, $recursionHelper);
130
    }
131
132
    public function additionalActionEdit(FormInterface $form, AbstractNamedDBElement $entity): bool
133
    {
134
        if (!$entity instanceof Currency) {
135
            return false;
136
        }
137
138
        //@phpstan-ignore-next-line
139
        if ($form->get('update_exchange_rate')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
        if ($form->get('update_exchange_rate')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
140
            $this->denyAccessUnlessGranted('edit', $entity);
141
            try {
142
                $this->exchangeRateUpdater->update($entity);
143
                $this->addFlash('info', 'currency.edit.exchange_rate_updated.success');
144
            } catch (ChainException $exception) {
145
                $exception = $exception->getExceptions()[0];
146
                if ($exception instanceof UnsupportedCurrencyPairException || stripos($exception->getMessage(), "supported") !== false) {
147
                    $this->addFlash('error', 'currency.edit.exchange_rate_update.unsupported_currency');
148
                } else {
149
                    $this->addFlash('error', 'currency.edit.exchange_rate_update.generic_error');
150
                }
151
            }
152
        }
153
154
        return true;
155
    }
156
157
    /**
158
     * @Route("/{id}/edit/{timestamp}", requirements={"id"="\d+"}, name="currency_edit")
159
     * @Route("/{id}", requirements={"id"="\d+"})
160
     *
161
     * @return Response
162
     */
163
    public function edit(Currency $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null): Response
164
    {
165
        return $this->_edit($entity, $request, $em, $timestamp);
166
    }
167
168
    /**
169
     * @Route("/new", name="currency_new")
170
     * @Route("/{id}/clone", name="currency_clone")
171
     * @Route("/")
172
     *
173
     * @return Response
174
     */
175
    public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer, ?Currency $entity = null): Response
176
    {
177
        return $this->_new($request, $em, $importer, $entity);
178
    }
179
180
    /**
181
     * @Route("/export", name="currency_export_all")
182
     *
183
     * @return Response
184
     */
185
    public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request): Response
186
    {
187
        return $this->_exportAll($em, $exporter, $request);
188
    }
189
190
    /**
191
     * @Route("/{id}/export", name="currency_export")
192
     *
193
     * @return Response
194
     */
195
    public function exportEntity(Currency $entity, EntityExporter $exporter, Request $request): Response
196
    {
197
        return $this->_exportEntity($entity, $exporter, $request);
198
    }
199
200
    public function deleteCheck(AbstractNamedDBElement $entity): bool
201
    {
202
        if ($entity instanceof Currency) {
203
            if ($entity->getPricedetails()->count() > 0) {
204
                $this->addFlash('error', 'entity.delete.must_not_contain_prices');
205
                return false;
206
            }
207
        }
208
209
        return true;
210
    }
211
}
212