Completed
Branch master (160732)
by Filipe
07:07 queued 04:49
created

Dummify::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dummify;
4
5
use Illuminate\Database\Capsule\Manager as DB;
6
7
/**
8
 * Dummify class
9
 */
10
class Dummify
11
{
12
    protected static $instance;
13
14
    protected $capsule;
15
16
    protected $table;
17
18
    protected $filter;
19
20
    /**
21
     * Creates a single for Dummify
22
     */
23
    public static function initialize()
24
    {
25
        $instance = new static();
26
        static::$instance = $instance;
27
    }
28
29
    /**
30
     * Constructor
31
     */
32
    public function __construct()
33
    {
34
        $this->capsule = new DB();
35
        $this->table = null;
36
        $this->filter = null;
37
    }
38
39
    /**
40
     * Returns Dummify's instance
41
     */
42
    public static function getInstance()
43
    {
44
        return static::$instance;
45
    }
46
47
    /**
48
     * Initializes and defines connection
49
     */
50
    public static function connectTo($params)
51
    {
52
        $instance = static::getInstance();
53
54
        if (is_null($instance)) {
55
            static::initialize();
56
            $instance = static::getInstance();
57
        }
58
59
        $instance->addConnection($params);
60
61
        return $instance;
62
    }
63
64
    /**
65
     * Checks if there is a specific connection
66
     */
67
    public function hasConnection($name = 'default')
68
    {
69
        return isset($this->capsule->getDatabaseManager()->getConnections()[$name]);
70
    }
71
72
    /**
73
     * Adds a connection
74
     */
75
    public function addConnection($params)
76
    {
77
        $this->capsule->addConnection($params, 'default');
78
79
        return $this;
80
    }
81
82
    /**
83
     * Gets a connection
84
     */
85
    public function getConnection()
86
    {
87
        return $this->capsule->connection('default');
88
    }
89
90
    /**
91
     * Selects a table and a passes a optional condition
92
     */
93
    public function from($table, callable $callable = null)
94
    {
95
        $this->table = $table;
96
        $this->filter = $callable;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Creates a factory using the connection
103
     */
104
    protected function getQuery()
105
    {
106
        $query = $this->capsule->table($this->table);
107
108
        $filter = $this->filter;
109
        if (!is_null($filter)) {
110
            $query = $filter($query);
111
        }
112
113
        return $query;
114
    }
115
116
    /**
117
     * Populates a table with a row generator.
118
     */
119
    public function insert(callable $callable, $number = 1)
120
    {
121
        while ($number-- > 0) {
122
            $data = $callable((object) []);
123
            $this->getQuery()->insert((array) $data);
124
        }
125
    }
126
127
    /**
128
     * Iterates over update record
129
     */
130
    public function update(callable $callable)
131
    {
132
        $data = $this->getQuery()->get();
133
134
        $data->each(function ($row) use ($callable) {
135
            $this->getQuery()
136
                ->where((array) $row)
137
                ->update((array) $callable($row));
138
        });
139
    }
140
}
141