Completed
Push — 21-merge-master-test ( 19c265...2d5475 )
by Alexander
13:06
created

FileFixtureTrait::loadData()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 7.551
c 0
b 0
f 0
cc 7
eloc 11
nc 7
nop 2
crap 7
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
     * @param bool $throwException whether to throw exception if fixture data file does not exist.
39
     * @return array the data to be put into the database
40
     * @throws InvalidConfigException if the specified data file does not exist.
41
     */
42 23
    protected function loadData($file, $throwException = true)
43
    {
44 23
        if ($file === null || $file === false) {
45 1
            return [];
46
        }
47
48 22
        if (basename($file) === $file && $this->dataDirectory !== null) {
49 4
            $file = $this->dataDirectory . '/' . $file;
50
        }
51
52 22
        $file = Yii::getAlias($file);
53 22
        if (is_file($file)) {
54 17
            return require $file;
55
        }
56
        
57 5
        if ($throwException) {
58 1
            throw new InvalidConfigException("Fixture data file does not exist: {$file}");
59
        }
60
61 4
        return [];
62
    }
63
64
}
65