Completed
Push — master ( 586b8e...cb0e4b )
by De Cramer
04:39 queued 53s
created

AbstractScriptMethod::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\ScriptMethods;
4
5
use eXpansion\Framework\Core\DataProviders\MethodScriptDataProviderInterface;
6
7
/**
8
 * Class AbstractScriptMethod
9
 *
10
 * @author    de Cramer Oliver<[email protected]>
11
 * @copyright 2017 Smile
12
 * @package eXpansion\Framework\Core\ScriptMethods
13
 */
14
class AbstractScriptMethod
15
{
16
    /** @var array | null */
17
    protected $currentData = null;
18
19
    /** @var bool  */
20
    protected $callMade = false;
21
22
    /** @var MethodScriptDataProviderInterface */
23
    protected $dataProvider;
24
25
    /** @var callback[] */
26
    protected $toDispatch = [];
27
28
    /**
29
     * Get TM.Scores or SM.Scores.
30
     *
31
     * @param callback $function
32
     *
33
     * @return void
34
     */
35
    public function get($function)
36
    {
37
        $this->toDispatch[] = $function;
38
39
        if (is_null($this->currentData) && !$this->callMade) {
40
            $this->callMade = true;
41
            $this->dataProvider->request();
42
            return;
43
        }
44
45
        $this->dispatchData();
46
    }
47
48
49
    /**
50
     * Set current stores.
51
     *
52
     * @param array $scores
53
     *
54
     * @return mixed
55
     */
56
    public function set($scores)
57
    {
58
        $this->currentData = $scores;
59
        $this->dispatchData();
60
    }
61
62
    /**
63
     * Set current data provider.
64
     *
65
     * @param MethodScriptDataProviderInterface $dataProvider
66
     *
67
     * @return mixed
68
     */
69
    public function setCurrentDataProvider(MethodScriptDataProviderInterface $dataProvider)
70
    {
71
        $this->dataProvider = $dataProvider;
72
    }
73
74
    /**
75
     * Dispatch data.
76
     */
77
    protected function dispatchData()
78
    {
79
        foreach ($this->toDispatch as $callback) {
80
            $callback($this->currentData);
81
        }
82
83
        $this->toDispatch = [];
84
        $this->callMade = false;
85
    }
86
}
87