HasStrategyTrait::newStrategy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 2
b 1
f 1
1
<?php
2
3
namespace ByTIC\MediaLibrary\Collections\UploadStrategy\Traits;
4
5
use ByTIC\MediaLibrary\Collections\UploadStrategy\AbstractStrategy;
6
use ByTIC\MediaLibrary\Collections\UploadStrategy\GenericStrategy;
7
use ByTIC\MediaLibrary\Collections\UploadStrategy\OriginalNameStrategy;
8
9
/**
10
 * Trait HasStrategyTrait.
11
 */
12
trait HasStrategyTrait
13
{
14
    /**
15
     * @var null|AbstractStrategy
16
     */
17
    protected $strategy = null;
18
19
    /**
20
     * @return AbstractStrategy|null
21
     */
22 2
    public function getStrategy()
23
    {
24 2
        if ($this->strategy === null) {
25 2
            $this->initStrategy();
26
        }
27
28 2
        return $this->strategy;
29
    }
30
31
    /**
32
     * @param AbstractStrategy|null $strategy
33
     */
34 2
    public function setStrategy($strategy)
35
    {
36 2
        $this->strategy = $strategy;
37 2
    }
38
39 2
    protected function initStrategy()
40
    {
41 2
        $this->setStrategy($this->generateStrategy());
42 2
    }
43
44
    /**
45
     * @return AbstractStrategy
46
     */
47 2
    protected function generateStrategy()
48
    {
49 2
        $strategy = $this->newStrategy();
50
51 2
        return $strategy;
52
    }
53
54
    /**
55
     * @return AbstractStrategy
56
     */
57 2
    protected function newStrategy()
58
    {
59 2
        $class = $this->getStrategyClassName();
60
61 2
        return new $class();
62
    }
63
64
    /**
65
     * @return string
66
     */
67 2
    protected function getStrategyClassName()
68
    {
69 2
        $mediaType = $this->getMediaType();
0 ignored issues
show
Bug introduced by
It seems like getMediaType() 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

69
        /** @scrutinizer ignore-call */ 
70
        $mediaType = $this->getMediaType();
Loading history...
70
        switch ($mediaType) {
71 2
            case 'images':
72
                return GenericStrategy::class;
73
74
        }
75
76 2
        return OriginalNameStrategy::class;
77
    }
78
}
79