Completed
Push — master ( 5a0d9f...69c9b8 )
by Caio
03:17
created

AttacherManager::writeImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
namespace CbCaio\ImgAttacher;
3
4
use CbCaio\ImgAttacher\Contracts\AttacherImageContract;
5
use CbCaio\ImgAttacher\Managers\FileManager;
6
use CbCaio\ImgAttacher\Managers\FilePathManager;
7
use CbCaio\ImgAttacher\Processors\ImageProcessor;
8
use Intervention\Image\Image;
9
10
class AttacherManager
11
{
12
    protected $processing_styles_routine;
13
    protected $base_url;
14
    protected $path_to_save;
15
16
    public function __construct()
17
    {
18
        $this->processing_styles_routine = app('config')->get('img-attacher')['processing_styles_routines'];
19
    }
20
21
    /**
22
     * @return bool
23
     */
24
    public function writeImage(AttacherImageContract $attacherImage)
25
    {
26
        $imageProcessor = $this->getImageProcessor();
27
28
        $images_to_save = $imageProcessor->applyStyles($attacherImage, $this->getProcessingStylesRoutines());
29
30
        $needToUpdate = $attacherImage->hasDifferentFileName();
31
32
        if ($needToUpdate)
33
        {
34
            $this->getFileManager()->updateMany($images_to_save, $attacherImage);
0 ignored issues
show
Bug introduced by
It seems like $images_to_save defined by $imageProcessor->applySt...essingStylesRoutines()) on line 28 can be null; however, CbCaio\ImgAttacher\Manag...leManager::updateMany() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
35
        } else
36
        {
37
            $this->getFileManager()->saveMany($images_to_save, $attacherImage);
0 ignored issues
show
Bug introduced by
It seems like $images_to_save defined by $imageProcessor->applySt...essingStylesRoutines()) on line 28 can be null; however, CbCaio\ImgAttacher\Manag...FileManager::saveMany() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
38
        }
39
40
        return TRUE;
41
    }
42
43
    /**
44
     * @param AttacherImageContract $attacherImage
45
     * @return bool
46
     */
47
    public function deleteImages(AttacherImageContract $attacherImage, $style = NULL)
48
    {
49
        $path = $attacherImage->getDeletePath($style);
50
51
        return $this->getFileManager()->delete($path);
52
    }
53
54
    /**
55
     * @return FileManager
56
     */
57
    public function getFileManager()
58
    {
59
        return app('img-attacher.FileManager');
60
    }
61
62
    /**
63
     * @return ImageProcessor
64
     */
65
    public function getImageProcessor()
66
    {
67
        return app('img-attacher.ImageProcessor');
68
    }
69
70
    /**
71
     * @return FilePathManager
72
     */
73
    public function getFilePathManager()
74
    {
75
        return app('img-attacher.FilePathManager');
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getProcessingStylesRoutines()
82
    {
83
        return $this->processing_styles_routine;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getBaseUrl()
90
    {
91
        return $this->getFilePathManager()->getBaseUrl();
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getPathToSave()
98
    {
99
        return $this->getFilePathManager()->getPath();
100
    }
101
}
102