Passed
Pull Request — master (#52)
by Daniel
05:20
created

UploadableEventListener::onPreWrite()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 10
c 1
b 0
f 0
cc 4
nc 2
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentBundle\EventListener\Api;
15
16
use Silverback\ApiComponentBundle\AnnotationReader\UploadableAnnotationReader;
17
use Silverback\ApiComponentBundle\Uploadable\UploadableHelper;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpKernel\Event\ViewEvent;
20
21
/**
22
 * @author Vincent Chalamon <[email protected]>
23
 */
24
final class UploadableEventListener
25
{
26
    private UploadableAnnotationReader $uploadableAnnotationReader;
27
    private UploadableHelper $uploadableHelper;
28
29
    public function __construct(UploadableAnnotationReader $uploadableAnnotationReader, UploadableHelper $uploadableHelper)
30
    {
31
        $this->uploadableAnnotationReader = $uploadableAnnotationReader;
32
        $this->uploadableHelper = $uploadableHelper;
33
    }
34
35
    public function onPreWrite(ViewEvent $event): void
36
    {
37
        $request = $event->getRequest();
38
        $data = $request->attributes->get('data');
39
        if (
40
            empty($data) ||
41
            !$this->uploadableAnnotationReader->isConfigured($data) ||
42
            $request->isMethod(Request::METHOD_DELETE)
43
        ) {
44
            return;
45
        }
46
        $this->uploadableHelper->persistFiles($data);
47
    }
48
}
49