Passed
Push — master ( 22651e...d2ef54 )
by Andrey
07:31
created

ThumbnailTrait::getDefaultThumbImage()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 19
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
namespace app\traits;
4
5
use yii\helpers\Html;
6
use yii\db\ActiveRecord;
7
use Itstructure\MFUploader\Module as MFUModule;
8
use Itstructure\MFUploader\models\{Mediafile, OwnerMediafile};
9
10
/**
11
 * Class ThumbnailTrait
12
 *
13
 * @package app\traits
14
 */
15
trait ThumbnailTrait
16
{
17
    /**
18
     * @var array|null|ActiveRecord|Mediafile
19
     */
20
    protected $thumbnailModel;
21
22
    /**
23
     * Get album thumb image.
24
     *
25
     * @param array  $options
26
     *
27
     * @return mixed
28
     */
29
    public function getDefaultThumbImage(array $options = [])
30
    {
31
        $thumbnailModel = $this->getThumbnailModel();
32
33
        if (null === $thumbnailModel){
34
            return null;
35
        }
36
37
        $url = $thumbnailModel->getThumbUrl(MFUModule::THUMB_ALIAS_DEFAULT);
38
39
        if (empty($url)) {
40
            return null;
41
        }
42
43
        if (empty($options['alt'])) {
44
            $options['alt'] = $thumbnailModel->alt;
45
        }
46
47
        return Html::img($url, $options);
48
    }
49
50
    /**
51
     * Get model's thumbnail.
52
     *
53
     * @return array|null|\yii\db\ActiveRecord|Mediafile
54
     */
55
    public function getThumbnailModel()
56
    {
57
        if (null === $this->id) {
58
            return null;
59
        }
60
61
        if ($this->thumbnailModel === null) {
62
            $this->thumbnailModel = OwnerMediafile::getOwnerThumbnail($this->tableName(), $this->id);
0 ignored issues
show
Bug introduced by
It seems like tableName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

62
            $this->thumbnailModel = OwnerMediafile::getOwnerThumbnail($this->/** @scrutinizer ignore-call */ tableName(), $this->id);
Loading history...
63
        }
64
65
        return $this->thumbnailModel;
66
    }
67
}