Passed
Push — develop ( c162b5...4afdc8 )
by Jens
20:26
created

StoresActionBuilder::callback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace Commercetools\Core\Builder\Update;
4
5
use Commercetools\Core\Error\InvalidArgumentException;
6
use Commercetools\Core\Request\AbstractAction;
7
use Commercetools\Core\Request\Stores\Command\StoreSetLanguagesAction;
8
use Commercetools\Core\Request\Stores\Command\StoreSetNameAction;
9
10
class StoresActionBuilder
11
{
12
    private $actions = [];
13
14
    /**
15
     * @link https://docs.commercetools.com/http-api-projects-stores#set-languages
16
     * @param StoreSetLanguagesAction|callable $action
17
     * @return $this
18
     */
19 1
    public function setLanguages($action = null)
20
    {
21 1
        $this->addAction($this->resolveAction(StoreSetLanguagesAction::class, $action));
22 1
        return $this;
23
    }
24
25
    /**
26
     * @link https://docs.commercetools.com/http-api-projects-stores#set-name
27
     * @param StoreSetNameAction|callable $action
28
     * @return $this
29
     */
30 1
    public function setName($action = null)
31
    {
32 1
        $this->addAction($this->resolveAction(StoreSetNameAction::class, $action));
33 1
        return $this;
34
    }
35
36
    /**
37
     * @return StoresActionBuilder
38
     */
39
    public static function of()
40
    {
41
        return new self();
42
    }
43
44
    /**
45
     * @param $class
46
     * @param $action
47
     * @return AbstractAction
48
     * @throws InvalidArgumentException
49
     */
50 2
    private function resolveAction($class, $action = null)
51
    {
52 2
        if (is_null($action) || is_callable($action)) {
53 2
            $callback = $action;
54 2
            $emptyAction = $class::of();
55 2
            $action = $this->callback($emptyAction, $callback);
56
        }
57 2
        if ($action instanceof $class) {
58 2
            return $action;
59
        }
60
        throw new InvalidArgumentException(
61
            sprintf('Expected method to be called with or callable to return %s', $class)
62
        );
63
    }
64
65
    /**
66
     * @param $action
67
     * @param callable $callback
68
     * @return AbstractAction
69
     */
70 2
    private function callback($action, callable $callback = null)
71
    {
72 2
        if (!is_null($callback)) {
73
            $action = $callback($action);
74
        }
75 2
        return $action;
76
    }
77
78
    /**
79
     * @param AbstractAction $action
80
     * @return $this;
81
     */
82 2
    public function addAction(AbstractAction $action)
83
    {
84 2
        $this->actions[] = $action;
85 2
        return $this;
86
    }
87
88
    /**
89
     * @return array
90
     */
91 2
    public function getActions()
92
    {
93 2
        return $this->actions;
94
    }
95
96
97
    /**
98
     * @param array $actions
99
     * @return $this
100
     */
101
    public function setActions(array $actions)
102
    {
103
        $this->actions = $actions;
104
        return $this;
105
    }
106
}
107