SyncTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 41
c 5
b 0
f 1
dl 0
loc 117
ccs 0
cts 63
cp 0
rs 10
wmc 10

6 Methods

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