UploadedFile   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getValue() 0 14 3
1
<?php
2
3
namespace Jasny\Controller\Parameter;
4
5
use Jasny\Controller\ParameterException;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
#[\Attribute]
9
class UploadedFile extends SingleParameter
10
{
11
    public function __construct(?string $key = null)
12
    {
13
        parent::__construct($key);
14
    }
15
16
    /**
17
     * Get uploaded file from request.
18
     */
19
    public function getValue(
20
        ServerRequestInterface $request,
21
        string $name,
22
        ?string $type,
23
        bool $required = false
24
    ): mixed {
25
        $key = $this->key ?? $name;
26
        $params = $request->getUploadedFiles();
27
28
        if ($required && !isset($params[$key])) {
29
            throw new ParameterException("Missing required uploaded file '$key'");
30
        }
31
32
        return $params[$key] ?? null;
33
    }
34
}
35