GrepDb::replaceDatabase()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * @copyright   (c) 2017-present brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace ptlis\GrepDb;
10
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\DriverManager;
13
use ptlis\GrepDb\Replace\Replace;
14
use ptlis\GrepDb\Replace\Result\RowReplaceResult;
15
use ptlis\GrepDb\Search\Result\RowSearchResult;
16
use ptlis\GrepDb\Search\Search;
17
18
/**
19
 * Perform a search or a search & replace on the database.
20
 */
21
final class GrepDb
22
{
23
    /** @var Connection */
24
    private $connection;
25
26
    /** @var Search */
27
    private $search;
28
29
    /** @var Replace */
30
    private $replace;
31
32
33
    public function __construct(
34
        string $username,
35
        string $password,
36
        string $host,
37
        int $port = 3306
38
    ) {
39
        $this->connection = DriverManager::getConnection([
40
            'user' => $username,
41
            'password' => $password,
42
            'host' => $host,
43
            'port' => $port,
44
            'driver' => 'pdo_mysql'
45
        ]);
46
        $this->search = new Search();
47
        $this->replace = new Replace();
48
    }
49
50
    /**
51
     * Performs search on the specified database and table.
52
     *
53
     * @param string $databaseName
54
     * @param string $tableName
55
     * @param string $searchTerm
56
     * @return \Generator|RowSearchResult[]
57
     */
58
    public function searchTable(
59
        string $databaseName,
60
        string $tableName,
61
        string $searchTerm
62
    ): \Generator {
63
        return $this->search->searchTable($this->connection, $databaseName, $tableName, $searchTerm);
64
    }
65
66
    /**
67
     * Performs search on all tables in the specified database.
68
     *
69
     * @param string $databaseName
70
     * @param string $searchTerm
71
     * @return \Generator|RowSearchResult[]
72
     */
73
    public function searchDatabase(
74
        string $databaseName,
75
        string $searchTerm
76
    ): \Generator {
77
        return $this->search->searchDatabase($this->connection, $databaseName, $searchTerm);
78
    }
79
80
    /**
81
     * Performs search on all available databases.
82
     *
83
     * @param string $searchTerm
84
     * @return \Generator|RowSearchResult[]
85
     */
86
    public function searchServer(
87
        string $searchTerm
88
    ): \Generator {
89
        return $this->search->searchServer($this->connection, $searchTerm);
90
    }
91
92
    /**
93
     * Performs search and replacement on the specified database and table.
94
     *
95
     * @param string $databaseName
96
     * @param string $tableName
97
     * @param string $searchTerm
98
     * @param string $replaceTerm
99
     * @return \Generator|RowReplaceResult[]
100
     */
101
    public function replaceTable(
102
        string $databaseName,
103
        string $tableName,
104
        string $searchTerm,
105
        string $replaceTerm
106
    ): \Generator {
107
        return $this->replace->replaceTable(
108
            $this->connection,
109
            $databaseName,
110
            $tableName,
111
            $searchTerm,
112
            $replaceTerm
113
        );
114
    }
115
116
    /**
117
     * Performs search and replacement on all tables in the specified database.
118
     *
119
     * @param string $databaseName
120
     * @param string $searchTerm
121
     * @param string $replaceTerm
122
     * @return \Generator|RowReplaceResult[]
123
     */
124
    public function replaceDatabase(
125
        string $databaseName,
126
        string $searchTerm,
127
        string $replaceTerm
128
    ): \Generator {
129
        return $this->replace->replaceDatabase($this->connection, $databaseName, $searchTerm, $replaceTerm);
130
    }
131
}