Passed
Push — master ( ec7862...f8d20d )
by Jean Paul
10:53
created

TxtExtractor::extract()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 23
rs 9.9
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Extractors;
4
5
use Coco\SourceWatcher\Core\Extractor;
6
use Coco\SourceWatcher\Core\IO\Inputs\FileInput;
7
use Coco\SourceWatcher\Core\Row;
8
use Coco\SourceWatcher\Core\SourceWatcherException;
9
10
class TxtExtractor extends Extractor
11
{
12
    protected string $column;
13
14
    protected array $availableOptions = [ "column" ];
15
16
    public function __construct ()
17
    {
18
        $this->column = "";
19
    }
20
21
    /**
22
     * @return string
23
     */
24
    public function getColumn () : string
25
    {
26
        return $this->column;
27
    }
28
29
    /**
30
     * @param string $column
31
     */
32
    public function setColumn ( string $column ) : void
33
    {
34
        $this->column = $column;
35
    }
36
37
    /**
38
     * @return array
39
     * @throws SourceWatcherException
40
     */
41
    public function extract () : array
42
    {
43
        if ( $this->input == null ) {
44
            throw new SourceWatcherException( "An input must be provided." );
45
        }
46
47
        $inputIsFileInput = $this->input instanceof FileInput;
48
49
        if ( !$inputIsFileInput ) {
50
            throw new SourceWatcherException( sprintf( "The input must be an instance of %s", FileInput::class ) );
51
        }
52
53
        $this->result = [];
54
55
        $fileHandler = fopen( $this->input->getInput(), "r" );
56
57
        while ( $currentFileLine = fgets( $fileHandler ) ) {
58
            array_push( $this->result, new Row( [ $this->column => trim( $currentFileLine ) ] ) );
59
        }
60
61
        fclose( $fileHandler );
62
63
        return $this->result;
64
    }
65
}
66