DefaultController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 9
crap 1

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
/* Copyright (C) 2015 Michael Giesler
4
 *
5
 * This file is part of Dembelo.
6
 *
7
 * Dembelo is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * Dembelo 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 3 for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License 3
18
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/**
22
 * @package AdminBundle
23
 */
24
25
namespace AdminBundle\Controller;
26
27
use DembeloMain\Document\User;
28
use DembeloMain\Model\Repository\Doctrine\ODM\AbstractRepository;
29
use DembeloMain\Document\Importfile;
30
use DembeloMain\Model\Repository\ImportfileRepositoryInterface;
31
use DembeloMain\Model\Repository\LicenseeRepositoryInterface;
32
use DembeloMain\Model\Repository\TopicRepositoryInterface;
33
use DembeloMain\Model\Repository\UserRepositoryInterface;
34
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
35
use Symfony\Component\Filesystem\Filesystem;
36
use Symfony\Component\HttpFoundation\Request;
37
use Symfony\Component\HttpFoundation\Response;
38
use DembeloMain\Document\Topic;
39
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
40
use Symfony\Component\Serializer\Encoder\JsonEncoder;
41
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating;
42
43
/**
44
 * Class DefaultController
45
 * @Route(service="app.admin_controller_default")
46
 */
47
class DefaultController
48
{
49
    /**
50
     * @var string
51
     */
52
    private $configTwineDirectory;
53
54
    /**
55
     * @var Templating
56
     */
57
    private $templating;
58
59
    /**
60
     * @var UserRepositoryInterface
61
     */
62
    private $userRepository;
63
64
    /**
65
     * @var LicenseeRepositoryInterface
66
     */
67
    private $licenseeRepository;
68
69
    /**
70
     * @var TopicRepositoryInterface
71
     */
72
    private $topicRepository;
73
74
    /**
75
     * @var ImportfileRepositoryInterface
76
     */
77
    private $importfileRepository;
78
79
    /**
80
     * @var UserPasswordEncoder
81
     */
82
    private $userPasswordEncoder;
83
84
    /**
85
     * @var string
86
     */
87
    private $topicImageDirectory;
88
89
    /**
90
     * @var Filesystem
91
     */
92
    private $filesystem;
93
94
    /**
95
     * DefaultController constructor.
96
     * @param Templating                    $templating
97
     * @param UserRepositoryInterface       $userRepository
98
     * @param LicenseeRepositoryInterface   $licenseeRepository
99
     * @param TopicRepositoryInterface      $topicRepository
100
     * @param ImportfileRepositoryInterface $importfileRepository
101
     * @param UserPasswordEncoder           $userPasswordEncoder
102
     * @param string                        $configTwineDirectory
103
     * @param string                        $topicImageDirectory
104
     * @param Filesystem                    $filesystem
105
     */
106 7
    public function __construct(Templating $templating, UserRepositoryInterface $userRepository, LicenseeRepositoryInterface $licenseeRepository, TopicRepositoryInterface $topicRepository, ImportfileRepositoryInterface $importfileRepository, UserPasswordEncoder $userPasswordEncoder, string $configTwineDirectory, string $topicImageDirectory, Filesystem $filesystem)
107
    {
108 7
        $this->configTwineDirectory = $configTwineDirectory;
109 7
        $this->userRepository = $userRepository;
110 7
        $this->templating = $templating;
111 7
        $this->licenseeRepository = $licenseeRepository;
112 7
        $this->topicRepository = $topicRepository;
113 7
        $this->importfileRepository = $importfileRepository;
114 7
        $this->userPasswordEncoder = $userPasswordEncoder;
115 7
        $this->topicImageDirectory = $topicImageDirectory;
116 7
        $this->filesystem = $filesystem;
117 7
    }
118
119
    /**
120
     * @Route("/", name="admin_mainpage")
121
     *
122
     * @return Response
123
     *
124
     * @throws \RuntimeException
125
     */
126 1
    public function indexAction(): Response
127
    {
128
        $mainMenuData = [
129 1
            ['id' => '1', 'type' => 'folder', 'value' => 'Benutzer', 'css' => 'folder_music'],
130
            ['id' => '2', 'type' => 'folder', 'value' => 'Lizenznehmer', 'css' => 'folder_music'],
131
            ['id' => '3', 'type' => 'folder', 'value' => 'Themenfelder', 'css' => 'folder_music'],
132
            ['id' => '4', 'type' => 'folder', 'value' => 'Importe', 'css' => 'folder_music'],
133
            ['id' => '5', 'type' => 'folder', 'value' => 'Textknoten', 'css' => 'folder_music'],
134
        ];
135
136 1
        $jsonEncoder = new JsonEncoder();
137
138 1
        return $this->templating->renderResponse(
139 1
            'AdminBundle::index.html.twig',
140
            [
141 1
                'mainMenuData' => $jsonEncoder->encode($mainMenuData, 'json'),
142
            ]
143
        );
144
    }
145
146
    /**
147
     * @Route("/save", name="admin_formsave")
148
     *
149
     * @param Request $request
150
     *
151
     * @return Response
152
     *
153
     * @throws \Symfony\Component\Filesystem\Exception\IOException
154
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
155
     * @throws \Exception
156
     * @throws \RuntimeException
157
     * @throws \InvalidArgumentException
158
     */
159 6
    public function formsaveAction(Request $request): Response
160
    {
161 6
        $params = $request->request->all();
162
163 6
        if (!isset($params['id'])) {
164 1
            return new Response(\json_encode(array('error' => true)));
165
        }
166 5
        $formtype = $params['formtype'] ?? '';
167
168
        /* @var $repository AbstractRepository */
169
        switch ($formtype) {
170 5
            case 'user':
171 1
                $repository = $this->userRepository;
172 1
                break;
173 4
            case 'topic':
174 1
                $repository = $this->topicRepository;
175 1
                break;
176 3
            case 'licensee':
177 1
                $repository = $this->licenseeRepository;
178 1
                break;
179 2
            case 'importfile':
180 1
                $repository = $this->importfileRepository;
181 1
                break;
182
            default:
183 1
                return new Response(\json_encode(array('error' => true)));
184
        }
185
186 4
        if (isset($params['id']) && $params['id'] === 'new') {
187 1
            $className = $repository->getClassName();
188 1
            $item = new $className();
189
        } else {
190 3
            $item = $repository->find($params['id']);
191 3
            if (null === $item || $item->getId() !== $params['id']) {
192
                return new Response(\json_encode(array('error' => true)));
193
            }
194
        }
195
196 4
        foreach ($params as $param => $value) {
197 4
            if (in_array($param, array('id', 'formtype', 'filename', 'orgname'), true)) {
198 4
                continue;
199
            }
200 2
            if ('password' === $param && empty($value)) {
201
                continue;
202
            }
203
204 2
            if ($item instanceof User && 'password' === $param) {
205 1
                $value = $this->userPasswordEncoder->encodePassword($item, $value);
206 2
            } elseif ('' === $value && in_array($param, ['licenseeId', 'imported'], true)) {
207
                $value = null;
208
            }
209 2
            $method = 'set'.ucfirst($param);
210 2
            if (method_exists($item, $method)) {
211 2
                $item->$method($value);
212
            }
213
        }
214
215 4
        if (method_exists($item, 'setMetadata')) {
216 1
            $item->setMetadata('updated', time());
217
        }
218 4
        $repository->save($item);
0 ignored issues
show
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Licensee and DembeloMain\Document\Lic...mbeloMain\Document\User and DembeloMain\Document\User; however, parameter $topic of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\Topic, maybe add an additional type check? ( Ignorable by Annotation )

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

218
        $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Licensee and DembeloMain\Document\Topic; however, parameter $user of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\User, maybe add an additional type check? ( Ignorable by Annotation )

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

218
        $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Top...mbeloMain\Document\User and DembeloMain\Document\Licensee and DembeloMain\Document\Lic...mbeloMain\Document\User and DembeloMain\Document\Topic and DembeloMain\Document\User; however, parameter $importfile of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\Importfile, maybe add an additional type check? ( Ignorable by Annotation )

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

218
        $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Top...mbeloMain\Document\User and DembeloMain\Document\Topic and DembeloMain\Document\User; however, parameter $licensee of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\Licensee, maybe add an additional type check? ( Ignorable by Annotation )

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

218
        $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
219
220 4
        if ($item instanceof Topic
221 4
            && array_key_exists('imageFileName', $params)
222 4
            && array_key_exists('originalImageName', $params)
223 4
            && !empty($params['imageFileName'])
224 4
            && !empty($params['originalImageName'])
225
        ) {
226 1
            $this->saveTopicImage($item, $params['imageFileName'], $params['originalImageName']);
227 1
            $repository->save($item);
0 ignored issues
show
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Lic...beloMain\Document\Topic and DembeloMain\Document\Top...mbeloMain\Document\User and DembeloMain\Document\Lic...beloMain\Document\Topic and DembeloMain\Document\Use...beloMain\Document\Topic and DembeloMain\Document\Topic; however, parameter $importfile of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\Importfile, maybe add an additional type check? ( Ignorable by Annotation )

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

227
            $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Lic...beloMain\Document\Topic and DembeloMain\Document\Topic; however, parameter $user of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\User, maybe add an additional type check? ( Ignorable by Annotation )

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

227
            $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Top...mbeloMain\Document\User and DembeloMain\Document\Use...beloMain\Document\Topic and DembeloMain\Document\Topic; however, parameter $licensee of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\Licensee, maybe add an additional type check? ( Ignorable by Annotation )

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

227
            $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
228
        }
229
230 4
        if ($item instanceof Importfile
231 4
            && array_key_exists('filename', $params)
232 4
            && array_key_exists('orgname', $params)
233 4
            && !empty($params['filename'])
234 4
            && !empty($params['orgname'])
235
        ) {
236 1
            $this->saveFile($item, $params['filename'], $params['orgname']);
237 1
            $repository->save($item);
0 ignored issues
show
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Importfile and DembeloMain\Document\Lic...ain\Document\Importfile and DembeloMain\Document\Use...ain\Document\Importfile; however, parameter $topic of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\Topic, maybe add an additional type check? ( Ignorable by Annotation )

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

237
            $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Use...ain\Document\Importfile and DembeloMain\Document\Top...ain\Document\Importfile and DembeloMain\Document\Importfile and DembeloMain\Document\Top...ain\Document\Importfile and DembeloMain\Document\Use...ain\Document\Importfile; however, parameter $licensee of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\Licensee, maybe add an additional type check? ( Ignorable by Annotation )

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

237
            $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
Bug introduced by
It seems like $item can also be of type DembeloMain\Document\Lic...ain\Document\Importfile and DembeloMain\Document\Top...ain\Document\Importfile and DembeloMain\Document\Importfile and DembeloMain\Document\Lic...ain\Document\Importfile; however, parameter $user of DembeloMain\Model\Reposi...sitoryInterface::save() does only seem to accept DembeloMain\Document\User, maybe add an additional type check? ( Ignorable by Annotation )

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

237
            $repository->save(/** @scrutinizer ignore-type */ $item);
Loading history...
238
        }
239
240
        $output = array(
241 4
            'error' => false,
242 4
            'newId' => $item->getId(),
243
        );
244
245 4
        return new Response(\json_encode($output));
246
    }
247
248
    /**
249
     * saves temporary file to final place
250
     *
251
     * @param Importfile $item     importfile instance
252
     * @param string     $filename filename hash
253
     * @param string     $orgname  original name
254
     *
255
     * @return void
256
     *
257
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
258
     * @throws \RuntimeException
259
     * @throws \Symfony\Component\Filesystem\Exception\IOException
260
     */
261 1
    private function saveFile(Importfile $item, $filename, $orgname): void
262
    {
263 1
        $directory = $this->configTwineDirectory;
264 1
        $file = $directory.$filename;
265 1
        if (!$this->filesystem->exists($file)) {
266
            return;
267
        }
268 1
        $finalDirectory = $directory.$item->getLicenseeId().'/';
269 1
        $this->filesystem->mkdir($finalDirectory);
270 1
        $finalName = $finalDirectory.$item->getId();
271
272 1
        $this->filesystem->rename($file, $finalName);
273
274 1
        $item->setOriginalname($orgname);
275 1
        $item->setFilename($finalName);
276 1
    }
277
278
    /**
279
     * saves temporary file to final place
280
     *
281
     * @param Topic  $item     topic instance
282
     * @param string $filename filename hash
283
     * @param string $orgname  original name
284
     *
285
     * @return void
286
     *
287
     * @throws \RuntimeException
288
     */
289 1
    private function saveTopicImage(Topic $item, string $filename, string $orgname): void
290
    {
291 1
        $directory = $this->topicImageDirectory;
292 1
        $finalDirectory = $directory.$item->getId().'/';
293 1
        $this->filesystem->mkdir($finalDirectory);
294
295 1
        $finalName = $finalDirectory.$orgname;
296 1
        $file = $directory.$filename;
297 1
        $this->filesystem->rename($file, $finalName);
298 1
        $item->setOriginalImageName($orgname);
299 1
        $item->setImageFilename($finalName);
300 1
    }
301
}
302