Passed
Push — master ( 6e2a06...da0795 )
by Jens
14:26
created

ZonesActionBuilder::changeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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