HasStrategy::newStrategy()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 3
c 2
b 1
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ByTIC\Namefy\Generator;
4
5
use ByTIC\Namefy\Strategies\AbstractStrategy;
6
7
/**
8
 * Trait HasStrategy
9
 * @package ByTIC\Namefy\Generator
10
 */
11
trait HasStrategy
12
{
13
    /**
14
     * @var null|AbstractStrategy
15
     */
16
    protected $strategy = null;
17
18
    /**
19
     * @return AbstractStrategy|null
20
     */
21
    public function getStrategy(): ?AbstractStrategy
22
    {
23
        return $this->strategy;
24
    }
25
26
    /**
27
     * @param AbstractStrategy|string $strategy
28
     * @return self
29
     */
30
    public function setStrategy($strategy): self
31
    {
32
        if (is_string($strategy)) {
33
            $strategy = $this->newStrategy($strategy);
34
        }
35
        $this->strategy = $strategy;
36
        return $this;
37
    }
38
39
    /**
40
     * @param string $class
41
     * @return AbstractStrategy
42
     */
43
    protected function newStrategy($class): AbstractStrategy
44
    {
45
        if (!class_exists($class)) {
46
            $class = '\\' . dirname(AbstractStrategy::class) . $class . 'Strategy';
47
        }
48
        return new $class();
49
    }
50
}
51