Failed Conditions
Pull Request — experimental/3.1 (#2222)
by chihiro
94:46
created

Loader::loadFromDirectory()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 3
nop 1
dl 0
loc 43
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Doctrine\Common\CsvDataFixtures;
4
5
use Doctrine\Common\DataFixtures\FixtureInterface;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Yaml\Yaml;
8
9
10
/**
11
 * CSVファイルのローダー.
12
 *
13
 * @see https://github.com/doctrine/data-fixtures/blob/master/lib/Doctrine/Common/DataFixtures/Loader.php
14
 */
15
class Loader
16
{
17
    protected $fixtures;
18
19
    /**
20
     * Load fixtures from directory.
21
     *
22
     * 同一階層に, Fixture のロード順を定義した definition.yml が必要.
23
     *
24
     * @param string $dir
25
     * @return array fixtures.
26
     */
27
    public function loadFromDirectory($dir)
28
    {
29
        if (!dir($dir)) {
30
            throw new \InvalidArgumentException(sprintf('"%s" does not exist', $dir));
31
        }
32
33
        // import順序の定義ファイルを取得.
34
        $file = $dir.'/definition.yml';
35
        if (!file_exists($file)) {
36
            throw new \InvalidArgumentException(sprintf('"%s" does not exist', $file));
37
        }
38
        $definition = Yaml::parse(file_get_contents($file));
39
        $definition = array_flip($definition);
40
41
        $finder = Finder::create()
42
            ->in($dir)
43
            ->name('*.csv')
44
            ->sort(
45
                // 定義ファイルに記載の順にソート.
46
                function (\SplFileInfo $a, \SplFileInfo $b) use ($definition) {
47
                    if (!isset($definition[$a->getFilename()])) {
48
                        throw new \Exception(sprintf('"%s" is undefined in %s', $a->getFilename()));
49
                    }
50
                    if (!isset($definition[$b->getFilename()])) {
51
                        throw new \Exception(sprintf('"%s" is undefined in %s', $b->getFilename()));
52
                    }
53
54
                    $a_rank = $definition[$a->getFilename()];
55
                    $b_rank = $definition[$b->getFilename()];
56
57
                    if ($a_rank < $b_rank) {
58
                        return -1;
59
                    } elseif ($a_rank > $b_rank) {
60
                        return 1;
61
                    } else {
62
                        return 0;
63
                    }
64
                }
65
            )
66
            ->files();
67
68
        return $this->loadFromIterator($finder->getIterator());
0 ignored issues
show
Bug introduced by
It seems like $finder->getIterator() targeting Symfony\Component\Finder\Finder::getIterator() can also be of type array<integer,object<Sym...nt\Finder\SplFileInfo>>; however, Eccube\Doctrine\Common\C...der::loadFromIterator() does only seem to accept object<Iterator>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
69
    }
70
71
    /**
72
     * Load fixtures from Iterator.
73
     *
74
     * @param \Iterator $Iterator Iterator of \SplFileInfo 
75
     * @return array fixtures.
76
     */
77
    public function loadFromIterator(\Iterator $Iterator)
78
    {
79
        $fixtures = [];
80
        foreach ($Iterator as $fixture) {
81
            // TODO $fixture が \SplFileInfo ではない場合の対応
82
            $CsvFixture = new CsvFixture($fixture->openFile());
83
            $this->addFixture($CsvFixture);
84
            $fixtures[] = $CsvFixture;
85
        }
86
        return $fixtures;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
87
    }
88
89
    public function getFixtures()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
90
    {
91
        return $this->fixtures;
92
    }
93
94
    public function addFixture(FixtureInterface $fixture)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
95
    {
96
        $this->fixtures[] = $fixture;
97
    }
98
}
99