DbSeeding   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 3 Features 1
Metric Value
wmc 16
c 10
b 3
f 1
lcom 1
cbo 1
dl 0
loc 135
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hasTableData() 0 4 1
A write() 0 14 2
C convert() 0 47 8
A compile() 0 11 1
A insertPropertyAndValue() 0 12 3
1
<?php namespace Nwidart\DbExporter;
2
3
use Config, DB, Str, File;
4
5
class DbSeeding extends DbExporter
6
{
7
    /**
8
     * @var String
9
     */
10
    protected $database;
11
12
    /**
13
     * @var String
14
     */
15
    protected $seedingStub;
16
17
    /**
18
     * @var bool
19
     */
20
    protected $customDb = false;
21
22
    /**
23
     * Set the database name
24
     * @param String $database
25
     */
26
    function __construct($database)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        $this->database = $database;
29
    }
30
31
    /**
32
     * Write the seed file
33
     */
34
    public function write()
35
    {
36
        // Check if convert method was called before
37
        // If not, call it on default DB
38
        if (!$this->customDb) {
39
            $this->convert();
40
        }
41
42
        $seed = $this->compile();
43
44
        $filename = Str::camel($this->database) . "TableSeeder";
45
46
        file_put_contents(config('db-exporter.export_path.seeds')."{$filename}.php", $seed);
47
    }
48
49
    /**
50
     * Convert the database tables to something usefull
51
     * @param null $database
52
     * @return $this
53
     */
54
    public function convert($database = null)
55
    {
56
        if (!is_null($database)) {
57
            $this->database = $database;
58
            $this->customDb = true;
59
        }
60
61
        // Get the tables for the database
62
        $tables = $this->getTables();
63
64
        $stub = "";
65
        // Loop over the tables
66
        foreach ($tables as $key => $value) {
67
            // Do not export the ignored tables
68
            if (in_array($value['table_name'], self::$ignore)) {
69
                continue;
70
            }
71
            $tableName = $value['table_name'];
72
            $tableData = $this->getTableData($value['table_name']);
73
            $insertStub = "";
74
75
            foreach ($tableData as $obj) {
76
                $insertStub .= "
77
            array(\n";
78
                foreach ($obj as $prop => $value) {
79
                    $insertStub .= $this->insertPropertyAndValue($prop, $value);
80
                }
81
82
                if (count($tableData) > 1) {
83
                    $insertStub .= "            ),\n";
84
                } else {
85
                    $insertStub .= "            )\n";
86
                }
87
            }
88
89
            if ($this->hasTableData($tableData)) {
90
                $stub .= "
91
        DB::table('" . $tableName . "')->insert(array(
92
            {$insertStub}
93
        ));";
94
            }
95
        }
96
97
        $this->seedingStub = $stub;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Compile the current seedingStub with the seed template
104
     * @return mixed
105
     */
106
    protected function compile()
107
    {
108
        // Grab the template
109
        $template = File::get(__DIR__ . '/templates/seed.txt');
110
111
        // Replace the classname
112
        $template = str_replace('{{className}}', \Str::camel($this->database) . "TableSeeder", $template);
113
        $template = str_replace('{{run}}', $this->seedingStub, $template);
114
115
        return $template;
116
    }
117
118
    private function insertPropertyAndValue($prop, $value)
119
    {
120
        $prop = addslashes($prop);
121
        $value = addslashes($value);
122
        if (is_numeric($value)) {
123
            return "                '{$prop}' => {$value},\n";
124
        } elseif($value == '') {
125
            return "                '{$prop}' => NULL,\n";
126
        } else {
127
            return "                '{$prop}' => '{$value}',\n";
128
        }
129
    }
130
131
    /**
132
     * @param $tableData
133
     * @return bool
134
     */
135
    public function hasTableData($tableData)
136
    {
137
        return count($tableData) >= 1;
138
    }
139
}
140