FileResolver::resolveSymfonyFile()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 1
dl 0
loc 22
ccs 12
cts 12
cp 1
crap 5
rs 9.6111
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
     * @param  mixed $file
20
     * @return \Illuminate\Http\File
21
     */
22 26
    public static function resolve($file)
23
    {
24 26
        if ($file instanceof IlluminateFile) {
25 17
            return $file;
26
        }
27
28 9
        return static::toIlluminateFile(static::resolveSymfonyFile($file));
29
    }
30
31
    /**
32
     * Prepare the file .
33
     *
34
     * @param  mixed  $file
35
     * @return \Symfony\Component\HttpFoundation\File\File
36
     * 
37
     * @throws \Lincable\Exceptions\NotResolvableFileException
38
     */
39 9
    public static function resolveSymfonyFile($file)
40
    {
41 9
        if (is_string($file)) {
42 2
            return new SymfonyFile($file);
43
        }
44
            
45 7
        if ($file instanceof FileRequest) {
46 2
            return $file->prepareFile();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file->prepareFile() also could return the type callable which is incompatible with the documented return type Symfony\Component\HttpFoundation\File\File.
Loading history...
47
        }
48
            
49 7
        if ($file instanceof UploadedFile) {
50 5
            return $file->move(
51 5
                FileFactory::getTemporaryDirectory(), 
0 ignored issues
show
Bug introduced by
Are you sure the usage of Lincable\Http\File\FileF...getTemporaryDirectory() targeting Lincable\Http\File\FileF...getTemporaryDirectory() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Lincable\Http\File\FileF...getTemporaryDirectory() of type void is incompatible with the type string expected by parameter $directory of Symfony\Component\HttpFo...le\UploadedFile::move(). ( Ignorable by Annotation )

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

51
                /** @scrutinizer ignore-type */ FileFactory::getTemporaryDirectory(), 
Loading history...
52 5
                $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

52
                $file->/** @scrutinizer ignore-call */ 
53
                       hashName()
Loading history...
53
            );   
54
        }
55
        
56 2
        if ($file instanceof Symfonyfile) {
57 1
            return $file;
58
        }
59
60 1
        throw new NotResolvableFileException($file);
61
    }
62
63
    /**
64
     * Convert a symfony file to illuminate file.
65
     *
66
     * @param  \Symfony\Component\HttpFoundation\File\File $file
67
     * @return \Illuminate\Http\File
68
     */
69 7
    public static function toIlluminateFile(SymfonyFile $file)
70
    {
71 7
        return new IlluminateFile($file->getPathName());
72
    }
73
}
74