Completed
Push — master ( b29d94...537a9c )
by Andrey
06:59
created

ThumbnailTrait::getThumbnailModel()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 12
rs 9.8666
c 0
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
        $url = $this->getDefaultThumbUrl();
32
33
        if (empty($url)) {
34
            return null;
35
        }
36
37
        if (empty($options['alt'])) {
38
            $options['alt'] = $this->thumbnailModel->alt;
39
        }
40
41
        return Html::img($url, $options);
42
    }
43
44
    /**
45
     * Get default url to thumbnail file.
46
     *
47
     * @return null|string
48
     */
49
    public function getDefaultThumbUrl()
50
    {
51
        if (null == $this->getThumbnailModel()){
52
            return null;
53
        }
54
55
        return $this->thumbnailModel->getThumbUrl(MFUModule::THUMB_ALIAS_DEFAULT);
56
    }
57
58
    /**
59
     * Get model's thumbnail.
60
     *
61
     * @return array|null|\yii\db\ActiveRecord|Mediafile
62
     */
63
    public function getThumbnailModel()
64
    {
65
        if (null === $this->id) {
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
            return null;
67
        }
68
69
        if ($this->thumbnailModel === null) {
70
            $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
71
        }
72
73
        return $this->thumbnailModel;
74
    }
75
}