StreamCloseAction::execute()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 9
nc 3
nop 0
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\Stream\Action;
11
12
use InvalidArgumentException;
13
use NextFlow\Core\Action\AbstractAction;
14
15
/**
16
 * Closes a stream.
17
 */
18
final class StreamCloseAction extends AbstractAction
19
{
20
    /** The output socket. */
21
    const SOCKET_OUTPUT = 'out';
22
23
    /** The stream variable socket. */
24
    const SOCKET_STREAM = 'stream';
25
26
    /**
27
     * Initializes a new instance of this class.
28
     */
29
    public function __construct()
30
    {
31
        parent::__construct();
32
33
        $this->createSocket(self::SOCKET_OUTPUT);
34
        $this->createSocket(self::SOCKET_STREAM);
35
    }
36
37
    /**
38
     * Executes the node's logic.
39
     */
40
    public function execute()
41
    {
42
        $socket = $this->getSocket(self::SOCKET_STREAM);
43
        if (!$socket || !$socket->hasNodes()) {
44
            throw new InvalidArgumentException('There is no stream to close.');
45
        }
46
47
        $streamValue = $socket->getNode(0)->getValue();
48
        if (!is_resource($streamValue)) {
49
            throw new InvalidArgumentException('Invalid stream provided.');
50
        }
51
52
        fclose($streamValue);
53
54
        $this->activate(self::SOCKET_OUTPUT);
55
    }
56
}
57