MultiStepSystem   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 81
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A key() 0 4 1
A step() 0 7 1
A store() 0 6 1
A complete() 0 6 1
A notCompleted() 0 10 3
A data() 0 8 1
A clearAll() 0 6 1
A __get() 0 4 1
1
<?php
2
3
namespace Infinitypaul\MultiStep;
4
5
use Infinitypaul\MultiStep\Store\Contracts\StepStorage;
6
7
class MultiStepSystem
8
{
9
    protected $step;
10
    protected $name;
11
    protected $storage;
12
13
    /**
14
     * MultiStepSystem constructor.
15
     *
16
     * @param \Infinitypaul\MultiStep\Store\Contracts\StepStorage $storage
17
     */
18
    public function __construct(StepStorage $storage)
19
    {
20
        $this->storage = $storage;
21
    }
22
23
    protected function key()
24
    {
25
        return "multistep.{$this->name}";
26
    }
27
28
    /**
29
     * @param $name
30
     * @param $step
31
     *
32
     * @return \Infinitypaul\MultiStep\MultiStepSystem
33
     */
34
    public function step($name, $step)
35
    {
36
        $this->step = $step;
37
        $this->name = $name;
38
39
        return $this;
40
    }
41
42
    public function store($data)
43
    {
44
        $this->storage->put($this->key().".{$this->step}.data", $data);
45
46
        return $this;
47
    }
48
49
    public function complete()
50
    {
51
        $this->storage->put($this->key().".{$this->step}.complete", true);
52
53
        return $this;
54
    }
55
56
    public function notCompleted(...$steps)
57
    {
58
        foreach ($steps as $step) {
59
            if (! $this->storage->get($this->key().".{$step}.complete")) {
60
                return true;
61
            }
62
        }
63
64
        return false;
65
    }
66
67
    public function data()
68
    {
69
        $products = collect($this->storage->get($this->key()))->pluck('data');
70
71
        return $products->flatMap(function ($values) {
72
            return $values;
73
        });
74
    }
75
76
    public function clearAll()
77
    {
78
        $this->storage->forget($this->key());
79
80
        return $this;
81
    }
82
83
    public function __get($property)
84
    {
85
        return $this->storage->get($this->key().".{$this->step}.data.{$property}");
86
    }
87
}
88