Completed
Push — master ( 463d73...47ba96 )
by Gerrit
02:04
created

RequestFileArgument::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Arguments;
14
15
use Addiks\SymfonyGenerics\Arguments\Argument;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Symfony\Component\HttpFoundation\Request;
18
use Webmozart\Assert\Assert;
19
use Symfony\Component\HttpFoundation\FileBag;
20
use Symfony\Component\HttpFoundation\File\UploadedFile;
21
22
final class RequestFileArgument implements Argument
23
{
24
25
    /**
26
     * @var RequestStack
27
     */
28
    private $requestStack;
29
30
    /**
31
     * @var string
32
     */
33
    private $key;
34
35
    /**
36
     * @var string
37
     */
38
    private $property;
39
40 6
    public function __construct(
41
        RequestStack $requestStack,
42
        string $key,
43
        string $property
44
    ) {
45 6
        $this->requestStack = $requestStack;
46 6
        $this->key = $key;
47 6
        $this->property = $property;
48 6
    }
49
50 6
    public function resolve()
51
    {
52
        /** @var Request $request */
53 6
        $request = $this->requestStack->getCurrentRequest();
54
55 6
        Assert::isInstanceOf(
56 6
            $request,
57 6
            Request::class,
58 6
            "Cannot resolve request-argument without active request!"
59
        );
60
61
        /** @var FileBag $files */
62 5
        $files = $request->files;
63
64
        /** @var UploadedFile $file */
65 5
        $file = $files->get($this->key);
66
67
        return [
68 5
            'object' => $file,
69 5
            'originalname' => $file->getClientOriginalName(),
70 5
            'filename' => $file->getFilename(),
71 5
            'content' => file_get_contents($file->getPathname()),
72 5
            'mimetype' => $file->getMimeType(),
73 5
        ][$this->property];
74
    }
75
76
}
77