Completed
Push — master ( c0e2c7...7b6048 )
by Song
02:52 queued 11s
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
     *
41
     * @return static
42
     */
43
    public static function make($steps, $current = null): self
44
    {
45
        return new static($steps, $current);
46
    }
47
48
    /**
49
     * @param array      $steps
50
     * @param string|int $current
51
     *
52
     * @return string|int
53
     */
54
    protected function resolveCurrentStep($steps, $current)
55
    {
56
        $current = $current ?: request($this->stepName, 0);
57
58
        if (!isset($steps[$current])) {
59
            $current = key($steps);
60
        }
61
62
        return $current;
63
    }
64
65
    /**
66
     * @return string|null
67
     */
68
    public function render()
69
    {
70
        $class = $this->steps[$this->current];
71
72
        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...
73
            admin_error("Class [{$class}] must be a sub-class of [Encore\Admin\Widgets\StepForm].");
74
75
            return;
76
        }
77
78
        /** @var StepForm $step */
79
        $step = new $class();
80
81
        return $step
82
            ->setSteps(array_keys($this->steps))
83
            ->setCurrent($this->current)
84
            ->render();
85
    }
86
}
87