Test Failed
Push — dev ( 3f81c0...d72bb5 )
by Yan
03:23
created

FileResolver::resolveFileRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable\Http\File;
4
5
use Illuminate\Http\Request;
6
use Lincable\Http\FileRequest;
7
use Illuminate\Container\Container;
8
use Illuminate\Http\File as IlluminateFile;
9
use Lincable\Exceptions\NotResolvableFileException;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
12
13
class FileResolver
14
{
15
    /**
16
     * Resolve the file object to a symfony file, handling the
17
     * file request operations.
18
     *
19
     * @throws \Lincable\Exceptions\NotResolvableFileException
20
     *
21
     * @param  mixed $file
22
     * @return \Illuminate\Http\File
23
     */
24
    public static function resolve($file)
25
    {
26
        switch (true) {
27
            case is_string($file):
28
                $file = new SymfonyFile($file);
29
                break;
30
            case $file instanceof FileRequest:
31
                $file = static::resolveFileRequest($file);
32
                break;
33
            case $file instanceof UploadedFile:
34
                $filename = $file->hashName();
0 ignored issues
show
introduced by
The method hashName() does not exist on Symfony\Component\HttpFoundation\File\UploadedFile. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
                /** @scrutinizer ignore-call */ 
35
                $filename = $file->hashName();
Loading history...
35
                $file = $file->move(config('lincable.temp_directory'), $filename);
0 ignored issues
show
Bug introduced by
It seems like config('lincable.temp_directory') can also be of type array; however, parameter $directory of Symfony\Component\HttpFo...le\UploadedFile::move() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
                $file = $file->move(/** @scrutinizer ignore-type */ config('lincable.temp_directory'), $filename);
Loading history...
36
                break;
37
            case $file instanceof IlluminateFile:
38
                return $file;
39
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
40
            case $file instanceof Symfonyfile: break;
41
            default:
42
                throw new NotResolvableFileException($file);
43
        }
44
        
45
        return static::toIlluminateFile($file);
46
    }
47
48
    /**
49
     * Convert a symfony file to illuminate file.
50
     *
51
     * @param  \Symfony\Component\HttpFoundation\File\File $file
52
     * @return \Illuminate\Http\File
53
     */
54
    public static function toIlluminateFile(SymfonyFile $file)
55
    {
56
        return new IlluminateFile($file->getPathName());
57
    }
58
59
    /**
60
     * Handle a file request and resolve to a file.
61
     *
62
     * @param  \Lincable\Http\FileRequest $file
63
     * @return \Symfony\Component\HttpFoundation\File\File
64
     */
65
    public static function resolveFileRequest(FileRequest $file)
66
    {
67
        // Get the global container instance.
68
        $app = Container::getInstance();
69
        
70
        if (! $file->isBooted()) {
71
            
72
            // Boot the file request with the current request.
73
            $file->boot($app->make(Request::class));
74
        }
75
        
76
        return $file->prepareFile($app);
77
    }
78
}
79