ActionFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 51
ccs 0
cts 22
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A registerAction() 0 4 1
A createAction() 0 12 2
1
<?php
2
3
namespace Majora\Framework\Domain\Action;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Majora\Framework\Domain\Action\ActionInterface;
7
8
/**
9
 * Factory class for domain actions
10
 */
11
class ActionFactory
12
{
13
    /**
14
     * @var ArrayCollection
15
     */
16
    protected $actions;
17
18
    /**
19
     * Construct
20
     *
21
     * @param array $actions
22
     */
23
    public function __construct(array $actions = array())
24
    {
25
        $this->actions = new ArrayCollection();
26
        foreach ($actions as $name => $action) {
27
            $this->registerAction($name, $action);
28
        }
29
    }
30
31
    /**
32
     * Register an action under given name
33
     *
34
     * @param string          $name
35
     * @param ActionInterface $action
36
     */
37
    public function registerAction($name, ActionInterface $action)
38
    {
39
        $this->actions->set($name, $action);
40
    }
41
42
    /**
43
     * Creates and return a new action under given name
44
     *
45
     * @param string $name
46
     *
47
     * @return ActionInterface
48
     */
49
    public function createAction($name)
50
    {
51
        if (!$this->actions->containsKey($name)) {
52
            throw new \InvalidArgumentException(sprintf(
53
                'Any action registered under "%s" name, only ["%s"] are.',
54
                $name,
55
                implode('","', $this->actions->getKeys())
56
            ));
57
        }
58
59
        return clone $this->actions->get($name);
60
    }
61
}
62