Completed
Push — master ( f3489f...dc3ccc )
by Gabriel
01:55
created

TableDumperCollection::dump()   B

Complexity

Conditions 9
Paths 36

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 0
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 36
nop 4
crap 90
1
<?php
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 10
     * {@inheritDoc}
18
     */
19
    public function append($value)
20 10
    {
21 2
        //Make sure we're adding a TableDumper object
22
        if (!($value instanceof TableDumper)) {
23
            throw new InvalidArgumentException("TableDumperCollection only accepts TableDumper objects", 1);
24
        }
25 8
26
        //Append with table_name as key
27
        return $this->offsetSet($value->getTable()->getName(), $value);
28
    }
29
30
    /**
31 44
     * {@inheritDoc}
32
     */
33
    public function offsetSet($index, $newval)
34 44
    {
35 2
        //Make sure we're adding a TableDumper object
36
        if (!($newval instanceof TableDumper)) {
37
            throw new InvalidArgumentException("TableDumperCollection only accepts TableDumper objects", 1);
38
        }
39 42
40
        //Append with table_name as key
41
        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 34
     * @return TableDumper Retruns a TableDumper of the table that was just added
49
     */
50 34
    public function addTable($table): TableDumper
51 4
    {  
52 34
        if ($table instanceof Table) {
53 28
            $tableName = $table->getName();
54 28
        } elseif (is_string($table)) {
55 28
            $tableName = $table;
56 2
            $table = new Table($tableName);
57
        } else {
58
            throw new InvalidArgumentException("Invalid value supplied for argument 'table'", 1);
59
        }
60 32
61
        //First check if a dumper already exists for this table
62 32
        if (!$this->offsetExists($tableName)) {
63 32
            //Create new one 
64
            $this->offsetSet($tableName, new TableDumper($table));
65 32
        }
66
67
        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 18
     * @return TableDumperCollection Returns a TableDumperCollection of the list of tables that was just added
75
     */
76
    public function addListTables($listTables): TableDumperCollection
77 18
    {
78 2
        //If arg is a TableDumperCollection, merge into this one
79 2
        if ($listTables instanceof TableDumperCollection) {
80 2
            foreach ($listTables as $table) {
81
                $this->append($table);
82 2
            }
83 16
84
            return $listTables;
85 14
        } elseif (is_array($listTables)) {
86
            //Create TableDumperCollection 
87 14
            $listDumpers = new TableDumperCollection;
88
89 14
            foreach ($listTables as $table) {
90 4
                //If table is already a Dumper, simply append to this
91 4
                if ($table instanceof TableDumper) {
92 4
                    $listDumpers[] = $table;
93 10
                    $this->append($table);
94
                } else {
95 14
                    $listDumpers[] = $this->addTable($table);
96
                }
97 14
            }
98
99
            return $listDumpers;
100 2
        }
101
102
        throw new \InvalidArgumentException("Invalid value supplied for argument 'listTables'", 1);
103
    }
104 4
105
    /**
106
     * Writes all DROP statements to the dump stream
107 4
     * 
108 4
     * @param  resource $stream Stream to write the dump to
109 4
     * 
110
     * @return void
111 4
     */
112
    public function dumpDropStatements($stream): void
113
    {
114
        foreach ($this as $dumper) {
115
            if($dumper->hasDrop()) {
116
                $dumper->dumpDropStatement($stream);   
117
            }
118
        }
119
    }
120
121
    /**
122
     * Writes all INSERT statements to the dump stream
123
     * 
124
     * @param  PDO      $db     PDO instance to use for DB queries
125
     * @param  resource $stream Stream to write the dump to
126
     * 
127
     * @return void
128
     */
129
    public function dumpInsertStatements(PDO $db, $stream): void
130
    {
131
        foreach ($this as $dumper) {
132
            if($dumper->hasData()) {
133
                $dumper->dumpInsertStatement($db, $stream);
134
            }
135
        }
136
    }
137
138
    /**
139
     * Writes all the SQL statements of this dumper to the dump stream
140
     * 
141
     * @param  PDO      $db             PDO instance to use for DB queries
142
     * @param  resource $stream         Stream to write the dump to
143
     * @param  boolean  $groupDrops     Determines if DROP statements will be grouped
144
     * @param  boolean  $groupInserts   Determines if INSERT statements will be grouped
145
     * 
146
     * @return void
147
     */
148
    public function dump(PDO $db, $stream, bool $groupDrops = false, bool $groupInserts = false): void
149
    {   
150
        if($groupDrops) {
151
            $this->dumpDropStatements($stream);
152
        }
153
154
        foreach ($this as $dumper) {
155
            if($dumper->hasDrop() && !$groupDrops) {
156
                $dumper->dumpDropStatement($stream);
157
            }
158
159
            if($dumper->hasStructure()) {
160
                $dumper->dumpCreateStatement($db, $stream);
161
            }
162
163
            if($dumper->hasData() && !$groupInserts) {
164
                $dumper->dumpInsertStatement($db, $stream);
165
            }
166
        }
167
168
        if($groupInserts) {
169
            $this->dumpInsertStatements($db, $stream);
170
        }
171
    }
172
173
174
    public function __call(string $name, array $arguments)
175
    {
176
        //Call methods on TableDumper values
177
        foreach ($this as $value) {
178
            call_user_func_array([$value, $name], $arguments);
179
        }
180
181
        return $this;
182
    }
183
}