GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( d226c8...d4855c )
by Ivan
10:07
created

Thumbnail   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 150
Duplicated Lines 9.33 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 2
cbo 2
dl 14
loc 150
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A rules() 0 8 1
A behaviors() 0 9 1
A attributeLabels() 0 9 1
A getImageThumbnailBySize() 0 21 3
B createThumbnail() 0 28 4
A afterSave() 0 16 4
A afterDelete() 14 14 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace app\modules\image\models;
4
5
use app\behaviors\ImageExist;
6
use Imagine\Image\Box;
7
use Imagine\Image\ImageInterface;
8
use League\Flysystem\Filesystem;
9
use League\Flysystem\Util;
10
use Yii;
11
use yii\helpers\ArrayHelper;
12
use yii\imagine\Image as Imagine;
13
use yii\web\BadRequestHttpException;
14
15
/**
16
 * This is the model class for table "{{%thumbnail}}".
17
 * @property integer $id
18
 * @property integer $img_id
19
 * @property string $thumb_path
20
 * @property integer $size_id
21
 */
22
class Thumbnail extends \yii\db\ActiveRecord
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    public static function tableName()
28
    {
29
        return '{{%thumbnail}}';
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function rules()
36
    {
37
        return [
38
            [['img_id', 'thumb_path', 'size_id'], 'required'],
39
            [['img_id', 'size_id'], 'integer'],
40
            [['thumb_path'], 'string', 'max' => 255]
41
        ];
42
    }
43
44
    public function behaviors()
45
    {
46
        return [
47
            [
48
                'class' => ImageExist::className(),
49
                'srcAttrName' => 'thumb_path',
50
            ]
51
        ];
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function attributeLabels()
58
    {
59
        return [
60
            'id' => Yii::t('app', 'ID'),
61
            'img_id' => Yii::t('app', 'Img ID'),
62
            'thumb_path' => Yii::t('app', 'Thumb Src'),
63
            'size_id' => Yii::t('app', 'Size ID'),
64
        ];
65
    }
66
67
    /**
68
     * Return thumb of image by size or create if not exist
69
     * @param $image Image
70
     * @param $size ThumbnailSize
71
     * @return Thumbnail
72
     */
73
    public static function getImageThumbnailBySize($image, $size)
74
    {
75
        $cacheKey = 'thumbBySize:'.$image->id . ';'.$size->id;
76
        $thumb = Yii::$app->cache->get($cacheKey);
77
        if ($thumb === false) {
78
            $thumb = static::findOne(['img_id' => $image->id, 'size_id' => $size->id]);
79
            if ($thumb === null) {
80
                $thumb = new Thumbnail;
81
                $thumb->setAttributes(
82
                    [
83
                        'img_id' => $image->id,
84
                        'size_id' => $size->id,
85
                    ]
86
                );
87
                $thumb->thumb_path = static::createThumbnail($image, $size);
88
                $thumb->save();
89
            }
90
            Yii::$app->cache->set($cacheKey, $thumb, 86400);
91
        }
92
        return $thumb;
93
    }
94
95
    /**
96
     * Create thumbnail in fs
97
     * @param $image Image
98
     * @param $size ThumbnailSize
99
     * @return string|false
100
     */
101
    public static function createThumbnail($image, $size)
102
    {
103
        try {
104
            /** @var Filesystem $fs */
105
            $fs = Yii::$app->getModule('image')->fsComponent;
106
107
            $file = Imagine::getImagine()->read($fs->readStream($image->filename));
108
            /** @var ImageInterface $thumb */
109
            if($size->resize_mode == ThumbnailSize::RESIZE){
110
                $thumb = $file->resize(new Box($size->width, $size->height));
111
            }else{
112
                $thumb = $file->thumbnail(new Box($size->width, $size->height), $size->resize_mode);    
113
            }
114
            
115
            $path = Yii::$app->getModule('image')->thumbnailsDirectory;
116
117
            if (!preg_match('#^(?<name>.+)\.(?<ext>[^\.]+)$#', $image->filename, $fileInfo)) {
118
                return false;
119
            }
120
121
            $stream = $thumb->get($fileInfo['ext'], ['quality' => $size->quality]);
122
            $src = "$path/{$fileInfo['name']}-{$size->width}x{$size->height}.{$fileInfo['ext']}";
123
            $fs->put($src, $stream);
124
            return $src;
125
        } catch (\Exception $e) {
126
            return false;
127
        }
128
    }
129
130
    /**
131
     * @inheritdoc
132
     * @param bool $insert
133
     * @param array $changedAttributes
134
     * @throws BadRequestHttpException
135
     */
136
    public function afterSave($insert, $changedAttributes)
137
    {
138
        parent::afterSave($insert, $changedAttributes);
139
        if (Yii::$app->getModule('image')->useWatermark == 1) {
140
            /** @var ThumbnailSize $size */
141
            $size = ThumbnailSize::findOne(ArrayHelper::getValue($this, 'size_id', 0));
142
            if ($size !== null) {
143
                $watermark = Watermark::findOne($size->default_watermark_id);
144
                if ($watermark !== null) {
145
                    ThumbnailWatermark::getThumbnailWatermark($this, $watermark);
146
                }
147
            } else {
148
                throw new BadRequestHttpException(Yii::t('app', 'Set thumbnail size'));
149
            }
150
        }
151
    }
152
153
    /**
154
     * @inheritdoc
155
     * @throws \Exception
156
     */
157 View Code Duplication
    public function afterDelete()
158
    {
159
        parent::afterDelete();
160
        $sameImages = static::findAll(['thumb_path' => $this->thumb_path]);
161
        if (empty($sameImages) === true) {
162
            if (Yii::$app->getModule('image')->fsComponent->has($this->thumb_path)) {
163
                Yii::$app->getModule('image')->fsComponent->delete($this->thumb_path);
164
            }
165
            $thumbnailWatermarks = ThumbnailWatermark::findAll(['thumb_id' => $this->id]);
166
            foreach ($thumbnailWatermarks as $thumbnailWatermark) {
167
                $thumbnailWatermark->delete();
168
            }
169
        }
170
    }
171
}
172