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

ZonesActionBuilder::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 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
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\Zones\Command\ZoneAddLocationAction;
8
use Commercetools\Core\Request\Zones\Command\ZoneChangeNameAction;
9
use Commercetools\Core\Request\Zones\Command\ZoneRemoveLocationAction;
10
use Commercetools\Core\Request\Zones\Command\ZoneSetDescriptionAction;
11
12
class ZonesActionBuilder
13
{
14
    private $actions = [];
15
16
    /**
17
     * @link https://docs.commercetools.com/http-api-projects-zones.html#add-location
18
     * @param ZoneAddLocationAction|callable $action
19
     * @return $this
20
     */
21 1
    public function addLocation($action = null)
22
    {
23 1
        $this->addAction($this->resolveAction(ZoneAddLocationAction::class, $action));
24 1
        return $this;
25
    }
26
27
    /**
28
     * @link https://docs.commercetools.com/http-api-projects-zones.html#change-name
29
     * @param ZoneChangeNameAction|callable $action
30
     * @return $this
31
     */
32 1
    public function changeName($action = null)
33
    {
34 1
        $this->addAction($this->resolveAction(ZoneChangeNameAction::class, $action));
35 1
        return $this;
36
    }
37
38
    /**
39
     * @link https://docs.commercetools.com/http-api-projects-zones.html#remove-location
40
     * @param ZoneRemoveLocationAction|callable $action
41
     * @return $this
42
     */
43 1
    public function removeLocation($action = null)
44
    {
45 1
        $this->addAction($this->resolveAction(ZoneRemoveLocationAction::class, $action));
46 1
        return $this;
47
    }
48
49
    /**
50
     * @link https://docs.commercetools.com/http-api-projects-zones.html#set-description
51
     * @param ZoneSetDescriptionAction|callable $action
52
     * @return $this
53
     */
54 1
    public function setDescription($action = null)
55
    {
56 1
        $this->addAction($this->resolveAction(ZoneSetDescriptionAction::class, $action));
57 1
        return $this;
58
    }
59
60
    /**
61
     * @return ZonesActionBuilder
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