Completed
Push — master ( 3cc478...c0e2c7 )
by Song
06:54 queued 04:37
created

MultipleSteps::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Widgets;
4
5
use Illuminate\Contracts\Support\Renderable;
6
7
class MultipleSteps implements Renderable
8
{
9
    /**
10
     * @var int|string
11
     */
12
    protected $current;
13
14
    /**
15
     * @var array
16
     */
17
    protected $steps = [];
18
19
    /**
20
     * @var string
21
     */
22
    protected $stepName = 'step';
23
24
    /**
25
     * MultipleSteps constructor.
26
     *
27
     * @param array $steps
28
     * @param null $current
29
     */
30
    public function __construct($steps = [], $current = null)
31
    {
32
        $this->steps = $steps;
33
34
        $this->current = $this->resolveCurrentStep($steps, $current);
35
    }
36
37
    /**
38
     * @param array $steps
39
     * @param null $current
40
     * @return static
41
     */
42
    public static function make($steps, $current = null): MultipleSteps
43
    {
44
        return new static($steps, $current);
45
    }
46
47
    /**
48
     * @param array $steps
49
     * @param string|int $current
50
     *
51
     * @return string|int
52
     */
53
    protected function resolveCurrentStep($steps, $current)
54
    {
55
        $current = $current ?: request($this->stepName, 0);
56
57
        if (!isset($steps[$current])) {
58
            $current = key($steps);
59
        }
60
61
        return $current;
62
    }
63
64
    /**
65
     * @return string|null
66
     */
67
    public function render()
68
    {
69
        $class = $this->steps[$this->current];
70
71
        if (!is_subclass_of($class, StepForm::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Encore\Admin\Widgets\StepForm::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
72
            admin_error("Class [{$class}] must be a sub-class of [Encore\Admin\Widgets\StepForm].");
73
            return;
74
        }
75
76
        /** @var StepForm $step */
77
        $step = new $class;
78
79
        return $step
80
            ->setSteps(array_keys($this->steps))
81
            ->setCurrent($this->current)
82
            ->render();
83
    }
84
}