EncryptedFileController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 8

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 Azine\JsCryptoStoreBundle\Controller;
4
5
use Azine\JsCryptoStoreBundle\Entity\EncryptedFile;
6
use Azine\JsCryptoStoreBundle\Entity\Repositories\EncryptedFileRepository;
7
use Azine\JsCryptoStoreBundle\Form\DownloadEncryptedFileType;
8
use Azine\JsCryptoStoreBundle\Form\UploadEncryptedFileType;
9
use Azine\JsCryptoStoreBundle\Service\OwnerProviderInterface;
10
use MuBu\DealAnalysisBundle\Entity\User;
0 ignored issues
show
Bug introduced by
The type MuBu\DealAnalysisBundle\Entity\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\Asset\Packages;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Asset\Packages was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * This controller provides actions related the encrypted files.
19
 */
20
class EncryptedFileController extends Controller
21
{
22
    private $ownerProvider;
23
    private $encryptionCipher;
24
    private $encryptionIterations;
25
    private $encryptionKs;
26
    private $encryptionTs;
27
    private $encryptionMode;
28
    private $maxFileSize;
29
    private $defaultLifeTime;
30
31
    public function __construct(OwnerProviderInterface $ownerProvider, $encryptionCipher, $encryptionIterations, $encryptionKs, $encryptionTs, $encryptionMode, $maxFileSize, $defaultLifeTime)
32
    {
33
        $this->ownerProvider = $ownerProvider;
34
        $this->encryptionCipher = $encryptionCipher;
35
        $this->encryptionIterations = $encryptionIterations;
36
        $this->encryptionKs = $encryptionKs;
37
        $this->encryptionTs = $encryptionTs;
38
        $this->encryptionMode = $encryptionMode;
39
        $this->maxFileSize = $maxFileSize;
40
        $this->defaultLifeTime = $defaultLifeTime;
41
    }
42
43
    /**
44
     *  Displays a dashboard
45
     * - show all files for the current user
46
     * - show all files for the admin
47
     * - show an upload-form.
48
     *
49
     * @param Request $request
50
     *
51
     * @return Response
52
     */
53
    public function dashboardAction(Request $request)
54
    {
55
        $uploadForm = $this->createForm(UploadEncryptedFileType::class);
56
        $uploadForm->handleRequest($request);
57
58
        $downloadForm = $this->createForm(DownloadEncryptedFileType::class);
59
        $downloadForm->handleRequest($request);
60
61
        // show users files => delete button & forms for download
62
        /** @var EncryptedFileRepository $encryptedFileRepository */
63
        $encryptedFileRepository = $this->getDoctrine()->getRepository(EncryptedFile::class);
64
        $userFiles = $encryptedFileRepository->findForDashBoard($this->ownerProvider->getOwnerId());
65
        $groupTokens = $encryptedFileRepository->getGroupTokensForUser($this->ownerProvider->getOwnerId());
66
67
        return $this->render('AzineJsCryptoStoreBundle::dashboard.html.twig',
68
            array(
69
                'groupTokens' => $groupTokens,
70
                'encryptionCipher' => $this->encryptionCipher,
71
                'encryptionIterations' => $this->encryptionIterations,
72
                'encryptionKs' => $this->encryptionKs,
73
                'encryptionTs' => $this->encryptionTs,
74
                'encryptionMode' => $this->encryptionMode,
75
                'maxFileSize' => $this->maxFileSize,
76
                'userFiles' => $userFiles,
77
                'uploadForm' => $uploadForm->createView(),
78
                'downloadForm' => $downloadForm->createView(),
79
            ));
80
    }
81
82
    /**
83
     * Displays a form to download a specific file.
84
     *
85
     * @param string $token
86
     *
87
     * @return Response
88
     */
89
    public function downloadAction(Request $request, $groupToken, $token = null)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

89
    public function downloadAction(/** @scrutinizer ignore-unused */ Request $request, $groupToken, $token = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        $files = $this->getDoctrine()->getRepository(EncryptedFile::class)->findForDownload($groupToken, $token);
92
93
        $forms = array();
94
        foreach ($files as $nextFile) {
95
            // show file form for download
96
            $form = $this->createForm(DownloadEncryptedFileType::class);
97
            $form->get('token')->setData($nextFile['token']);
98
            $forms[] = $form->createView();
99
        }
100
        $groupToken = substr($files[0]['groupToken'], 0, strrpos($files[0]['groupToken'], '-'));
101
102
        return $this->render('AzineJsCryptoStoreBundle::download.html.twig',
103
            array(
104
                'files' => $files,
105
                'groupToken' => $groupToken,
106
                'downloadForms' => $forms,
107
                'encryptionCipher' => $this->encryptionCipher,
108
                'encryptionIterations' => $this->encryptionIterations,
109
                'encryptionKs' => $this->encryptionKs,
110
                'encryptionTs' => $this->encryptionTs,
111
                'encryptionMode' => $this->encryptionMode,
112
                'maxFileSize' => $this->maxFileSize,
113
            ));
114
    }
115
116
    /**
117
     * Handle uploading, downloading or removing a file.
118
     *
119
     * @param Request $request
120
     *
121
     * @return JsonResponse
122
     */
123
    public function fileAction(Request $request)
124
    {
125
        $response = new JsonResponse();
126
        $responseData = array();
127
        if ('POST' === $request->getMethod()) {
128
            // store the submited file
129
            if (null != $request->get('fileData')) {
130
                $responseData = $this->storeFile($request);
131
            } else {
132
                $token = $request->get('token');
133
                /** @var EncryptedFile $encryptedFile */
134
                $encryptedFile = $this->getDoctrine()->getRepository(EncryptedFile::class)->findOneBy(array('token' => $token));
135
                $responseData = $this->getFileMetaData($encryptedFile);
136
            }
137
        } elseif ('DELETE' === $request->getMethod()) {
138
            $responseData = $this->deleteFile($request);
139
        }
140
141
        return $response->setData($responseData);
142
    }
143
144
    private function getFileMetaData(EncryptedFile $encryptedFile)
145
    {
146
        $responseData = array();
147
        $responseData['mimeType'] = $encryptedFile->getMimeType();
148
        $responseData['fileName'] = $encryptedFile->getFileName();
149
150
        /** @var Packages $manager */
151
        $manager = $this->get('assets.packages');
152
        $responseData['fileUrl'] = $manager->getUrl('bundles/azinejscryptostore/files').substr($encryptedFile->getFile(), strrpos($encryptedFile->getFile(), '/'));
0 ignored issues
show
Bug introduced by
It seems like $encryptedFile->getFile() can also be of type null; however, parameter $haystack of strrpos() does only seem to accept string, 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

152
        $responseData['fileUrl'] = $manager->getUrl('bundles/azinejscryptostore/files').substr($encryptedFile->getFile(), strrpos(/** @scrutinizer ignore-type */ $encryptedFile->getFile(), '/'));
Loading history...
Bug introduced by
It seems like $encryptedFile->getFile() can also be of type null; however, parameter $string of substr() does only seem to accept string, 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

152
        $responseData['fileUrl'] = $manager->getUrl('bundles/azinejscryptostore/files').substr(/** @scrutinizer ignore-type */ $encryptedFile->getFile(), strrpos($encryptedFile->getFile(), '/'));
Loading history...
153
154
        return $responseData;
155
    }
156
157
    private function storeFile(Request $request)
158
    {
159
        $fileData = $request->get('fileData');
160
        $fileName = $request->get('fileName');
161
        $mimeType = $request->get('mimeType');
162
        $description = $request->get('description');
163
        $expiry = $request->get('expiry');
164
        $groupToken = $request->get('groupToken');
165
        if (null == $groupToken) {
166
            $groupToken = ' ';
167
        }
168
        $groupToken .= '-'.md5($this->ownerProvider->getOwnerId());
169
        $encryptedFile = new EncryptedFile();
170
        $storageDirectory = __DIR__.'/../Resources/public/files/';
171
        $storageFileName = tempnam($storageDirectory, 'encrypted-');
172
        chmod($storageFileName, 0664);
173
        file_put_contents($storageFileName, $fileData);
174
        $encryptedFile->setFile($storageFileName);
175
        $encryptedFile->setFileName($fileName);
176
        $encryptedFile->setMimeType($mimeType);
177
        $encryptedFile->setDescription($description);
178
        if ('' == $expiry) {
179
            $expiry = $this->defaultLifeTime;
180
        }
181
        $encryptedFile->setExpiry(new \DateTime($expiry));
182
        $fileToken = md5($fileData);
0 ignored issues
show
Bug introduced by
It seems like $fileData can also be of type null; however, parameter $string of md5() does only seem to accept string, 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

182
        $fileToken = md5(/** @scrutinizer ignore-type */ $fileData);
Loading history...
183
        $encryptedFile->setToken($fileToken);
184
        $encryptedFile->setGroupToken($groupToken);
185
        $encryptedFile->setOwnerId($this->ownerProvider->getOwnerId());
0 ignored issues
show
Bug introduced by
$this->ownerProvider->getOwnerId() of type string is incompatible with the type integer|null expected by parameter $ownerId of Azine\JsCryptoStoreBundl...yptedFile::setOwnerId(). ( Ignorable by Annotation )

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

185
        $encryptedFile->setOwnerId(/** @scrutinizer ignore-type */ $this->ownerProvider->getOwnerId());
Loading history...
186
        $em = $this->getDoctrine()->getManager();
187
        $em->persist($encryptedFile);
188
        $em->flush();
189
        $responseData['token'] = $fileToken;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$responseData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $responseData = array(); before regardless.
Loading history...
190
        $responseData['groupToken'] = $groupToken;
191
        $responseData['group'] = substr($groupToken, 0, strrpos($groupToken, '-'));
192
        $responseData['expiryDate'] = $encryptedFile->getExpiry();
193
194
        return $responseData;
195
    }
196
197
    private function deleteFile(Request $request)
198
    {
199
        $fileToken = $request->get('token');
200
        $em = $this->getDoctrine()->getManager();
201
        /** @var EncryptedFile $encryptedFile */
202
        $encryptedFile = $em->getRepository(EncryptedFile::class)->findOneBy(array('token' => $fileToken));
203
        if ($encryptedFile) {
0 ignored issues
show
introduced by
$encryptedFile is of type Azine\JsCryptoStoreBundle\Entity\EncryptedFile, thus it always evaluated to true.
Loading history...
204
            $storageFileName = $encryptedFile->getFile();
205
            $em->remove($encryptedFile);
206
            $em->flush();
207
            unlink($storageFileName);
0 ignored issues
show
Bug introduced by
It seems like $storageFileName can also be of type null; however, parameter $filename of unlink() does only seem to accept string, 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

207
            unlink(/** @scrutinizer ignore-type */ $storageFileName);
Loading history...
208
        }
209
        $responseData['msg'] = 'file deleted';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$responseData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $responseData = array(); before regardless.
Loading history...
210
        $responseData['token'] = $fileToken;
211
212
        return $responseData;
213
    }
214
}
215