Completed
Push — master ( dc3ccc...e475aa )
by Gabriel
01:58
created

TableDumperCollection::append()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
     * {@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 66
    public function offsetSet($index, $newval)
34
    {
35
        //Make sure we're adding a TableDumper object
36 66
        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 63
        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 51
    public function addTable($table): TableDumper
51
    {  
52 51
        if ($table instanceof Table) {
53 6
            $tableName = $table->getName();
54 45
        } elseif (is_string($table)) {
55 42
            $tableName = $table;
56 42
            $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 48
        if (!$this->offsetExists($tableName)) {
63
            //Create new one 
64 48
            $this->offsetSet($tableName, new TableDumper($table));
65
        }
66
67 48
        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 27
    public function addListTables($listTables): TableDumperCollection
77
    {
78
        //If arg is a TableDumperCollection, merge into this one
79 27
        if ($listTables instanceof TableDumperCollection) {
80 3
            foreach ($listTables as $table) {
81 3
                $this->append($table);
82
            }
83
84 3
            return $listTables;
85 24
        } elseif (is_array($listTables)) {
86
            //Create TableDumperCollection 
87 21
            $listDumpers = new TableDumperCollection;
88
89 21
            foreach ($listTables as $table) {
90
                //If table is already a Dumper, simply append to this
91 21
                if ($table instanceof TableDumper) {
92 6
                    $listDumpers[] = $table;
93 6
                    $this->append($table);
94
                } else {
95 15
                    $listDumpers[] = $this->addTable($table);
96
                }
97
            }
98
99 21
            return $listDumpers;
100
        }
101
102 3
        throw new \InvalidArgumentException("Invalid value supplied for argument 'listTables'", 1);
103
    }
104
105
    /**
106
     * Writes all DROP statements to the dump stream
107
     * 
108
     * @param  resource $stream Stream to write the dump to
109
     * 
110
     * @return void
111
     */
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 21
    public function dump(PDO $db, $stream, bool $groupDrops = false, bool $groupInserts = false): void
149
    {   
150 21
        if($groupDrops) {
151
            $this->dumpDropStatements($stream);
152
        }
153
154 21
        foreach ($this as $dumper) {
155 21
            if($dumper->hasDrop() && !$groupDrops) {
156 9
                $dumper->dumpDropStatement($stream);
157
            }
158
159 21
            if($dumper->hasStructure()) {
160 9
                $dumper->dumpCreateStatement($db, $stream);
161
            }
162
163 21
            if($dumper->hasData() && !$groupInserts) {
164 12
                $dumper->dumpInsertStatement($db, $stream);
165
            }
166
        }
167
168 21
        if($groupInserts) {
169
            $this->dumpInsertStatements($db, $stream);
170
        }
171 21
    }
172
173
174 6
    public function __call(string $name, array $arguments)
175
    {
176
        //Call methods on TableDumper values
177 6
        foreach ($this as $value) {
178 6
            call_user_func_array([$value, $name], $arguments);
179
        }
180
181 6
        return $this;
182
    }
183
}