Completed
Push — master ( 2474ff...f4aaad )
by Sebastian
09:36 queued 14s
created

StandardInput   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createIterator() 0 14 3
1
<?php
2
3
/**
4
 * This file is part of SebastianFeldmann\Cli.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace SebastianFeldmann\Cli\Reader;
13
14
use Exception;
15
16
/**
17
 * StandardInput
18
 *
19
 * @package SebastianFeldmann\Cli
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/sebastianfeldmann/cli
22
 * @since   Class available since Release 3.3.0
23
 */
24
class StandardInput extends Abstraction
25
{
26
    /**
27
     * Standard Input stream handle
28
     *
29
     * @var resource
30
     */
31
    private $handle;
32
33
    /**
34
     * StandardInput constructor.
35
     *
36
     * @param $stdInHandle
37
     */
38
    public function __construct($stdInHandle)
39
    {
40
        $this->handle = $stdInHandle;
41
    }
42
43
    /**
44
     * Create the generator
45
     *
46
     * @return iterable
47
     * @throws \Exception
48
     */
49
    protected function createIterator(): iterable
50
    {
51
        $read   = [$this->handle];
52
        $write  = [];
53
        $except = [];
54
        $result = stream_select($read, $write, $except, 0);
55
56
        if ($result === false) {
57
            throw new Exception('stream_select failed');
58
        }
59
        if ($result !== 0) {
60
            yield stream_get_line($this->handle, null);
61
        }
62
    }
63
}
64