Completed
Push — 2.1 ( b44a46...4c2160 )
by
unknown
12:30
created

FileFixtureTrait::loadData()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 5
nop 1
crap 6
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\test;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
13
/**
14
 * FileFixtureTrait provides functionalities for loading data fixture from file.
15
 *
16
 * @author Leandro Guindani Gehlen <[email protected]>
17
 * @since 2.0.14
18
 */
19
trait FileFixtureTrait
20
{
21
    /**
22
     * @var string the directory path or [path alias](guide:concept-aliases) that contains the fixture data
23
     */
24
    public $dataDirectory;
25
    /**
26
     * @var string|bool the file path or [path alias](guide:concept-aliases) of the data file that contains the fixture data
27
     * to be returned by [[getData()]]. You can set this property to be false to prevent loading any data.
28
     */
29
    public $dataFile;
30
31
    /**
32
     * Returns the fixture data.
33
     *
34
     * The default implementation will try to return the fixture data by including the external file specified by [[dataFile]].
35
     * The file should return the data array that will be stored in [[data]] after inserting into the database.
36
     *
37
     * @param string $file the data file path
38
     * @return array the data to be put into the database
39
     * @throws InvalidConfigException if the specified data file does not exist.
40
     */
41 19
    protected function loadData($file)
42
    {
43 19
        if ($file === false || $file === null) {
44 1
            return [];
45
        }
46
47 18
        if (basename($file) === $file && $this->dataDirectory !== null) {
48 4
            $file = $this->dataDirectory . '/' . $file;
49
        }
50
51 18
        $file = Yii::getAlias($file);
52 18
        if (is_file($file)) {
53 17
            return require $file;
54
        }
55
56 1
        throw new InvalidConfigException("Fixture data file does not exist: {$file}");
57
    }
58
59
}
60