Completed
Pull Request — master (#1926)
by mark
01:29
created

AbstractSeed::table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Seed;
9
10
use Phinx\Db\Adapter\AdapterInterface;
11
use Phinx\Db\Table;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Abstract Seed Class.
17
 *
18
 * It is expected that the seeds you write extend from this class.
19
 *
20
 * This abstract class proxies the various database methods to your specified
21
 * adapter.
22
 *
23
 * @author Rob Morgan <[email protected]>
24
 */
25
abstract class AbstractSeed implements SeedInterface
26
{
27
    /**
28
     * @var \Phinx\Db\Adapter\AdapterInterface
29
     */
30
    protected $adapter;
31
32
    /**
33
     * @var \Symfony\Component\Console\Input\InputInterface
34
     */
35
    protected $input;
36
37
    /**
38
     * @var \Symfony\Component\Console\Output\OutputInterface
39
     */
40
    protected $output;
41
42
    /**
43
     * Override to specify dependencies for dependency injection from the configured PSR-11 container
44
     */
45
    public function __construct()
46
    {
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function run()
53
    {
54
    }
55
56
    /**
57
     * Return seeds dependencies.
58
     *
59
     * @return array
60
     */
61
    public function getDependencies()
62
    {
63
        return [];
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 11
    public function setAdapter(AdapterInterface $adapter)
70
    {
71 11
        $this->adapter = $adapter;
72 11
73 11
        return $this;
74 11
    }
75 11
76 11
    /**
77
     * @inheritDoc
78 11
     */
79 11
    public function getAdapter()
80
    {
81
        return $this->adapter;
82
    }
83
84
    /**
85
     * @inheritDoc
86 11
     */
87
    public function setInput(InputInterface $input)
88 11
    {
89
        $this->input = $input;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function getInput()
98
    {
99
        return $this->input;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    public function setOutput(OutputInterface $output)
106
    {
107
        $this->output = $output;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115
    public function getOutput()
116
    {
117 11
        return $this->output;
118
    }
119 11
120 11
    /**
121
     * @inheritDoc
122
     */
123
    public function getName()
124
    {
125
        return static::class;
126 1
    }
127
128 1
    /**
129
     * @inheritDoc
130
     */
131
    public function execute($sql)
132
    {
133
        return $this->getAdapter()->execute($sql);
134 11
    }
135
136 11
    /**
137 11
     * @inheritDoc
138
     */
139
    public function query($sql)
140
    {
141
        return $this->getAdapter()->query($sql);
142
    }
143 1
144
    /**
145 1
     * @inheritDoc
146
     */
147
    public function fetchRow($sql)
148
    {
149
        return $this->getAdapter()->fetchRow($sql);
150
    }
151 6
152
    /**
153 6
     * @inheritDoc
154
     */
155
    public function fetchAll($sql)
156
    {
157
        return $this->getAdapter()->fetchAll($sql);
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function insert($table, $data)
164
    {
165
        $tableInstance = new Table($table, [], $this->getAdapter());
166
        $tableInstance->insert($data)->save();
167
    }
168
169
    /**
170
     * @param string $tableName Table name.
171
     *
172
     * @return bool
173
     */
174
    public function hasData(string $tableName): bool
175
    {
176
        $table = $this->getAdapter()->quoteTableName($tableName);
177
        $countQuery = $this->getAdapter()->query('SELECT COUNT(*) as count FROM ' . $table);
178
        $res = $countQuery->fetchAll();
179
180
        return $res[0]['count'] > 0;
181
    }
182
183
    /**
184
     * @inheritDoc
185
     */
186
    public function hasTable($tableName)
187
    {
188
        return $this->getAdapter()->hasTable($tableName);
189
    }
190
191
    /**
192
     * @inheritDoc
193
     */
194
    public function table($tableName, $options = [])
195
    {
196
        return new Table($tableName, $options, $this->getAdapter());
197
    }
198
}
199