HasDefaultMediaTrait::generateDefaultMedia()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 8
c 2
b 1
f 0
nc 5
nop 0
dl 0
loc 15
ccs 0
cts 7
cp 0
crap 30
rs 9.6111
1
<?php
2
3
namespace ByTIC\MediaLibrary\Collections\Traits;
4
5
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
6
use ByTIC\MediaLibrary\Media\Media;
7
use ByTIC\MediaLibrary\Support\MediaModels;
8
use Nip\Records\Record;
9
use function asset;
10
11
/**
12
 * Trait HasDefaultMediaTrait.
13
 *
14
 * @method HasMediaTrait|Record getRecord
15
 */
16
trait HasDefaultMediaTrait
17
{
18
    /**
19
     * @var null|Media
20
     */
21
    protected $defaultMedia = null;
22
23
    /**
24
     * @param Media $media
25
     */
26
    public function setDefaultMedia($media)
27
    {
28
        $this->defaultMedia = $media;
29
    }
30
31
    /**
32
     * @param $mediaName
33
     *
34
     * @return mixed
35
     */
36
    public function persistDefaultMediaFromName($mediaName)
37
    {
38
        $media = $this->get($mediaName, null);
0 ignored issues
show
Bug introduced by
It seems like get() 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

38
        /** @scrutinizer ignore-call */ 
39
        $media = $this->get($mediaName, null);
Loading history...
39
        if ($media) {
40
            $this->setDefaultMedia($media);
41
42
            return $this->persistDefaultMedia();
43
        }
44
    }
45
46
    public function persistDefaultMedia()
47
    {
48
        $media = $this->getDefaultMedia();
49
50
        if (false === $media->getFile()->exists()) {
51
            return;
52
        }
53
        if (method_exists($this->getRecord(), 'persistDefaultMedia')) {
54
            return $this->getRecord()->persistDefaultMedia($this, $media);
55
        }
56
57
        $propertiesRecord = MediaModels::properties()->forCollection($this);
58
        $propertiesRecord->defaultMedia($media->getName());
59
        $propertiesRecord->save();
60
    }
61
62
    /**
63
     * @return Media|null
64
     */
65
    public function getDefaultMedia()
66
    {
67
        if ($this->defaultMedia === null) {
68
            $this->initDefaultMedia();
69
        }
70
71
        return $this->defaultMedia;
72
    }
73
74
    protected function initDefaultMedia()
75
    {
76
        $this->setDefaultMedia($this->generateDefaultMedia());
77
    }
78
79
    /**
80
     * @return Media
81
     */
82
    public function generateDefaultMedia()
83
    {
84
        $propertiesRecord = MediaModels::properties()->forCollection($this);
85
        if ($propertiesRecord) {
86
            $defaultMedia = $propertiesRecord->defaultMedia();
87
            if ($defaultMedia && $this->has($defaultMedia)) {
0 ignored issues
show
Bug introduced by
It seems like has() 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

87
            if ($defaultMedia && $this->/** @scrutinizer ignore-call */ has($defaultMedia)) {
Loading history...
88
                return $this->get($defaultMedia);
89
            }
90
        }
91
92
        if (count($this->items)) {
93
            return reset($this->items);
94
        }
95
96
        return $this->compileDefaultMedia();
97
    }
98
99
    /**
100
     * @return Media
101
     */
102
    protected function compileDefaultMedia()
103
    {
104
        $media = $this->newMedia();
0 ignored issues
show
Bug introduced by
It seems like newMedia() 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

104
        /** @scrutinizer ignore-call */ 
105
        $media = $this->newMedia();
Loading history...
105
106
        return $media;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getDefaultMediaUrl()
113
    {
114
        if (method_exists($this->getRecord(), 'getDefaultMediaUrl')) {
115
            return $this->getRecord()->getDefaultMediaUrl($this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRecord(...tDefaultMediaUrl($this) returns the type Nip\Records\AbstractMode...\Collections\Collection which is incompatible with the documented return type string.
Loading history...
116
        }
117
118
        if (method_exists($this->getRecord()->getManager(), 'getDefaultMediaUrl')) {
0 ignored issues
show
Bug introduced by
It seems like getManager() 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

118
        if (method_exists($this->getRecord()->/** @scrutinizer ignore-call */ getManager(), 'getDefaultMediaUrl')) {
Loading history...
119
            return $this->getRecord()->getManager()->getDefaultMediaUrl($this);
120
        }
121
122
        return $this->getDefaultMediaGenericUrl();
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getDefaultMediaGenericUrl()
129
    {
130
        return asset('/images/'
131
            . $this->getRecord()->getManager()->getTable() . '/'
132
            . $this->getDefaultFileName());
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getDefaultFileName()
139
    {
140
        $name = inflector()->singularize($this->getName());
0 ignored issues
show
Bug introduced by
It seems like getName() 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

140
        $name = inflector()->singularize($this->/** @scrutinizer ignore-call */ getName());
Loading history...
141
        $extension = $this->getName() == 'logos' ? 'png' : 'jpg';
142
143
        return $name . '.' . $extension;
144
    }
145
}
146