Passed
Pull Request — master (#58)
by Daniel
06:14
created

DownloadAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 25
ccs 0
cts 9
cp 0
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 23 4
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components 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\ApiComponentsBundle\Action\Uploadable;
15
16
use Silverback\ApiComponentsBundle\AnnotationReader\UploadableAnnotationReader;
17
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
18
use Silverback\ApiComponentsBundle\Uploadable\UploadableHelper;
19
use Symfony\Component\HttpFoundation\BinaryFileResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class DownloadAction
27
{
28
    public function __invoke(
29
        object $data,
30
        string $property,
31
        Request $request,
32
        UploadableAnnotationReader $annotationReader,
33
        UploadableHelper $uploadableHelper
34
    ) {
35
        if (!$annotationReader->isConfigured($data)) {
36
            throw new InvalidArgumentException(sprintf('%s is not an uploadable resource. It should not be configured to use %s.', \get_class($data), __CLASS__));
37
        }
38
39
        try {
40
            $file = $uploadableHelper->getFile($data, $property);
41
        } catch (InvalidArgumentException $e) {
42
            // Property not configured, convert to not found URL
43
            throw new NotFoundHttpException($e->getMessage());
44
        }
45
46
        if (!$file) {
0 ignored issues
show
introduced by
$file is of type resource, thus it always evaluated to false.
Loading history...
47
            throw new NotFoundHttpException('File not found.');
48
        }
49
50
        return new BinaryFileResponse($file);
51
    }
52
}
53