Issues (590)

src/Migration/Migration.php (3 issues)

1
<?php
2
3
namespace Bdf\Prime\Migration;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Exception\PrimeException;
7
use Bdf\Prime\Repository\RepositoryInterface;
8
use Bdf\Prime\Schema\SchemaManager;
9
use Bdf\Prime\ServiceLocator;
10
use Doctrine\DBAL\Result;
11
use Psr\Container\ContainerInterface;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\Console\Helper\HelperSet;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Migration
19
 */
20
class Migration implements MigrationInterface
21
{
22
    /**
23
     * The migration version
24
     *
25
     * @var string
26
     */
27
    private $version;
28
29
    /**
30
     * The application container
31
     *
32
     * @var ContainerInterface
33
     */
34
    protected $di;
35
36
    /**
37
     * The console input.
38
     *
39
     * @var InputInterface
40
     */
41
    protected $input;
42
43
    /**
44
     * The console output.
45
     *
46
     * @var OutputInterface
47
     */
48
    protected $output;
49
50
    /**
51
     * The console helper.
52
     *
53
     * @var HelperSet
54
     */
55
    protected $helperSet;
56
57
    /**
58
     * Migration constructor
59
     *
60
     * @param string $version
61
     * @param ContainerInterface $di
62
     */
63 52
    public function __construct(string $version, ContainerInterface $di)
64
    {
65 52
        $this->version = $version;
66 52
        $this->di = $di;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function initialize(): void
73
    {
74
        // To overwrite
75 1
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 1
    public function up(): void
81
    {
82
        // To overwrite
83 1
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 1
    public function down(): void
89
    {
90
        // To overwrite
91 1
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 24
    public function stage(): string
97
    {
98 24
        return self::STAGE_DEFAULT;
99
    }
100
101
    /**
102
     * Get migration version (migration ID)
103
     *
104
     * @return string
105
     */
106 23
    final public function version(): string
107
    {
108 23
        return $this->version;
109
    }
110
111
    /**
112
     * Get migration name
113
     *
114
     * @return string
115
     */
116 46
    public function name(): string
117
    {
118 46
        return get_class($this);
119
    }
120
121
    /**
122
     * Get the console input
123
     *
124
     * @return InputInterface
125
     */
126
    public function getInput(): InputInterface
127
    {
128
        return $this->input;
129
    }
130
131
    /**
132
     * Set the console input
133
     *
134
     * @param InputInterface $input
135
     *
136
     * @return void
137
     */
138 12
    public function setInput(InputInterface $input): void
139
    {
140 12
        $this->input = $input;
141
    }
142
143
    /**
144
     * Get the console output
145
     *
146
     * @return OutputInterface
147
     */
148
    public function getOutput(): OutputInterface
149
    {
150
        return $this->output;
151
    }
152
153
    /**
154
     * Set the console output
155
     *
156
     * @param OutputInterface $output
157
     *
158
     * @return void
159
     */
160 18
    public function setOutput(OutputInterface $output): void
161
    {
162 18
        $this->output = $output;
163
    }
164
165
    /**
166
     * Sets the helper set.
167
     *
168
     * @param HelperSet $helperSet A HelperSet instance
169
     *
170
     * @return void
171
     */
172 12
    public function setHelperSet(HelperSet $helperSet): void
173
    {
174 12
        $this->helperSet = $helperSet;
175
    }
176
177
    /**
178
     * Gets the helper set.
179
     *
180
     * @return HelperSet A HelperSet instance
181
     */
182
    public function getHelperSet()
183
    {
184
        return $this->helperSet;
185
    }
186
187
    /**
188
     * Execute a select query
189
     *
190
     * @param string $sql
191
     * @param array  $params
192
     * @param string $connectionName
193
     *
194
     * @return Result
195
     * @throws PrimeException
196
     */
197 2
    public function query($sql, array $params = [], $connectionName = null): Result
198
    {
199 2
        return $this->connection($connectionName)->executeQuery($sql, $params);
0 ignored issues
show
The method executeQuery() does not exist on Bdf\Prime\Connection\ConnectionInterface. Did you maybe mean execute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

199
        return $this->connection($connectionName)->/** @scrutinizer ignore-call */ executeQuery($sql, $params);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
200
    }
201
202
    /**
203
     * Execute a update query
204
     *
205
     * @param string $sql
206
     * @param array  $params
207
     * @param string $connectionName
208
     *
209
     * @return int
210
     * @throws PrimeException
211
     */
212 1
    public function update($sql, array $params = [], $connectionName = null)
213
    {
214 1
        return $this->connection($connectionName)->executeUpdate($sql, $params);
0 ignored issues
show
The method executeUpdate() does not exist on Bdf\Prime\Connection\ConnectionInterface. Did you maybe mean execute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

214
        return $this->connection($connectionName)->/** @scrutinizer ignore-call */ executeUpdate($sql, $params);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
    }
216
217
    /**
218
     * Get schema manager instance
219
     *
220
     * @param string $connectionName
221
     *
222
     * @return SchemaManager
223
     * @throws PrimeException
224
     */
225 1
    public function schema($connectionName = null)
226
    {
227 1
        return new SchemaManager($this->connection($connectionName));
228
    }
229
230
    /**
231
     * Get db connection
232
     *
233
     * @param string|null $connectionName
234
     *
235
     * @return ConnectionInterface&\Doctrine\DBAL\Connection
236
     */
237 3
    public function connection($connectionName = null)
238
    {
239
        /** @var ConnectionInterface&\Doctrine\DBAL\Connection */
240 3
        return $this->prime()->connection($connectionName);
241
    }
242
243
    /**
244
     * Get entity repository
245
     *
246
     * @param class-string<E>|E $entity
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<E>|E at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<E>|E.
Loading history...
247
     *
248
     * @return RepositoryInterface<E>
249
     *
250
     * @template E as object
251
     */
252
    public function repository($entity)
253
    {
254
        return $this->prime()->repository($entity);
255
    }
256
257
    /**
258
     * Get prime service locator
259
     *
260
     * @return ServiceLocator
261
     */
262 3
    public function prime()
263
    {
264 3
        return $this->di->get('prime');
265
    }
266
267
    /**
268
     * Logger extension accessor
269
     *
270
     * @return LoggerInterface
271
     */
272
    public function log()
273
    {
274
        return $this->di->get('logger');
275
    }
276
}
277