Passed
Pull Request — master (#52)
by Vincent
11:12 queued 04:00
created

UploadableEventListener::onPreWrite()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.2222
cc 6
nc 4
nop 1
crap 42
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\Helper\UploadableHelper;
17
use Silverback\ApiComponentBundle\Utility\ClassMetadataTrait;
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
    use ClassMetadataTrait;
27
28
    private UploadableHelper $uploadableHelper;
29
30
    public function __construct(UploadableHelper $uploadableHelper)
31
    {
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->uploadableHelper->isConfigured($data) ||
42
            $request->isMethod(Request::METHOD_DELETE)
43
        ) {
44
            return;
45
        }
46
47
        foreach ($this->uploadableHelper->getFields($data) as $field) {
48
            if (null !== $field) {
49
                // todo Upload file to adapter
50
                // todo Set fileName on object
51
                // todo Set $field value to null for security/performance reasons
52
            }
53
        }
54
    }
55
}
56