Test Failed
Push — master ( 87b409...65f6df )
by Filippo
21:44 queued 16:15
created

StrategyFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A strategyInstance() 0 9 2
A __construct() 0 3 1
A buildInstance() 0 7 2
A getInstance() 0 7 2
A buildFrom() 0 12 2
1
<?php
2
3
namespace Jobtech\LaravelChunky\Strategies;
4
5
use Illuminate\Container\Container;
6
use Jobtech\LaravelChunky\ChunkySettings;
7
use Jobtech\LaravelChunky\Exceptions\ChunkyException;
8
use Jobtech\LaravelChunky\Exceptions\StrategyException;
9
use Jobtech\LaravelChunky\Strategies\Contracts\StrategyFactory as StrategyFactoryContract;
10
11
class StrategyFactory implements StrategyFactoryContract
12
{
13
    private static $instance;
14
    /**
15
     * @var \Jobtech\LaravelChunky\ChunkySettings
16
     */
17
    private ChunkySettings $settings;
18
19
    public function __construct(ChunkySettings $settings)
20
    {
21
        $this->settings = $settings;
22
    }
23
24
    /**
25
     * Returns a new instance for the given strategy class.
26
     *
27
     * @param string                                         $strategy
28
     *
29
     * @return mixed
30
     */
31
    private function buildInstance(string $strategy)
32
    {
33
        if (! method_exists($strategy, 'instance')) {
34
            throw new StrategyException('Cannot instantiate strategy instance');
35
        }
36
37
        return $strategy::instance();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function strategyInstance()
44
    {
45
        $default = $this->settings->defaultMergeStrategy();
46
47
        if ($default === null) {
0 ignored issues
show
introduced by
The condition $default === null is always false.
Loading history...
48
            throw new StrategyException('Undefined default strategy');
49
        }
50
51
        return $this->buildInstance($default);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function buildFrom($chunksManager = null, $mergeManager = null)
58
    {
59
        $strategy = $this->strategyInstance();
60
61
        if ($strategy === null) {
62
            throw new ChunkyException('Undefined strategy for mime type or no default strategy');
63
        }
64
65
        $strategy->chunksManager($chunksManager);
66
        $strategy->mergeManager($mergeManager);
67
68
        return $strategy;
69
    }
70
71
    public static function getInstance(): StrategyFactory
72
    {
73
        if (static::$instance === null) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
74
            static::$instance = Container::getInstance()->make(StrategyFactoryContract::class);
75
        }
76
77
        return static::$instance;
78
    }
79
}
80