RemoveElementAction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A execute() 0 17 4
1
<?php
2
/**
3
 * NextFlow (http://github.com/nextflow)
4
 *
5
 * @link http://github.com/nextflow/nextflow-php for the canonical source repository
6
 * @copyright Copyright (c) 2014-2016 NextFlow (http://github.com/nextflow)
7
 * @license https://raw.github.com/nextflow/nextflow-php/master/LICENSE MIT
8
 */
9
10
namespace NextFlow\Arrays\Action;
11
12
use NextFlow\Core\Action\AbstractAction;
13
14
/**
15
 * Removes an element from the array.
16
 */
17
final class RemoveElementAction extends AbstractAction
18
{
19
    /** The output action socket. */
20
    const SOCKET_OUTPUT = 'out';
21
22
    /** The array variable socket. */
23
    const SOCKET_ARRAY = 'array';
24
25
    /** The element variable socket. */
26
    const SOCKET_ELEMENT = 'element';
27
28
    /**
29
     * Initializes a new instance of this class.
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
35
        $this->createSocket(self::SOCKET_OUTPUT);
36
        $this->createSocket(self::SOCKET_ARRAY);
37
        $this->createSocket(self::SOCKET_ELEMENT);
38
    }
39
40
    /**
41
     * Executes the node's logic.
42
     */
43
    public function execute()
44
    {
45
        $array = $this->getSocket(self::SOCKET_ARRAY)->getNode(0);
46
        if ($array === null) {
47
            throw new \InvalidArgumentException('No array variable provided.');
48
        }
49
50
        $element = $this->getSocket(self::SOCKET_ELEMENT)->getNode(0);
51
        if ($element === null || $element->getValue() === null) {
52
            throw new \InvalidArgumentException('No element variable provided.');
53
        }
54
55
        $newArray = array_diff($array->getValue(), array($element->getValue()));
56
        $array->setValue($newArray);
57
58
        $this->activate(self::SOCKET_OUTPUT);
59
    }
60
}
61