Completed
Push — master ( a2c931...7905ad )
by Maxime
07:31 queued 04:40
created

SyncTrait::dumpSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 15
ccs 0
cts 14
cp 0
crap 2
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Distilleries\Contentful\Commands\Sync\Traits;
4
5
trait SyncTrait
6
{
7
8
    protected function displayWarn(string $string): void
9
    {
10
        if (method_exists($this, 'warn')) {
11
            $this->warn($string);
0 ignored issues
show
Bug introduced by
It seems like warn() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
12
        }
13
    }
14
15
    /**
16
     * Switch to `sync` database.
17
     *
18
     * @return void
19
     */
20
    protected function switchToSyncDb()
21
    {
22
        $this->displayWarn('Switch to sync database');
23
24
        config([
25
            'database.default' => 'mysql_sync',
26
        ]);
27
    }
28
29
    /**
30
     * Dump `sync` database into a SQL file and return its path.
31
     *
32
     * @param  boolean $isPreview
33
     * @return string
34
     */
35
    protected function dumpSync(bool $isPreview, string $connector = 'mysql'): string
36
    {
37
        $path = storage_path('dumps/' . date('YmdHis') . '_sync' . ($isPreview ? '_preview' : '') . '.sql');
38
        $this->displayWarn('Dump "' . basename($path) . '"...');
39
40
        $dirName = dirname($path);
41
        if (!is_dir($dirName)) {
42
            mkdir($dirName, 0777, true);
43
        }
44
45
        $this->dumpSql($path, $this->getConnector($isPreview, $connector));
46
47
        return realpath($path);
48
    }
49
50
    /**
51
     * Dump SQL database in given file path.
52
     *
53
     * @param  string $path
54
     * @param  string $connector
55
     * @return void
56
     */
57
    protected function dumpSql(string $path, string $connector)
58
    {
59
        $exec = 'export MYSQL_PWD=\'%s\'; mysqldump --add-drop-table --default-character-set=%s %s -u %s -h %s --port %s > %s';
60
61
        $command = sprintf($exec,
62
            addcslashes(config('database.connections.' . $connector . '.password'), "'"),
63
            config('database.connections.' . $connector . '.charset'),
64
            config('database.connections.' . $connector . '.database'),
65
            config('database.connections.' . $connector . '.username'),
66
            config('database.connections.' . $connector . '.host'),
67
            config('database.connections.' . $connector . '.port'),
68
            $path
69
        );
70
        exec($command);
71
    }
72
73
    protected function getConnector(bool $isPreview, string $connector = 'mysql'): string
74
    {
75
        $compiledConnector = $connector . ($isPreview ? '_preview' : '');
76
        if (empty(config('database.connections.' . $compiledConnector . '.username'))) {
77
            $compiledConnector = $connector;
78
        }
79
80
        return $compiledConnector;
81
82
    }
83
84
85
    /**
86
     * Put previous dump in live-preview database.
87
     *
88
     * @param  string $path
89
     * @param  boolean $isPreview
90
     * @return void
91
     */
92
    protected function putSync(string $path, bool $isPreview, string $connector = 'mysql')
93
    {
94
        $compiledConnector = $this->getConnector($isPreview, $connector);
95
96
        config([
97
            'database.default' => $compiledConnector,
98
        ]);
99
100
        $this->displayWarn('Put into "' . $compiledConnector . '" database...');
101
        $this->putSql($path, $compiledConnector);
102
    }
103
104
    /**
105
     * Put SQL file into given database.
106
     *
107
     * @param  string $path
108
     * @param  string $connector
109
     * @return void
110
     */
111
    protected function putSql(string $path, string $connector)
112
    {
113
        $exec = 'export MYSQL_PWD=\'%s\'; mysql -u %s -h %s --port %s %s < %s';
114
115
        $command = sprintf($exec,
116
            addcslashes(config('database.connections.' . $connector . '.password'), "'"),
117
            config('database.connections.' . $connector . '.username'),
118
            config('database.connections.' . $connector . '.host'),
119
            config('database.connections.' . $connector . '.port'),
120
            config('database.connections.' . $connector . '.database'),
121
            $path
122
        );
123
124
        exec($command);
125
    }
126
}
127