Completed
Push — master ( a17c10...09cc32 )
by Maxime
04:10
created

SyncTrait::switchToSyncDb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Distilleries\Contentful\Commands\Sync\Traits;
4
5
trait SyncTrait
6
{
7
    /**
8
     * Switch to `sync` database.
9
     *
10
     * @return void
11
     */
12
    protected function switchToSyncDb()
13
    {
14
        $this->warn('Switch to sync database');
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...
15
16
        config([
17
            'database.default' => 'mysql_sync',
18
        ]);
19
    }
20
21
    /**
22
     * Dump `sync` database into a SQL file and return its path.
23
     *
24
     * @param  boolean $isPreview
25
     * @return string
26
     */
27 View Code Duplication
    protected function dumpSync(bool $isPreview, string $connector = 'mysql'): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $path = storage_path('dumps/' . date('YmdHis') . '_sync' . ($isPreview ? '_preview' : '') . '.sql');
30
        $this->warn('Dump "' . basename($path) . '"...');
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...
31
32
        $dirName = dirname($path);
33
        if (!is_dir($dirName)) {
34
            mkdir($dirName, 0777, true);
35
        }
36
37
        $this->dumpSql($path, $this->getConnector($isPreview, $connector));
38
39
        return realpath($path);
40
    }
41
42
    /**
43
     * Dump SQL database in given file path.
44
     *
45
     * @param  string $path
46
     * @param  string $connector
47
     * @return void
48
     */
49 View Code Duplication
    protected function dumpSql(string $path, string $connector)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $exec = 'export MYSQL_PWD=\'%s\'; mysqldump --add-drop-table --default-character-set=%s %s -u %s -h %s --port %s > %s';
52
53
        $command = sprintf($exec,
54
            addcslashes(config('database.connections.' . $connector . '.password'), "'"),
55
            config('database.connections.' . $connector . '.charset'),
56
            config('database.connections.' . $connector . '.database'),
57
            config('database.connections.' . $connector . '.username'),
58
            config('database.connections.' . $connector . '.host'),
59
            config('database.connections.' . $connector . '.port'),
60
            $path
61
        );
62
        exec($command);
63
    }
64
65
    protected function getConnector(bool $isPreview, string $connector = 'mysql'): string
66
    {
67
        $compiledConnector = $connector . ($isPreview ? '_preview' : '');
68
        if (empty(config('database.connections.' . $compiledConnector . '.username'))) {
69
            $compiledConnector = $connector;
70
        }
71
72
        return $compiledConnector;
73
74
    }
75
76
77
    /**
78
     * Put previous dump in live-preview database.
79
     *
80
     * @param  string $path
81
     * @param  boolean $isPreview
82
     * @return void
83
     */
84
    protected function putSync(string $path, bool $isPreview, string $connector = 'mysql')
85
    {
86
        $compiledConnector = $this->getConnector($isPreview, $connector);
87
88
        config([
89
            'database.default' => $compiledConnector,
90
        ]);
91
92
        $this->warn('Put into "' . $compiledConnector . '" database...');
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...
93
        $this->putSql($path, $compiledConnector);
94
    }
95
96
    /**
97
     * Put SQL file into given database.
98
     *
99
     * @param  string $path
100
     * @param  string $connector
101
     * @return void
102
     */
103 View Code Duplication
    protected function putSql(string $path, string $connector)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $exec = 'export MYSQL_PWD=\'%s\'; mysql -u %s -h %s --port %s %s < %s';
106
107
        $command = sprintf($exec,
108
            addcslashes(config('database.connections.' . $connector . '.password'), "'"),
109
            config('database.connections.' . $connector . '.username'),
110
            config('database.connections.' . $connector . '.host'),
111
            config('database.connections.' . $connector . '.port'),
112
            config('database.connections.' . $connector . '.database'),
113
            $path
114
        );
115
116
        exec($command);
117
    }
118
}
119