Completed
Push — master ( cba42a...a89212 )
by Gabriel
01:47
created

TableDumperCollection::dumpInsertStatements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
2
namespace Y0lk\SQLDumper;
3
4
use ArrayObject;
5
use PDO;
6
use InvalidArgumentException;
7
8
/**
9
 * A TableDumperCollection is used to group TableDumper objects together, allowing you to specify dump options on multiple table at once. 
10
 * All TableDumper methods can be called directly on a TableDumperCollection, and will be executed on all the TableDumper instances in that collection.
11
 *
12
 * @author Gabriel Jean <[email protected]>
13
 */
14
class TableDumperCollection extends ArrayObject 
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19 15
    public function append($value)
20
    {
21
        //Make sure we're adding a TableDumper object
22 15
        if (!($value instanceof TableDumper)) {
23 3
            throw new InvalidArgumentException("TableDumperCollection only accepts TableDumper objects", 1);
24
        }
25
26
        //Append with table_name as key
27 12
        return $this->offsetSet($value->getTable()->getName(), $value);
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 72
    public function offsetSet($index, $newval)
34
    {
35
        //Make sure we're adding a TableDumper object
36 72
        if (!($newval instanceof TableDumper)) {
37 3
            throw new InvalidArgumentException("TableDumperCollection only accepts TableDumper objects", 1);
38
        }
39
40
        //Append with table_name as key
41 69
        return parent::offsetSet($newval->getTable()->getName(), $newval);
42
    }
43
44
45
    /**
46
     * @param Table|string  Adds a table, either by name, or by Table instance, to the collection
47
     *
48
     * @return TableDumper Retruns a TableDumper of the table that was just added
49
     */
50 57
    public function addTable($table): TableDumper
51
    {  
52 57
        if ($table instanceof Table) {
53 6
            $tableName = $table->getName();
54 51
        } elseif (is_string($table)) {
55 48
            $tableName = $table;
56 48
            $table = new Table($tableName);
57
        } else {
58 3
            throw new InvalidArgumentException("Invalid value supplied for argument 'table'", 1);
59
        }
60
61
        //First check if a dumper already exists for this table
62 54
        if (!$this->offsetExists($tableName)) {
63
            //Create new one 
64 54
            $this->offsetSet($tableName, new TableDumper($table));
65
        }
66
67 54
        return $this->offsetGet($tableName);
68
    }
69
70
71
    /**
72
     * @param TableDumperCollection|array<TableDumper|Table|string>    Adds a list of tables, either by passing TableDumperCollection, or an array containing either TableDumper objects, Table objects or table naes
73
     *
74
     * @return TableDumperCollection Returns a TableDumperCollection of the list of tables that was just added
75
     */
76 33
    public function addListTables($listTables): TableDumperCollection
77
    {
78
        //If arg is a TableDumperCollection, merge into this one
79 33
        if ($listTables instanceof TableDumperCollection) {
80 3
            foreach ($listTables as $table) {
81 3
                $this->append($table);
82
            }
83
84 3
            return $listTables;
85 30
        } elseif (is_array($listTables)) {
86 27
            return $this->addListTableArray($listTables);
87
        }
88
89 3
        throw new \InvalidArgumentException("Invalid value supplied for argument 'listTables'", 1);
90
    }
91
92
    /**
93
     * Adds a list of tables passed as an array
94
     * @param array $listTables Array of tables to add
95
     *
96
     * @return TableDumperCollection Returns a TableDumperCollection of the list of tables that was just added
97
     */
98 27
    protected function addListTableArray(array $listTables): TableDumperCollection
99
    {
100
        //Create TableDumperCollection 
101 27
        $listDumpers = new TableDumperCollection;
102
103 27
        foreach ($listTables as $table) {
104
            //If table is already a Dumper, simply append to this
105 27
            if ($table instanceof TableDumper) {
106 6
                $listDumpers[] = $table;
107 6
                $this->append($table);
108
            } else {
109 21
                $listDumpers[] = $this->addTable($table);
110
            }
111
        }
112
113 27
        return $listDumpers;
114
    }
115
116
    /**
117
     * Writes all DROP statements to the dump stream
118
     * 
119
     * @param  resource $stream Stream to write the dump to
120
     * 
121
     * @return void
122
     */
123 9
    public function dumpDropStatements($stream): void
124
    {
125 9
        foreach ($this as $dumper) {
126 9
            if ($dumper->hasDrop()) {
127 9
                $dumper->dumpDropStatement($stream);   
128
            }
129
        }
130 9
    }
131
132
    /**
133
     * Writes all INSERT statements to the dump stream
134
     * 
135
     * @param  PDO      $db     PDO instance to use for DB queries
136
     * @param  resource $stream Stream to write the dump to
137
     * 
138
     * @return void
139
     */
140 9
    public function dumpInsertStatements(PDO $db, $stream): void
141
    {
142 9
        foreach ($this as $dumper) {
143 9
            if ($dumper->hasData()) {
144 9
                $dumper->dumpInsertStatement($db, $stream);
145
            }
146
        }
147 9
    }
148
149
    /**
150
     * Writes all the SQL statements of this dumper to the dump stream
151
     * 
152
     * @param  PDO      $db             PDO instance to use for DB queries
153
     * @param  resource $stream         Stream to write the dump to
154
     * @param  boolean  $groupDrops     Determines if DROP statements will be grouped
155
     * @param  boolean  $groupInserts   Determines if INSERT statements will be grouped
156
     * 
157
     * @return void
158
     */
159 30
    public function dump(PDO $db, $stream, bool $groupDrops = false, bool $groupInserts = false): void
160
    {   
161 30
        if ($groupDrops) {
162 6
            $this->dumpDropStatements($stream);
163
        }
164
165 30
        foreach ($this as $dumper) {
166 30
            if (!$groupDrops) {
167 24
                $dumper->dumpDropStatement($stream);
168
            }
169
170 30
            $dumper->dumpCreateStatement($db, $stream);
171
172 30
            if (!$groupInserts) {
173 24
                $dumper->dumpInsertStatement($db, $stream);
174
            }
175
        }
176
177 30
        if ($groupInserts) {
178 6
            $this->dumpInsertStatements($db, $stream);
179
        }
180 30
    }
181
182
183 6
    public function __call(string $name, array $arguments)
184
    {
185
        //Call methods on TableDumper values
186 6
        foreach ($this as $value) {
187 6
            call_user_func_array([$value, $name], $arguments);
188
        }
189
190 6
        return $this;
191
    }
192
}