Passed
Push — develop ( f53d0b...3a55e6 )
by Jens
09:08
created

CustomerGroupsActionBuilder::setActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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\CustomerGroups\Command\CustomerGroupChangeNameAction;
8
use Commercetools\Core\Request\CustomerGroups\Command\CustomerGroupSetCustomFieldAction;
9
use Commercetools\Core\Request\CustomerGroups\Command\CustomerGroupSetCustomTypeAction;
10
use Commercetools\Core\Request\CustomerGroups\Command\CustomerGroupSetKeyAction;
11
12
class CustomerGroupsActionBuilder
13
{
14
    private $actions = [];
15
16
    /**
17
     * @link https://docs.commercetools.com/http-api-projects-customerGroups.html#change-name
18
     * @param CustomerGroupChangeNameAction|callable $action
19
     * @return $this
20
     */
21 1
    public function changeName($action = null)
22
    {
23 1
        $this->addAction($this->resolveAction(CustomerGroupChangeNameAction::class, $action));
24 1
        return $this;
25
    }
26
27
    /**
28
     *
29
     * @param CustomerGroupSetCustomFieldAction|callable $action
30
     * @return $this
31
     */
32 1
    public function setCustomField($action = null)
33
    {
34 1
        $this->addAction($this->resolveAction(CustomerGroupSetCustomFieldAction::class, $action));
35 1
        return $this;
36
    }
37
38
    /**
39
     *
40
     * @param CustomerGroupSetCustomTypeAction|callable $action
41
     * @return $this
42
     */
43 1
    public function setCustomType($action = null)
44
    {
45 1
        $this->addAction($this->resolveAction(CustomerGroupSetCustomTypeAction::class, $action));
46 1
        return $this;
47
    }
48
49
    /**
50
     * @link https://docs.commercetools.com/http-api-projects-customerGroups.html#set-key
51
     * @param CustomerGroupSetKeyAction|callable $action
52
     * @return $this
53
     */
54 1
    public function setKey($action = null)
55
    {
56 1
        $this->addAction($this->resolveAction(CustomerGroupSetKeyAction::class, $action));
57 1
        return $this;
58
    }
59
60
    /**
61
     * @return CustomerGroupsActionBuilder
62
     */
63
    public function of()
64
    {
65
        return new self();
66
    }
67
68
    /**
69
     * @param $class
70
     * @param $action
71
     * @return AbstractAction
72
     * @throws InvalidArgumentException
73
     */
74 4
    private function resolveAction($class, $action = null)
75
    {
76 4
        if (is_null($action) || is_callable($action)) {
77 4
            $callback = $action;
78 4
            $emptyAction = $class::of();
79 4
            $action = $this->callback($emptyAction, $callback);
80
        }
81 4
        if ($action instanceof $class) {
82 4
            return $action;
83
        }
84
        throw new InvalidArgumentException(
85
            sprintf('Expected method to be called with or callable to return %s', $class)
86
        );
87
    }
88
89
    /**
90
     * @param $action
91
     * @param callable $callback
92
     * @return AbstractAction
93
     */
94 4
    private function callback($action, callable $callback = null)
95
    {
96 4
        if (!is_null($callback)) {
97
            $action = $callback($action);
98
        }
99 4
        return $action;
100
    }
101
102
    /**
103
     * @param AbstractAction $action
104
     * @return $this;
105
     */
106 4
    public function addAction(AbstractAction $action)
107
    {
108 4
        $this->actions[] = $action;
109 4
        return $this;
110
    }
111
112
    /**
113
     * @return array
114
     */
115 4
    public function getActions()
116
    {
117 4
        return $this->actions;
118
    }
119
120
    /**
121
     * @param array $actions
122
     * @return $this
123
     */
124
    public function setActions(array $actions)
125
    {
126
        $this->actions = $actions;
127
        return $this;
128
    }
129
}
130