MultiStepManager   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 91
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A disk() 0 6 2
A get() 0 4 1
A resolve() 0 15 3
A getConfig() 0 4 2
A createSessionDriver() 0 4 1
A createDatabaseDriver() 0 4 1
A createJsonDriver() 0 4 1
A createStepSystem() 0 4 1
A getDefaultDriver() 0 4 1
A __call() 0 4 1
1
<?php
2
3
namespace Infinitypaul\MultiStep\Controller;
4
5
use Infinitypaul\MultiStep\MultiStepSystem;
6
use Infinitypaul\MultiStep\Store\Contracts\StepStorage;
7
use Infinitypaul\MultiStep\Store\DatabaseStorage;
8
use Infinitypaul\MultiStep\Store\JsonOutput;
9
use Infinitypaul\MultiStep\Store\SessionStorage;
10
use InvalidArgumentException;
11
12
class MultiStepManager
13
{
14
    protected $app;
15
16
    /**
17
     * The array of resolved filesystem drivers.
18
     *
19
     * @var array
20
     */
21
    protected $disks = [];
22
23
    public function __construct($app)
24
    {
25
        $this->app = $app;
26
    }
27
28
    public function disk($name = null)
29
    {
30
        $name = $name ?: $this->getDefaultDriver();
31
32
        return $this->disks[$name] = $this->get($name);
33
    }
34
35
    protected function get($name)
36
    {
37
        return $this->disks[$name] ?? $this->resolve($name);
38
    }
39
40
    protected function resolve($name)
41
    {
42
        $config = $this->getConfig();
43
        if (is_null($config)) {
44
            throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver.");
45
        }
46
47
        $driverMethod = 'create'.ucfirst($name).'Driver';
48
49
        if (method_exists($this, $driverMethod)) {
50
            return $this->{$driverMethod}($config);
51
        } else {
52
            throw new InvalidArgumentException("Driver [{$name}] is not supported.");
53
        }
54
    }
55
56
    protected function getConfig()
57
    {
58
        return $this->getDefaultDriver() ?: null;
59
    }
60
61
    protected function createSessionDriver($config)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        return $this->createStepSystem(new SessionStorage(request()));
64
    }
65
66
    protected function createDatabaseDriver($config)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        return $this->createStepSystem(new DatabaseStorage);
69
    }
70
71
    protected function createJsonDriver()
72
    {
73
        return $this->createStepSystem(new JsonOutput);
74
    }
75
76
    protected function createStepSystem(StepStorage $stepStorage)
77
    {
78
        return new MultiStepSystem($stepStorage);
79
    }
80
81
    /**
82
     * Get the default file driver.
83
     *
84
     * @return string
85
     */
86
    protected function getDefaultDriver()
87
    {
88
        return $this->app['config']['steps.default'];
89
    }
90
91
    /**
92
     * Dynamically call the default driver instance.
93
     *
94
     * @param  string  $method
95
     * @param  array  $parameters
96
     * @return mixed
97
     */
98
    public function __call($method, $parameters)
99
    {
100
        return $this->disk()->$method(...$parameters);
101
    }
102
}
103