CompositeAction::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Action;
13
14
use LightSaml\Context\ContextInterface;
15
16
class CompositeAction implements ActionInterface, DebugPrintTreeActionInterface, CompositeActionInterface
17
{
18
    /** @var ActionInterface[] */
19
    protected $children = array();
20
21
    /**
22
     * @param ActionInterface[] $children
23
     */
24 12
    public function __construct(array $children = array())
25
    {
26 12
        foreach ($children as $action) {
27 5
            $this->add($action);
28
        }
29 12
    }
30
31
    /**
32
     * @return ActionInterface[]
33
     */
34 6
    public function getChildren()
35
    {
36 6
        return $this->children;
37
    }
38
39
    /**
40
     * @param ActionInterface $action
41
     *
42
     * @return CompositeAction
43
     */
44 11
    public function add(ActionInterface $action)
45
    {
46 11
        $this->children[] = $action;
47
48 11
        return $this;
49
    }
50
51
    /**
52
     * @param callable $callable
53
     *
54
     * @return ActionInterface|null
55
     */
56 1
    public function map($callable)
57
    {
58 1
        foreach ($this->children as $k => $action) {
59 1
            $newAction = call_user_func($callable, $action);
60 1
            if ($newAction) {
61 1
                $this->children[$k] = $newAction;
62
            }
63
        }
64 1
    }
65
66
    /**
67
     * @param ContextInterface $context
68
     *
69
     * @return void
70
     */
71 6
    public function execute(ContextInterface $context)
72
    {
73 6
        foreach ($this->children as $action) {
74 6
            $action->execute($context);
75
        }
76 6
    }
77
78
    /**
79
     * @return array
80
     */
81 2
    public function debugPrintTree()
82
    {
83 2
        $arr = array();
84 2
        foreach ($this->children as $childAction) {
85 2 View Code Duplication
            if ($childAction instanceof DebugPrintTreeActionInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86 2
                $arr = array_merge($arr, $childAction->debugPrintTree());
87
            } else {
88 2
                $arr = array_merge($arr, array(get_class($childAction) => array()));
89
            }
90
        }
91
92
        $result = array(
93 2
            static::class => $arr,
94
        );
95
96 2
        return $result;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 1
    public function __toString()
103
    {
104 1
        return json_encode($this->debugPrintTree(), JSON_PRETTY_PRINT);
105
    }
106
}
107