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

ReviewsActionBuilder::transitionState()   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\Reviews\Command\ReviewSetAuthorNameAction;
8
use Commercetools\Core\Request\Reviews\Command\ReviewSetCustomFieldAction;
9
use Commercetools\Core\Request\Reviews\Command\ReviewSetCustomTypeAction;
10
use Commercetools\Core\Request\Reviews\Command\ReviewSetCustomerAction;
11
use Commercetools\Core\Request\Reviews\Command\ReviewSetKeyAction;
12
use Commercetools\Core\Request\Reviews\Command\ReviewSetLocaleAction;
13
use Commercetools\Core\Request\Reviews\Command\ReviewSetRatingAction;
14
use Commercetools\Core\Request\Reviews\Command\ReviewSetTargetAction;
15
use Commercetools\Core\Request\Reviews\Command\ReviewSetTextAction;
16
use Commercetools\Core\Request\Reviews\Command\ReviewSetTitleAction;
17
use Commercetools\Core\Request\Reviews\Command\ReviewTransitionStateAction;
18
19
class ReviewsActionBuilder
20
{
21
    private $actions = [];
22
23
    /**
24
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#set-author-name
25
     * @param ReviewSetAuthorNameAction|callable $action
26
     * @return $this
27
     */
28 1
    public function setAuthorName($action = null)
29
    {
30 1
        $this->addAction($this->resolveAction(ReviewSetAuthorNameAction::class, $action));
31 1
        return $this;
32
    }
33
34
    /**
35
     *
36
     * @param ReviewSetCustomFieldAction|callable $action
37
     * @return $this
38
     */
39 1
    public function setCustomField($action = null)
40
    {
41 1
        $this->addAction($this->resolveAction(ReviewSetCustomFieldAction::class, $action));
42 1
        return $this;
43
    }
44
45
    /**
46
     *
47
     * @param ReviewSetCustomTypeAction|callable $action
48
     * @return $this
49
     */
50 1
    public function setCustomType($action = null)
51
    {
52 1
        $this->addAction($this->resolveAction(ReviewSetCustomTypeAction::class, $action));
53 1
        return $this;
54
    }
55
56
    /**
57
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#set-customer
58
     * @param ReviewSetCustomerAction|callable $action
59
     * @return $this
60
     */
61 1
    public function setCustomer($action = null)
62
    {
63 1
        $this->addAction($this->resolveAction(ReviewSetCustomerAction::class, $action));
64 1
        return $this;
65
    }
66
67
    /**
68
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#set-key
69
     * @param ReviewSetKeyAction|callable $action
70
     * @return $this
71
     */
72 1
    public function setKey($action = null)
73
    {
74 1
        $this->addAction($this->resolveAction(ReviewSetKeyAction::class, $action));
75 1
        return $this;
76
    }
77
78
    /**
79
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#set-locale
80
     * @param ReviewSetLocaleAction|callable $action
81
     * @return $this
82
     */
83 1
    public function setLocale($action = null)
84
    {
85 1
        $this->addAction($this->resolveAction(ReviewSetLocaleAction::class, $action));
86 1
        return $this;
87
    }
88
89
    /**
90
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#set-rating
91
     * @param ReviewSetRatingAction|callable $action
92
     * @return $this
93
     */
94 1
    public function setRating($action = null)
95
    {
96 1
        $this->addAction($this->resolveAction(ReviewSetRatingAction::class, $action));
97 1
        return $this;
98
    }
99
100
    /**
101
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#set-target
102
     * @param ReviewSetTargetAction|callable $action
103
     * @return $this
104
     */
105 1
    public function setTarget($action = null)
106
    {
107 1
        $this->addAction($this->resolveAction(ReviewSetTargetAction::class, $action));
108 1
        return $this;
109
    }
110
111
    /**
112
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#set-text
113
     * @param ReviewSetTextAction|callable $action
114
     * @return $this
115
     */
116 1
    public function setText($action = null)
117
    {
118 1
        $this->addAction($this->resolveAction(ReviewSetTextAction::class, $action));
119 1
        return $this;
120
    }
121
122
    /**
123
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#set-title
124
     * @param ReviewSetTitleAction|callable $action
125
     * @return $this
126
     */
127 1
    public function setTitle($action = null)
128
    {
129 1
        $this->addAction($this->resolveAction(ReviewSetTitleAction::class, $action));
130 1
        return $this;
131
    }
132
133
    /**
134
     * @link https://docs.commercetools.com/http-api-projects-reviews.html#transition-state
135
     * @param ReviewTransitionStateAction|callable $action
136
     * @return $this
137
     */
138 1
    public function transitionState($action = null)
139
    {
140 1
        $this->addAction($this->resolveAction(ReviewTransitionStateAction::class, $action));
141 1
        return $this;
142
    }
143
144
    /**
145
     * @return ReviewsActionBuilder
146
     */
147
    public function of()
148
    {
149
        return new self();
150
    }
151
152
    /**
153
     * @param $class
154
     * @param $action
155
     * @return AbstractAction
156
     * @throws InvalidArgumentException
157
     */
158 11
    private function resolveAction($class, $action = null)
159
    {
160 11
        if (is_null($action) || is_callable($action)) {
161 11
            $callback = $action;
162 11
            $emptyAction = $class::of();
163 11
            $action = $this->callback($emptyAction, $callback);
164
        }
165 11
        if ($action instanceof $class) {
166 11
            return $action;
167
        }
168
        throw new InvalidArgumentException(
169
            sprintf('Expected method to be called with or callable to return %s', $class)
170
        );
171
    }
172
173
    /**
174
     * @param $action
175
     * @param callable $callback
176
     * @return AbstractAction
177
     */
178 11
    private function callback($action, callable $callback = null)
179
    {
180 11
        if (!is_null($callback)) {
181
            $action = $callback($action);
182
        }
183 11
        return $action;
184
    }
185
186
    /**
187
     * @param AbstractAction $action
188
     * @return $this;
189
     */
190 11
    public function addAction(AbstractAction $action)
191
    {
192 11
        $this->actions[] = $action;
193 11
        return $this;
194
    }
195
196
    /**
197
     * @return array
198
     */
199 11
    public function getActions()
200
    {
201 11
        return $this->actions;
202
    }
203
204
    /**
205
     * @param array $actions
206
     * @return $this
207
     */
208
    public function setActions(array $actions)
209
    {
210
        $this->actions = $actions;
211
        return $this;
212
    }
213
}
214