Completed
Push — master ( d3d242...9eb338 )
by Elf
05:07
created

ImageStorage::getImageEncodingFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace App\Support\Traits;
4
5
use App\Support\Image\Filters\Resize;
6
use Exception;
7
use App\Support\Helper;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
trait ImageStorage
11
{
12
    use AssetHelper;
13
14
    /**
15
     * Store image file.
16
     *
17
     * @param  mixed  $file
18
     * @param  string|null  $identifier
19
     * @return string|null  The stored path
20
     */
21
    protected function storeImageFile($file, $identifier = null)
22
    {
23
        if ($file instanceof UploadedFile && ! $file->isValid()) {
24
            return;
25
        }
26
27
        try {
28
            $image = app('image')
29
                ->make($file)
30
                ->filter($this->getImageFilter($identifier))
31
                ->encode(
32
                    $this->getImageEncodingFormat($identifier),
33
                    $this->getImageEncodingQuality($identifier)
34
                );
35
        } catch (Exception $e) {
36
            return;
37
        }
38
39
        $path = trim(trim($this->getImageDirectory($identifier), '/').'/'.trim($this->getImageFilename($image, $identifier), '/'), '/');
40
41
        if ($path && $this->getFilesystem($identifier)->put($path, $image)) {
42
            return $path;
43
        }
44
    }
45
46
    /**
47
     * Get image filter.
48
     *
49
     * @see http://image.intervention.io/api/filter
50
     *
51
     * @param  string|null  $identifier
52
     */
53
    protected function getImageFilter($identifier = null)
1 ignored issue
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
    {
55
        return (new Resize)->width(1024);
1 ignored issue
show
Documentation Bug introduced by
The method width does not exist on object<App\Support\Image\Filters\Resize>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
    }
57
58
    /**
59
     * Get image encoding format.
60
     *
61
     * @see http://image.intervention.io/api/encode
62
     *
63
     * @param  string|null  $identifier
64
     * @return string|null
65
     */
66
    protected function getImageEncodingFormat($identifier = null)
1 ignored issue
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
    }
69
70
    /**
71
     * Get image encoding quality.
72
     *
73
     * @see http://image.intervention.io/api/encode
74
     *
75
     * @param  string|null  $identifier
76
     * @return int
77
     */
78
    protected function getImageEncodingQuality($identifier = null)
1 ignored issue
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        return 90;
81
    }
82
83
    /**
84
     * Get image output directory.
85
     *
86
     * @param  string|null  $identifier
87
     * @return string
88
     */
89
    protected function getImageDirectory($identifier = null)
1 ignored issue
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        return 'images/'.dechex((int) date('Y') - 2010).'/'.dechex(date('W'));
92
    }
93
94
    /**
95
     * Get image output filename.
96
     *
97
     * @param  \Intervention\Image\Image  $image
98
     * @param  string|null  $identifier
99
     * @return string
100
     */
101
    protected function getImageFilename($image, $identifier = null)
1 ignored issue
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        return md5($image).Helper::fileExtensionForMimeType($image->mime(), '.');
104
    }
105
}
106