ImportSql   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 24
c 0
b 0
f 0
dl 0
loc 65
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 40 7
A isEnabled() 0 3 1
1
<?php
2
3
namespace Sunnysideup\MigrateData\Tasks;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\ORM\DB;
8
use Sunnysideup\Flush\FlushNow;
9
use Sunnysideup\Flush\FlushNowImplementor;
10
11
class ImportSql extends BuildTask
12
{
13
    /**
14
     * standard SS variable.
15
     *
16
     * @var string
17
     */
18
    protected $title = 'Import SQL';
19
20
    /**
21
     * standard SS variable.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'CAREFUL!!! - Import data from an SQL file';
26
27
    private static $file_names = [
28
        // 'my-sql-file-goes-here.sql',
29
    ];
30
31
    public function isEnabled()
32
    {
33
        return Director::isDev();
34
    }
35
36
    public function run($request)
37
    {
38
        foreach ($this->config()->get('file_names') as $fileName) {
39
            FlushNowImplementor::do_flush("<hr /><hr /><hr /><hr /><hr /><hr /><hr />START: '.{$fileName}.'<hr /><hr /><hr /><hr /><hr /><hr /><hr />");
40
41
            $fileName = Director::baseFolder() . '/' . $fileName;
42
43
            // Temporary variable, used to store current query
44
            $templine = '';
45
46
            if (!file($fileName)) {
47
                die('File not found: ' . $fileName);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
48
            }
49
            // Read in entire file
50
            $lines = file($fileName);
51
            $count = 0;
52
53
            // Loop through each line
54
            foreach ($lines as $line) {
55
                // Skip it if it's a comment
56
                if ('--' === substr((string) $line, 0, 2) || '' === $line) {
57
                    continue;
58
                }
59
60
                // Add this line to the current segment
61
                $templine .= $line;
62
                // If it has a semicolon at the end, it's the end of the query
63
                if (';' === substr(trim($line), -1, 1)) {
64
                    ++$count;
65
                    FlushNowImplementor::do_flush('running SQL ... line: ' . $count);
66
                    // Perform the query
67
                    DB::query($templine);
68
                    // Reset temp variable to empty
69
                    $templine = '';
70
                }
71
            }
72
73
            FlushNowImplementor::do_flush("<hr /><hr /><hr /><hr /><hr /><hr /><hr />END: '.{$fileName}.'<hr /><hr /><hr /><hr /><hr /><hr /><hr />");
74
        }
75
        FlushNowImplementor::do_flush('<hr /><hr /><hr /><hr /><hr /><hr /><hr />COMPLETED<hr /><hr /><hr /><hr /><hr /><hr /><hr />');
76
    }
77
}
78