StreamOpenAction::execute()   C
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 6.7272
cc 7
eloc 15
nc 4
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
 * Opens a stream.
17
 */
18
final class StreamOpenAction extends AbstractAction
19
{
20
    /** The output socket. */
21
    const SOCKET_OUTPUT = 'out';
22
23
    /** The filename variable socket. */
24
    const SOCKET_FILENAME = 'filename';
25
26
    /** The mode variable socket. */
27
    const SOCKET_MODE = 'mode';
28
29
    /** The stream variable socket. */
30
    const SOCKET_STREAM = 'stream';
31
32
    /**
33
     * Initializes a new instance of this class.
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct();
38
39
        $this->createSocket(self::SOCKET_OUTPUT);
40
        $this->createSocket(self::SOCKET_FILENAME);
41
        $this->createSocket(self::SOCKET_MODE);
42
        $this->createSocket(self::SOCKET_STREAM);
43
    }
44
45
    /**
46
     * Executes the node's logic.
47
     */
48
    public function execute()
49
    {
50
        $fileNameSocket = $this->getSocket(self::SOCKET_FILENAME);
51
        if (!$fileNameSocket || !$fileNameSocket->hasNodes()) {
52
            throw new InvalidArgumentException('There is no filename provided.');
53
        }
54
55
        $modeSocket = $this->getSocket(self::SOCKET_MODE);
56
        if (!$modeSocket || !$modeSocket->hasNodes()) {
57
            throw new InvalidArgumentException('There is no stream mode provided.');
58
        }
59
60
        $resourceSocket = $this->getSocket(self::SOCKET_STREAM);
61
        if (!$resourceSocket || !$resourceSocket->hasNodes()) {
62
            throw new InvalidArgumentException('There is no stream variable provided.');
63
        }
64
65
        $fileNameValue = $fileNameSocket->getNode(0)->getValue();
66
        $modeValue = $modeSocket->getNode(0)->getValue();
67
68
        $resource = fopen($fileNameValue, $modeValue);
69
        $resourceSocket->getNode(0)->setValue($resource);
70
71
        $this->activate(self::SOCKET_OUTPUT);
72
    }
73
}
74