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

UploadableEventListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
dl 0
loc 23
ccs 0
cts 12
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onPreWrite() 0 12 4
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