Completed
Push — master ( 87e0a5...68b11c )
by Markus
10:00
created

StringOrFileList::checkStreamWrapper()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace Mathielen\ImportEngineBundle\DependencyInjection;
4
5
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
6
7
class StringOrFileList extends \ArrayObject
8
{
9
    public function __construct(array $listOrStringsOrFiles)
10
    {
11
        foreach ($listOrStringsOrFiles as $k => &$v) {
12
            if (filter_var($v, FILTER_VALIDATE_URL)) {
13
                //nothing
14
            } elseif (is_dir($v)) {
15
                $iterator = new RecursiveDirectoryIterator($v, \FilesystemIterator::KEY_AS_PATHNAME);
16
                $iterator = new \RecursiveIteratorIterator($iterator);
17
18
                /** @var \Symfony\Component\Finder\SplFileInfo $file */
19
                foreach ($iterator as $file) {
20
                    if (is_file($file)) {
21
                        $listOrStringsOrFiles[$file->getRelativePathname()] = $file;
22
                    }
23
                }
24
25
                unset($listOrStringsOrFiles[$k]);
26
            } elseif (is_file($v)) {
27
                $v = new \SplFileInfo($v);
28
            } else {
29
                throw new \RuntimeException("Unknown value type $v");
30
            }
31
        }
32
33
        parent::__construct($listOrStringsOrFiles);
34
    }
35
36
    public function offsetGet($offset)
37
    {
38
        //offset could be myfile.sql?targetconnection
39
        $url = parse_url($offset);
40
        $offset = $url['path'];
41
42
        if (!parent::offsetExists($offset)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetExists() instead of offsetGet()). Are you sure this is correct? If so, you might want to change this to $this->offsetExists().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
43
            return $this->checkStreamWrapper($offset);
44
        }
45
46
        $v = parent::offsetGet($offset);
47
        if ($v instanceof \SplFileInfo) {
48
            return file_get_contents($v);
49
        }
50
51
        return $v;
52
    }
53
54
    private function checkStreamWrapper($offset)
55
    {
56
        foreach ($this as $k => &$v) {
57
            if (filter_var($v, FILTER_VALIDATE_URL)) {
58
                $path = $v.'/'.$offset;
59
60
                if (file_exists($path)) {
61
                    return file_get_contents($path);
62
                }
63
            }
64
        }
65
66
        throw new \InvalidArgumentException("Item with id '$offset' could not be found.");
67
    }
68
}