DbProcessor   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
eloc 33
c 1
b 0
f 1
dl 0
loc 86
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getTables() 0 3 1
A __construct() 0 8 1
A backup() 0 2 1
A restore() 0 2 1
A checkBinary() 0 4 3
A setPort() 0 3 1
B parseDsn() 0 19 8
1
<?php
2
3
4
namespace floor12\backup\logic\processors;
5
6
7
use floor12\backup\Exceptions\BinaryNotFoundException;
8
use floor12\backup\Exceptions\DsnParseException;
9
use floor12\backup\models\IOPriority;
10
use floor12\backup\Module;
11
use Yii;
12
use yii\db\Connection;
13
14
abstract class  DbProcessor implements DbProcessorInterface
15
{
16
    /**@var Connection */
17
    protected $connection;
18
    /**@var array */
19
    protected $parsedDsn;
20
    /**@var string */
21
    protected $backupFilePath;
22
    /**@var Module */
23
    protected $module;
24
    /**@var string */
25
    protected $username;
26
    /**@var string */
27
    protected $database;
28
    /**@var string */
29
    protected $host;
30
    /** @var int */
31
    protected $port;
32
    /**@var string */
33
    protected $io;
34
    /**@var string */
35
    protected $password;
36
37
38
    /**
39
     * DbProcessor constructor.
40
     * @param string $backupFilePath
41
     * @param Connection $connection
42
     * @throws DsnParseException
43
     */
44
    public function __construct(string $backupFilePath, Connection $connection, $io = IOPriority::IDLE)
45
    {
46
        $this->parseDsn($connection->dsn);
47
        $this->backupFilePath = $backupFilePath;
48
        $this->username = $connection->username;
49
        $this->password = $connection->password;
50
        $this->module = Yii::$app->getModule('backup');
51
        $this->io = $io;
52
    }
53
54
    protected function parseDsn(string $dsn): void
55
    {
56
        $dsn = substr($dsn, strpos($dsn, ':') + 1);
57
        $dsnParts = explode(';', $dsn);
58
59
        if (empty($dsnParts))
60
            throw new DsnParseException();
61
62
        foreach ($dsnParts as $part) {
63
            $row = explode('=', $part);
64
            if (isset($row[0]) && isset($row[1]))
65
                $this->parsedDsn[$row[0]] = $row[1];
66
        }
67
68
        $this->host = $this->parsedDsn['host'] ?: null;
69
        if (!empty($this->parsedDsn['port'])) {
70
            $this->port = $this->parsedDsn['port'];
71
        }
72
        $this->database = $this->parsedDsn['dbname'] ?: null;
73
    }
74
75
    public function setPort(int $port): void
76
    {
77
        $this->port = $port;
78
    }
79
80
    protected function checkBinary(string $binary): void
81
    {
82
        if (!file_exists($binary) || !is_executable($binary)) {
83
            throw new BinaryNotFoundException($binary);
84
        }
85
    }
86
87
    public function backup(): void
88
    {
89
90
    }
91
92
    public function restore(array $tableNames = []): void
93
    {
94
95
    }
96
97
    public function getTables(): array
98
    {
99
        return [];
100
    }
101
102
}
103