Passed
Push — master ( e515a3...13e6ec )
by Vladimir
02:11
created

TinifyResize::buildAlgorithmInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/Vintage-web-production/yii2-tinify
4
 * @copyright Copyright (c) 2017 Vintage Web Production
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace vintage\tinify\components;
9
10
use Yii;
11
use yii\base\Object;
12
use yii\base\InvalidConfigException;
13
use Tinify\Source;
14
use vintage\tinify\algorithms\Cover;
15
use vintage\tinify\algorithms\Fit;
16
use vintage\tinify\algorithms\Scale;
17
use vintage\tinify\helpers\TinifyData;
18
19
/**
20
 * Component for image resizing.
21
 *
22
 * @author Vladimir Kuprienko <[email protected]>
23
 * @since 1.1
24
 */
25
class TinifyResize extends Object
26
{
27
    /**
28
     * @var string
29
     */
30
    protected $fileName;
31
    /**
32
     * @var string
33
     */
34
    protected $algorithm;
35
    /**
36
     * @var int
37
     */
38
    protected $width;
39
    /**
40
     * @var int
41
     */
42
    protected $height;
43
44
45
    /**
46
     * @inheritdoc
47
     * @param string $fileName
48
     * @throws InvalidConfigException
49
     */
50
    public function __construct($fileName, $config = [])
51
    {
52
        $this->fileName = Yii::getAlias($fileName);
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::getAlias($fileName) can also be of type boolean. However, the property $fileName is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
53
        if (!TinifyData::allowCompression($this->fileName)) {
54
            throw new InvalidConfigException('You can resize only "jpg" and "png" images');
55
        }
56
57
        parent::__construct($config);
58
    }
59
60
    /**
61
     * Sets algorithm 'scale'.
62
     *
63
     * @return self
64
     */
65
    public function scale()
66
    {
67
        $this->algorithm = Scale::className();
68
        return $this;
69
    }
70
71
    /**
72
     * Sets algorithm 'fit'.
73
     *
74
     * @return self
75
     */
76
    public function fit()
77
    {
78
        $this->algorithm = Fit::className();
79
        return $this;
80
    }
81
82
    /**
83
     * Sets algorithm 'cover'.
84
     *
85
     * @return self
86
     */
87
    public function cover()
88
    {
89
        $this->algorithm = Cover::className();
90
        return $this;
91
    }
92
93
    /**
94
     * Sets width.
95
     *
96
     * @param int $value
97
     * @return self
98
     */
99
    public function width($value)
100
    {
101
        $this->width = $value;
102
        return $this;
103
    }
104
105
    /**
106
     * Sets height.
107
     *
108
     * @param int $value
109
     * @return self
110
     */
111
    public function height($value)
112
    {
113
        $this->height = $value;
114
        return $this;
115
    }
116
117
    /**
118
     * Resize image.
119
     *
120
     * @throws InvalidConfigException
121
     */
122
    public function process()
123
    {
124
        if ($this->width == null && $this->height == null) {
125
            throw new InvalidConfigException('You should to set "width" or "height" ');
126
        }
127
        if ($this->algorithm == null) {
128
            $this->scale();
129
        }
130
131
        $source = Source::fromFile($this->fileName);
132
        $source->resize($this->buildAlgorithmInstance()->getConfig())
133
            ->toFile($this->fileName);
134
    }
135
136
    /**
137
     * Creates algorithm instance.
138
     *
139
     * @return \vintage\tinify\algorithms\TinifyAlgorithmInterface
140
     */
141
    protected function buildAlgorithmInstance()
142
    {
143
        return Yii::createObject([
144
            'class' => $this->algorithm,
145
            'width' => $this->width,
146
            'height' => $this->height,
147
        ]);
148
    }
149
}
150