Passed
Push — master ( 18b32f...fa2b73 )
by Jens
08:15
created

ExtensionsActionBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 13
eloc 25
dl 0
loc 105
ccs 24
cts 32
cp 0.75
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getActions() 0 3 1
A setKey() 0 4 1
A changeDestination() 0 4 1
A of() 0 3 1
A resolveAction() 0 12 4
A setActions() 0 4 1
A callback() 0 6 2
A addAction() 0 4 1
A changeTriggers() 0 4 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
11
class ExtensionsActionBuilder
12
{
13
    private $actions = [];
14
15
    /**
16
     * @link https://docs.commercetools.com/http-api-projects-api-extensions.html#change-destination
17
     * @param ExtensionChangeDestinationAction|callable $action
18
     * @return $this
19
     */
20 1
    public function changeDestination($action = null)
21
    {
22 1
        $this->addAction($this->resolveAction(ExtensionChangeDestinationAction::class, $action));
23 1
        return $this;
24
    }
25
26
    /**
27
     * @link https://docs.commercetools.com/http-api-projects-api-extensions.html#change-triggers
28
     * @param ExtensionChangeTriggersAction|callable $action
29
     * @return $this
30
     */
31 1
    public function changeTriggers($action = null)
32
    {
33 1
        $this->addAction($this->resolveAction(ExtensionChangeTriggersAction::class, $action));
34 1
        return $this;
35
    }
36
37
    /**
38
     * @link https://docs.commercetools.com/http-api-projects-api-extensions.html#set-key
39
     * @param ExtensionSetKeyAction|callable $action
40
     * @return $this
41
     */
42 1
    public function setKey($action = null)
43
    {
44 1
        $this->addAction($this->resolveAction(ExtensionSetKeyAction::class, $action));
45 1
        return $this;
46
    }
47
48
    /**
49
     * @return ExtensionsActionBuilder
50
     */
51
    public function of()
52
    {
53
        return new self();
54
    }
55
56
    /**
57
     * @param $class
58
     * @param $action
59
     * @return AbstractAction
60
     * @throws InvalidArgumentException
61
     */
62 3
    private function resolveAction($class, $action = null)
63
    {
64 3
        if (is_null($action) || is_callable($action)) {
65 3
            $callback = $action;
66 3
            $emptyAction = $class::of();
67 3
            $action = $this->callback($emptyAction, $callback);
68
        }
69 3
        if ($action instanceof $class) {
70 3
            return $action;
71
        }
72
        throw new InvalidArgumentException(
73
            sprintf('Expected method to be called with or callable to return %s', $class)
74
        );
75
    }
76
77
    /**
78
     * @param $action
79
     * @param callable $callback
80
     * @return AbstractAction
81
     */
82 3
    private function callback($action, callable $callback = null)
83
    {
84 3
        if (!is_null($callback)) {
85
            $action = $callback($action);
86
        }
87 3
        return $action;
88
    }
89
90
    /**
91
     * @param AbstractAction $action
92
     * @return $this;
93
     */
94 3
    public function addAction(AbstractAction $action)
95
    {
96 3
        $this->actions[] = $action;
97 3
        return $this;
98
    }
99
100
    /**
101
     * @return array
102
     */
103 3
    public function getActions()
104
    {
105 3
        return $this->actions;
106
    }
107
108
    /**
109
     * @param array $actions
110
     * @return $this
111
     */
112
    public function setActions(array $actions)
113
    {
114
        $this->actions = $actions;
115
        return $this;
116
    }
117
}
118