Passed
Push — develop ( 3e43c3...c162b5 )
by Barbara
22:49 queued 16s
created

StoresActionBuilder::setLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 2
cts 2
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\Stores\Command\StoreSetLanguagesAction;
8
use Commercetools\Core\Request\Stores\Command\StoreSetNameAction;
9
10
class StoresActionBuilder
11
{
12
    private $actions = [];
13
14
    /**
15
     * @link https://docs.commercetools.com/http-api-projects-stores#set-languages
16
     * @param StoreSetLanguagesAction|callable $action
17
     * @return $this
18 1
     */
19
    public function setLanguages($action = null)
20 1
    {
21 1
        $this->addAction($this->resolveAction(StoreSetLanguagesAction::class, $action));
22
        return $this;
23
    }
24
25
    /**
26
     * @link https://docs.commercetools.com/http-api-projects-stores#set-name
27
     * @param StoreSetNameAction|callable $action
28
     * @return $this
29
     */
30
    public function setName($action = null)
31
    {
32
        $this->addAction($this->resolveAction(StoreSetNameAction::class, $action));
33
        return $this;
34
    }
35
36
    /**
37
     * @return StoresActionBuilder
38 1
     */
39
    public static function of()
40 1
    {
41 1
        return new self();
42 1
    }
43 1
44
    /**
45 1
     * @param $class
46 1
     * @param $action
47
     * @return AbstractAction
48
     * @throws InvalidArgumentException
49
     */
50
    private function resolveAction($class, $action = null)
51
    {
52
        if (is_null($action) || is_callable($action)) {
53
            $callback = $action;
54
            $emptyAction = $class::of();
55
            $action = $this->callback($emptyAction, $callback);
56
        }
57
        if ($action instanceof $class) {
58 1
            return $action;
59
        }
60 1
        throw new InvalidArgumentException(
61
            sprintf('Expected method to be called with or callable to return %s', $class)
62
        );
63 1
    }
64
65
    /**
66
     * @param $action
67
     * @param callable $callback
68
     * @return AbstractAction
69
     */
70 1
    private function callback($action, callable $callback = null)
71
    {
72 1
        if (!is_null($callback)) {
73 1
            $action = $callback($action);
74
        }
75
        return $action;
76
    }
77
78
    /**
79 1
     * @param AbstractAction $action
80
     * @return $this;
81 1
     */
82
    public function addAction(AbstractAction $action)
83
    {
84
        $this->actions[] = $action;
85
        return $this;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getActions()
92
    {
93
        return $this->actions;
94
    }
95
96
97
    /**
98
     * @param array $actions
99
     * @return $this
100
     */
101
    public function setActions(array $actions)
102
    {
103
        $this->actions = $actions;
104
        return $this;
105
    }
106
}
107