AbstractNode::getParam()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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\Core\Node;
11
12
/**
13
 * The base final class for all node types.
14
 */
15
abstract class AbstractNode implements NodeInterface
16
{
17
    /**
18
     * The id of the node.
19
     *
20
     * @var string
21
     */
22
    private $id;
23
24
    /**
25
     * The parameters of this node.
26
     *
27
     * @var array
28
     */
29
    private $parameters;
30
31
    /**
32
     * The sockets that this node has.
33
     *
34
     * @var Socket[]
35
     */
36
    private $sockets;
37
38
    /**
39
     * The value of this node.
40
     *
41
     * @var mixed
42
     */
43
    private $value;
44
45
    /**
46
     * Initializes a new instance of this class.
47
     */
48
    public function __construct()
49
    {
50
        $this->parameters = array();
51
        $this->sockets = array();
52
    }
53
54
    /**
55
     * Gets the id of the node.
56
     *
57
     * @return string
58
     */
59
    public function getId()
60
    {
61
        if ($this->id === null) {
62
            $this->id = spl_object_hash($this);
63
        }
64
        return $this->id;
65
    }
66
67
    /**
68
     * Activates the socket with the given index.
69
     *
70
     * @param string $socket The name of the socket to activate.
71
     */
72
    protected function activate($socket)
73
    {
74
        $this->getSocket($socket)->activate();
75
    }
76
77
    /**
78
     * Binds the given node to a socket.
79
     *
80
     * @param string $socketName The name of the socket to bind to.
81
     * @param NodeInterface $node The node to bind to.
82
     * @return int
83
     */
84
    public function bind($socketName, NodeInterface $node)
85
    {
86
        $socket = $this->getSocket($socketName);
87
88
        $socket->addNode($node);
89
90
        return $socket->getNodeCount() - 1;
91
    }
92
93
    /**
94
     * Creates a socket with the given name.
95
     *
96
     * @param string $name The name of the scoket to create.
97
     */
98
    protected function createSocket($name)
99
    {
100
        if (array_key_exists($name, $this->sockets)) {
101
            throw new \InvalidArgumentException('The socket "' . $name . '" already exists.');
102
        }
103
104
        $this->sockets[$name] = new Socket($name);
105
    }
106
107
    /**
108
     * Gets the socket with the given index.
109
     *
110
     * @param string $name The name of the socket to get.
111
     * @return Socket
112
     */
113
    public function getSocket($name)
114
    {
115
        if (!$this->hasSocket($name)) {
116
            throw new \InvalidArgumentException('The socket "' . $name . '" does not exist.');
117
        }
118
119
        return $this->sockets[$name];
120
    }
121
122
    /**
123
     * Gets a list with all sockets that exist for this node.
124
     *
125
     * @return Socket[]
126
     */
127
    public function getSockets()
128
    {
129
        return $this->sockets;
130
    }
131
132
    /**
133
     * Checks if the node has a socket with the given name.
134
     *
135
     * @param string $name The name of the socket to check.
136
     * @return bool
137
     */
138
    public function hasSocket($name)
139
    {
140
        return array_key_exists($name, $this->sockets);
141
    }
142
143
    /**
144
     * Gets the parameter with the given name.
145
     *
146
     * @param string $name The name of the parameter to get.
147
     * @return string
148
     */
149
    public function getParam($name)
150
    {
151
        if (array_key_exists($name, $this->parameters)) {
152
            return $this->parameters[$name];
153
        }
154
        return null;
155
    }
156
157
    /**
158
     * Gets the parameters of this node.
159
     *
160
     * @return array
161
     */
162
    public function getParams()
163
    {
164
        return $this->parameters;
165
    }
166
167
    /**
168
     * Sets the parameter with the given name.
169
     *
170
     * @param string $name The name of the parameter to get.
171
     * @param mixed $value The value to set.
172
     */
173
    public function setParam($name, $value)
174
    {
175
        $this->parameters[$name] = $value;
176
    }
177
178
    /**
179
     * Gets the value of this node.
180
     *
181
     * @return mixed
182
     */
183
    public function getValue()
184
    {
185
        return $this->value;
186
    }
187
188
    /**
189
     * Sets the value of this node.
190
     *
191
     * @param mixed $value
192
     */
193
    public function setValue($value)
194
    {
195
        $this->value = $value;
196
    }
197
}
198