Completed
Push — master ( 091aac...c0ee1b )
by Gabriel
01:46
created

TableDumperCollection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 16
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 103
ccs 42
cts 42
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 10 2
A offsetSet() 0 10 2
A addTable() 0 19 4
B addListTables() 0 29 6
A __call() 0 9 2
1
<?php
2
namespace Y0lk\SQLDumper;
3
4
use ArrayObject;
5
6
/**
7
 * A TableDumperCollection is used to group TableDumper objects together, allowing you to specify dump options on multiple table at once. 
8
 * All TableDumper methods can be called directly on a TableDumperCollection, and will be executed on all the TableDumper instances in that collection.
9
 *
10
 * @author Gabriel Jean <[email protected]>
11
 */
12
class TableDumperCollection extends ArrayObject 
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17 15
    public function append($value)
18
    {
19
        //Make sure we're adding a TableDumper object
20 15
        if (!($value instanceof TableDumper)) {
21 3
            throw new \Exception("TableDumperCollection only accepts TableDumper objects", 1);
22
        }
23
24
        //Append with table_name as key
25 12
        return $this->offsetSet($value->getTable()->getName(), $value);
26
    }
27
28
    /**
29
     * {@inheritDoc}
30
     */
31 30
    public function offsetSet($index, $newval)
32
    {
33
        //Make sure we're adding a TableDumper object
34 30
        if (!($newval instanceof TableDumper)) {
35 3
            throw new \Exception("TableDumperCollection only accepts TableDumper objects", 1);
36
        }
37
38
        //Append with table_name as key
39 27
        return parent::offsetSet($newval->getTable()->getName(), $newval);
40
    }
41
42
43
    /**
44
     * @param Table|string  Adds a table, either by name, or by Table instance, to the collection
45
     *
46
     * @return TableDumper Retruns a TableDumper of the table that was just added
47
     */
48 15
    public function addTable($table)
49
    {  
50 15
        if ($table instanceof Table) {
51 6
            $tableName = $table->getName();
52 11
        } elseif (is_string($table)) {
53 6
            $tableName = $table;
54 2
        } else {
55 3
            throw new \Exception("Invalid value supplied for argument 'table'", 1);
56
        }
57
58
        //First check if a dumper already exists for this table
59 12
        if (!$this->offsetExists($tableName)) {
60
            //Create new one
61 12
            $table = new Table($tableName);
62 12
            $this->offsetSet($tableName, new TableDumper($table));
63 4
        }
64
65 12
        return $this->offsetGet($tableName);
66
    }
67
68
69
    /**
70
     * @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 names
71
     *
72
     * @return TableDumperCollection Returns a TableDumperCollection of the list of tables that was just added
73
     */
74 18
    public function addListTables($listTables)
75
    {
76
        //If arg is a TableDumperCollection, merge into this one
77 18
        if ($listTables instanceof TableDumperCollection) {
78 3
            foreach ($listTables as $table) {
79 3
                $this->append($table);
80 1
            }
81
82 3
            return $listTables;
83
        } 
84 15
        elseif (is_array($listTables)) {
85
            //Create TableDumperCollection 
86 12
            $listDumpers = new TableDumperCollection;
87
88 12
            foreach ($listTables as $table) {
89
                //If table is already a Dumper, simply append to this
90 12
                if ($table instanceof TableDumper) {
91 6
                    $listDumpers[] = $table;
92 6
                    $this->append($table);
93 2
                } else {
94 10
                    $listDumpers[] = $this->addTable($table);
95
                }
96 4
            }
97
98 12
            return $listDumpers;
99
        }
100
101 3
        throw new \Exception("Invalid value supplied for argument 'listTables'", 1);
102
    }
103
104
105 3
    public function __call($name, $arguments)
106
    {
107
        //Call methods on TableDumper values
108 3
        foreach ($this as $value) {
109 3
            call_user_func_array([$value, $name], $arguments);
110 1
        }
111
112 3
        return $this;
113
    }
114
}