ActiveFixture::resetTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CorpSoft\Fixture;
4
5
use CorpSoft\Fixture\Exceptions\InvalidConfigException;
6
use Exception;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Schema;
9
10
/**
11
 * ActiveFixture represents a fixture backed up by a [[modelClass|Illuminate\Database\Eloquent\Model class]].
12
 *
13
 * [[modelClass]] must be set. You should also provide fixture data in the file
14
 * specified by [[dataFile]] or overriding [[getData()]] if you want to use code to generate the fixture data.
15
 *
16
 * When the fixture is being loaded, it will first call [[resetTable()]] to remove any existing data in the table.
17
 * It will then populate the table with the data returned by [[getData()]].
18
 *
19
 * After the fixture is loaded, you can access the loaded data via the [[data]] property.
20
 */
21
class ActiveFixture extends BaseActiveFixture
22
{
23
    /**
24
     * @var string the file path that contains the fixture data to be returned by [[getData()]]
25
     */
26
    public $dataFile;
27
28
    /**
29
     * @var string table name
30
     */
31
    protected $table;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function load(): void
37
    {
38
        $this->data = [];
39
        $table = $this->getTable();
40
41
        foreach ($this->getData() as $alias => $row) {
42
            if (Schema::hasColumns($this->table, ['id'])) {
43
                $primaryKey = DB::table($table)->insertGetId($row);
44
                $this->data[$alias] = array_merge($row, ['id' => $primaryKey]);
45
            } else {
46
                if (DB::table($table)->insert($row)) {
47
                    $this->data[$alias] = $row;
48
                } else {
49
                    throw new Exception("Fixture does not loaded for table: {$this->table}");
50
                }
51
            }
52
        }
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function unload(): void
59
    {
60
        $this->resetTable();
61
62
        parent::unload();
63
    }
64
65
    /**
66
     * Removes all existing data from the specified table.
67
     *
68
     * @throws Exception
69
     */
70
    protected function resetTable(): void
71
    {
72
        DB::table($this->getTable())->delete();
73
    }
74
75
    /**
76
     * Returns the table name for [[modelClass]]
77
     *
78
     * @throws Exception
79
     *
80
     * @return string
81
     */
82
    protected function getTable(): string
83
    {
84
        if ($this->modelClass === null && $this->table === null) {
85
            throw new InvalidConfigException('Either "modelClass" or "table" must be set.');
86
        }
87
88
        if ($this->table === null) {
89
            $this->table = with(new $this->modelClass())->getTable();
90
        }
91
92
        if (!Schema::hasTable($this->table)) {
93
            throw new InvalidConfigException("Table does not exist: {$this->table}");
94
        }
95
96
        return $this->table;
97
    }
98
}
99