Passed
Push — master ( 5379a4...a2888f )
by Joao
04:49
created

src/Repository/FixedTextFileDataset.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace ByJG\AnyDataset\Repository;
4
5
use ByJG\AnyDataset\Enum\FixedTextDefinition;
6
use ByJG\AnyDataset\Exception\DatasetException;
7
use ByJG\AnyDataset\Exception\NotFoundException;
8
use Exception;
9
use InvalidArgumentException;
10
11
class FixedTextFileDataset
12
{
13
14
    protected $_source;
15
16
    /**
17
     * @var FixedTextDefinition[]
18
     */
19
    protected $_fieldDefinition;
20
    protected $_sourceType;
21
22
    /**
23
     * Text File Data Set
24
     *
25
     * @param string $source
26
     * @param FixedTextDefinition[] $fieldDefinition
27
     * @throws NotFoundException
28
     */
29
    public function __construct($source, $fieldDefinition)
30
    {
31
        if (!is_array($fieldDefinition)) {
32
            throw new InvalidArgumentException("You must define an array of field definition.");
33
        }
34
35
        $this->_source = $source;
36
        $this->_sourceType = "HTTP";
37
38
        if (!preg_match("~^https?://~", $source)) {
39
            if (!file_exists($this->_source)) {
40
                throw new NotFoundException("The specified file " . $this->_source . " does not exists");
41
            }
42
43
            $this->_sourceType = "FILE";
44
        }
45
46
        $this->_fieldDefinition = $fieldDefinition;
47
    }
48
49
    /**
50
     * @access public
51
     * @return DBIterator
52
     * @throws DatasetException
53
     * @throws Exception
54
     */
55 2
    public function getIterator()
56
    {
57 2
        if ($this->_sourceType == "HTTP") {
58
            return $this->getIteratorHttp();
59
        } else {
60 2
            return $this->getIteratorFile();
61
        }
62
    }
63
64
    protected function getIteratorHttp()
65
    {
66
        // Expression Regular:
67
        // [1]: http or ftp
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
68
        // [2]: Server name
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
        // [3]: Full Path
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70
        $pat = "/(http|ftp|https):\/\/([\w+|\.]+)/i";
71
        $urlParts = preg_split($pat, $this->_source, -1, PREG_SPLIT_DELIM_CAPTURE);
72
73
        $handle = fsockopen($urlParts[2], 80, $errno, $errstr, 30);
74
        if (!$handle) {
75
            throw new DatasetException("TextFileDataset Socket error: $errstr ($errno)");
76
        } else {
77
            $out = "GET " . $urlParts[4] . " HTTP/1.1\r\n";
78
            $out .= "Host: " . $urlParts[2] . "\r\n";
79
            $out .= "Connection: Close\r\n\r\n";
80
81
            fwrite($handle, $out);
82
83
            try {
84
                $it = new FixedTextFileIterator($handle, $this->_fieldDefinition);
85
                return $it;
86
            } catch (Exception $ex) {
87
                fclose($handle);
88
                throw $ex;
89
            }
90
        }
91
    }
92
93
    protected function getIteratorFile()
94
    {
95
        $handle = fopen($this->_source, "r");
96
        if (!$handle) {
97
            throw new DatasetException("TextFileDataset File open error");
98
        } else {
99
            try {
100
                $it = new FixedTextFileIterator($handle, $this->_fieldDefinition);
101
                return $it;
102
            } catch (Exception $ex) {
103
                fclose($handle);
104
                throw $ex;
105
            }
106
        }
107
    }
108
}
109