Completed
Push — dev ( f0eb90...48c810 )
by Yan
02:10
created

FileResolver::resolve()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 6
nop 1
dl 0
loc 23
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
namespace Lincable\Http\File;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Http\File as IlluminateFile;
7
use Lincable\Exceptions\NotResolvableFileException;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
10
11
class FileResolver
12
{
13
    /**
14
     * Resolve the file object to a symfony file, handling the 
15
     * file request operations.
16
     * 
17
     * @throws \Lincable\Exceptions\NotResolvableFileException
18
     * 
19
     * @param  mixed $file
20
     * @return \Illuminate\Http\File
21
     */
22
    public static function resolve($file) 
23
    {
24
        switch(true) {
25
            case is_string($file):
26
                $file = new SymfonyFile($file);
27
                break;
28
            case $file instanceof FileRequest:
0 ignored issues
show
Bug introduced by
The type Lincable\Http\File\FileRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
                $file = $file->preprareFile(Container::getInstance());
30
                break;
31
            case $file instanceof UploadedFile:
32
                $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

32
                /** @scrutinizer ignore-call */ 
33
                $filename = $file->hashName();
Loading history...
33
                $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

33
                $file = $file->move(/** @scrutinizer ignore-type */ config('lincable.temp_directory'), $filename);
Loading history...
34
                break; 
35
            case $file instanceof IlluminateFile:
36
                return $file;
37
                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...
38
            case $file instanceof Symfonyfile:
39
                break;
40
            default:
41
                throw new NotResolvableFileException($file);
42
        }
43
44
        return static::toIlluminateFile($file);
45
    }
46
47
    /**
48
     * Convert a symfony file to illuminate file.
49
     * 
50
     * @param  \Symfony\Component\HttpFoundation\File\File $file
51
     * @return \Illuminate\Http\File
52
     */
53
    public static function toIlluminateFile(SymfonyFile $file)
54
    {
55
        return new IlluminateFile($file->getPathName());
56
    }
57
}