CurrencyController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
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 28
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 - 2022 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
namespace App\Controller\AdminPages;
24
25
use App\Entity\Attachments\CurrencyAttachment;
26
use App\Entity\Base\AbstractNamedDBElement;
27
use App\Entity\Parameters\CurrencyParameter;
28
use App\Entity\PriceInformations\Currency;
29
use App\Form\AdminPages\CurrencyAdminForm;
30
use App\Services\Attachments\AttachmentSubmitHandler;
31
use App\Services\ImportExportSystem\EntityExporter;
32
use App\Services\ImportExportSystem\EntityImporter;
33
use App\Services\Tools\ExchangeRateUpdater;
34
use App\Services\LabelSystem\Barcodes\BarcodeExampleElementsGenerator;
35
use App\Services\LabelSystem\LabelGenerator;
36
use App\Services\LogSystem\EventCommentHelper;
37
use App\Services\LogSystem\HistoryHelper;
38
use App\Services\LogSystem\TimeTravel;
39
use App\Services\Trees\StructuralElementRecursionHelper;
40
use Doctrine\ORM\EntityManagerInterface;
41
use Exchanger\Exception\ChainException;
42
use Exchanger\Exception\Exception;
43
use Exchanger\Exception\UnsupportedCurrencyPairException;
44
use Omines\DataTablesBundle\DataTableFactory;
45
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
46
use Symfony\Component\Form\FormInterface;
47
use Symfony\Component\HttpFoundation\RedirectResponse;
48
use Symfony\Component\HttpFoundation\Request;
49
use Symfony\Component\HttpFoundation\Response;
50
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
51
use Symfony\Component\Routing\Annotation\Route;
52
use Symfony\Contracts\Translation\TranslatorInterface;
53
54
/**
55
 * @Route("/currency")
56
 *
57
 * Class CurrencyController
58
 */
59
class CurrencyController extends BaseAdminController
60
{
61
    protected string $entity_class = Currency::class;
62
    protected string $twig_template = 'admin/currency_admin.html.twig';
63
    protected string $form_class = CurrencyAdminForm::class;
64
    protected string $route_base = 'currency';
65
    protected string $attachment_class = CurrencyAttachment::class;
66
    protected ?string $parameter_class = CurrencyParameter::class;
67
68
    protected ExchangeRateUpdater $exchangeRateUpdater;
69
70
    public function __construct(
71
        TranslatorInterface $translator,
72
        UserPasswordHasherInterface $passwordEncoder,
73
        AttachmentSubmitHandler $attachmentSubmitHandler,
74
        EventCommentHelper $commentHelper,
75
        HistoryHelper $historyHelper,
76
        TimeTravel $timeTravel,
77
        DataTableFactory $dataTableFactory,
78
        EventDispatcherInterface $eventDispatcher,
79
        BarcodeExampleElementsGenerator $barcodeExampleGenerator,
80
        LabelGenerator $labelGenerator,
81
        EntityManagerInterface $entityManager,
82
        ExchangeRateUpdater $exchangeRateUpdater
83
    ) {
84
        $this->exchangeRateUpdater = $exchangeRateUpdater;
85
86
        parent::__construct(
87
            $translator,
88
            $passwordEncoder,
89
            $attachmentSubmitHandler,
90
            $commentHelper,
91
            $historyHelper,
92
            $timeTravel,
93
            $dataTableFactory,
94
            $eventDispatcher,
95
            $barcodeExampleGenerator,
96
            $labelGenerator,
97
            $entityManager
98
        );
99
    }
100
101
    /**
102
     * @Route("/{id}", name="currency_delete", methods={"DELETE"})
103
     */
104
    public function delete(Request $request, Currency $entity, StructuralElementRecursionHelper $recursionHelper): RedirectResponse
105
    {
106
        return $this->_delete($request, $entity, $recursionHelper);
107
    }
108
109
    public function additionalActionEdit(FormInterface $form, AbstractNamedDBElement $entity): bool
110
    {
111
        if (!$entity instanceof Currency) {
112
            return false;
113
        }
114
115
        //@phpstan-ignore-next-line
116
        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

116
        if ($form->get('update_exchange_rate')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
117
            $this->denyAccessUnlessGranted('edit', $entity);
118
            try {
119
                $this->exchangeRateUpdater->update($entity);
120
                $this->addFlash('info', 'currency.edit.exchange_rate_updated.success');
121
            } catch (Exception $exception) {
122
                //$exception = $exception->getExceptions()[0];
123
                if ($exception instanceof UnsupportedCurrencyPairException || false !== stripos($exception->getMessage(), 'supported')) {
124
                    $this->addFlash('error', 'currency.edit.exchange_rate_update.unsupported_currency');
125
                } else {
126
                    $this->addFlash('error', 'currency.edit.exchange_rate_update.generic_error');
127
                }
128
            }
129
        }
130
131
        return true;
132
    }
133
134
    /**
135
     * @Route("/{id}/edit/{timestamp}", requirements={"id"="\d+"}, name="currency_edit")
136
     * @Route("/{id}", requirements={"id"="\d+"})
137
     */
138
    public function edit(Currency $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null): Response
139
    {
140
        return $this->_edit($entity, $request, $em, $timestamp);
141
    }
142
143
    /**
144
     * @Route("/new", name="currency_new")
145
     * @Route("/{id}/clone", name="currency_clone")
146
     * @Route("/")
147
     */
148
    public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer, ?Currency $entity = null): Response
149
    {
150
        return $this->_new($request, $em, $importer, $entity);
151
    }
152
153
    /**
154
     * @Route("/export", name="currency_export_all")
155
     */
156
    public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request): Response
157
    {
158
        return $this->_exportAll($em, $exporter, $request);
159
    }
160
161
    /**
162
     * @Route("/{id}/export", name="currency_export")
163
     */
164
    public function exportEntity(Currency $entity, EntityExporter $exporter, Request $request): Response
165
    {
166
        return $this->_exportEntity($entity, $exporter, $request);
167
    }
168
169
    public function deleteCheck(AbstractNamedDBElement $entity): bool
170
    {
171
        if (($entity instanceof Currency) && $entity->getPricedetails()->count() > 0) {
172
            $this->addFlash('error', 'entity.delete.must_not_contain_prices');
173
174
            return false;
175
        }
176
177
        return true;
178
    }
179
}
180