CompositeActionBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 22
c 1
b 0
f 0
dl 0
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 17 5
A build() 0 13 3
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\Builder\Action;
13
14
use LightSaml\Action\ActionInterface;
15
use LightSaml\Action\CompositeAction;
16
17
class CompositeActionBuilder implements ActionBuilderInterface
18
{
19
    /**
20
     * int priority => ActionInterface[].
21
     *
22
     * @var array
23
     */
24
    private $actions = [];
25
26
    /** @var int */
27
    protected $increaseStep = 5;
28
29
    /** @var int */
30
    private $biggestPriority = 0;
31
32
    /**
33
     * @param int|bool $priority
34
     *
35
     * @return CompositeActionBuilder
36
     */
37
    public function add(ActionInterface $action, $priority = false)
38
    {
39
        if (false === $priority) {
40
            ++$this->biggestPriority;
41
            $priority = $this->biggestPriority;
42
        } elseif (false === is_int($priority)) {
43
            throw new \InvalidArgumentException('Expected integer value for priority');
44
        } elseif ($priority > $this->biggestPriority) {
45
            $this->biggestPriority = $priority;
0 ignored issues
show
Documentation Bug introduced by
The property $biggestPriority was declared of type integer, but $priority is of type true. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
46
        }
47
48
        if (false === isset($this->actions[$priority])) {
49
            $this->actions[$priority] = [];
50
        }
51
        $this->actions[$priority][] = $action;
52
53
        return $this;
54
    }
55
56
    /**
57
     * @return CompositeAction
58
     */
59
    public function build()
60
    {
61
        $actions = $this->actions;
62
        ksort($actions);
63
64
        $result = new CompositeAction();
65
        foreach ($actions as $arr) {
66
            foreach ($arr as $action) {
67
                $result->add($action);
68
            }
69
        }
70
71
        return $result;
72
    }
73
}
74