Passed
Push — master ( da0795...78880b )
by Jens
14:45 queued 18s
created

ExtensionsActionBuilder::setTimeoutInMs()   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
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
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\Extensions\Command\ExtensionChangeDestinationAction;
8
use Commercetools\Core\Request\Extensions\Command\ExtensionChangeTriggersAction;
9
use Commercetools\Core\Request\Extensions\Command\ExtensionSetKeyAction;
10
use Commercetools\Core\Request\Extensions\Command\ExtensionSetTimeoutInMsAction;
11
12
class ExtensionsActionBuilder
13
{
14
    private $actions = [];
15
16
    /**
17
     * @link https://docs.commercetools.com/http-api-projects-api-extensions.html#change-destination
18
     * @param ExtensionChangeDestinationAction|callable $action
19
     * @return $this
20
     */
21 1
    public function changeDestination($action = null)
22
    {
23 1
        $this->addAction($this->resolveAction(ExtensionChangeDestinationAction::class, $action));
24 1
        return $this;
25
    }
26
27
    /**
28
     * @link https://docs.commercetools.com/http-api-projects-api-extensions.html#change-triggers
29
     * @param ExtensionChangeTriggersAction|callable $action
30
     * @return $this
31
     */
32 1
    public function changeTriggers($action = null)
33
    {
34 1
        $this->addAction($this->resolveAction(ExtensionChangeTriggersAction::class, $action));
35 1
        return $this;
36
    }
37
38
    /**
39
     * @link https://docs.commercetools.com/http-api-projects-api-extensions.html#set-key
40
     * @param ExtensionSetKeyAction|callable $action
41
     * @return $this
42
     */
43 1
    public function setKey($action = null)
44
    {
45 1
        $this->addAction($this->resolveAction(ExtensionSetKeyAction::class, $action));
46 1
        return $this;
47
    }
48
49
    /**
50
     * @link https://docs.commercetools.com/http-api-projects-api-extensions.html#set-timeoutinms
51
     * @param ExtensionSetTimeoutInMsAction|callable $action
52
     * @return $this
53
     */
54 1
    public function setTimeoutInMs($action = null)
55
    {
56 1
        $this->addAction($this->resolveAction(ExtensionSetTimeoutInMsAction::class, $action));
57 1
        return $this;
58
    }
59
60
    /**
61
     * @return ExtensionsActionBuilder
62
     */
63
    public static 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
    /**
122
     * @param array $actions
123
     * @return $this
124
     */
125
    public function setActions(array $actions)
126
    {
127
        $this->actions = $actions;
128
        return $this;
129
    }
130
}
131