Passed
Push — master ( b2df95...206546 )
by Felipe
02:13
created

MoveUploadedFilesMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 12
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 16 4
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of coisa/http.
4
 *
5
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace CoiSA\Http\Middleware;
12
13
use CoiSA\Http\Message\UploadedFile\FilterInterface;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Psr\Http\Message\UploadedFileInterface;
17
use Psr\Http\Server\MiddlewareInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
20
/**
21
 * Class MoveUploadedFilesMiddleware
22
 *
23
 * @package CoiSA\Http\Middleware
24
 */
25
final class MoveUploadedFilesMiddleware implements MiddlewareInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $targetPath;
31
32
    /**
33
     * @var callable
34
     */
35
    private $filter;
36
37
    /**
38
     * MoveUploadedFilesMiddleware constructor.
39
     *
40
     * @param string $targetPath
41
     * @param FilterInterface|null $filter
42
     */
43
    public function __construct(string $targetPath, FilterInterface $filter = null)
44
    {
45
        $this->targetPath = $targetPath;
46
        $this->filter = $filter;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filter can also be of type CoiSA\Http\Message\UploadedFile\FilterInterface. However, the property $filter is declared as type callable. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
53
    {
54
        $uploadedFiles = $request->getUploadedFiles();
55
56
        if ($this->filter) {
57
            $uploadedFiles = $this->filter->filter($uploadedFiles);
58
        }
59
60
        if (!empty($uploadedFiles)) {
61
            /** @var UploadedFileInterface $uploadedFile */
62
            foreach ($uploadedFiles as $uploadedFile) {
63
                $uploadedFile->moveTo($this->targetPath);
64
            }
65
        }
66
67
        return $handler->handle($request);
68
    }
69
}
70