Completed
Pull Request — master (#1510)
by
unknown
13:49 queued 20s
created

MongoMigrationAdapter::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Phinx\Db\Adapter;
4
5
use MongoDB\Client;
6
use MongoDB\Collection;
7
use MongoDB\Database;
8
use Phinx\Db\Action\AddIndex;
9
use Phinx\Db\Action\DropIndex;
10
use Phinx\Db\Action\DropTable;
11
use Phinx\Db\Adapter\AdapterInterface as PhinxAdapter;
12
use Phinx\Db\Table\Column;
13
use Phinx\Db\Table\Table;
14
use Phinx\Migration\MigrationInterface;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class MongoMigrationAdapter implements PhinxAdapter
19
{
20
21
    protected $collectionName = 'phinx_migration';
22
23
    /** @var InputInterface $consoleInput */
24
    protected $consoleInput;
25
26
    /** @var OutputInterface $consoleOutput */
27
    protected $consoleOutput;
28
29
    /** @var Client $mongoClient */
30
    protected $mongoClient;
31
32
    /** @var Database $database */
33
    protected $database;
34
35
    /** @var Collection $collection */
36
    protected $collection;
37
38
    /** @var string $databaseName */
39
    protected $databaseName;
40
41
    /** @var string $uri */
42
    protected $uri;
43
44
    protected $session;
45
46
    protected $options = [
47
        'table_prefix' => ''
48
    ];
49
50
    function __construct(array $options)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
    {
52
        $this->collectionName = $options['default_migration_table'];
53
        $this->databaseName = $options['name'];
54
        $this->uri = $options['uri'];
55
        $this->options = $options;
56
        $this->connect();
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getVersions()
63
    {
64
        return array_keys($this->getVersionLog());
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getVersionLog()
71
    {
72
        $result = [];
73
        $rows = $this->getMigrationCollection()->find();
74
        foreach ($rows as $row) {
75
            $result[$row['version']] = $row;
76
        }
77
        return $result;
78
    }
79
80
    /**
81
     * @param array $options
82
     * @return $this|PhinxAdapter
83
     */
84
    public function setOptions(array $options)
85
    {
86
        return $this;
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getOptions()
93
    {
94
        return $this->options;
95
    }
96
97
    public function hasOption($name)
98
    {
99
        return true;
100
    }
101
102
    /**
103
     * @param string $name
104
     * @return mixed|null
105
     */
106
    public function getOption($name)
107
    {
108
        if (isset($this->options[$name])) {
109
            return $this->options[$name];
110
        }
111
        return null;
112
    }
113
114
    /**
115
     * @param InputInterface $input
116
     * @return $this|PhinxAdapter
117
     */
118
    public function setInput(InputInterface $input)
119
    {
120
        $this->consoleInput = $input;
121
        return $this;
122
    }
123
124
    /**
125
     * @return InputInterface
126
     */
127
    public function getInput()
128
    {
129
        return $this->consoleInput;
130
    }
131
132
    /**
133
     * @param OutputInterface $output
134
     * @return PhinxAdapter
135
     */
136
    public function setOutput(OutputInterface $output)
137
    {
138
        $this->consoleOutput = $output;
139
        return $this;
140
    }
141
142
    public function getOutput()
143
    {
144
        return $this->consoleOutput;
145
    }
146
147
    /**
148
     * @param MigrationInterface $migration
149
     * @param string $direction
150
     * @param int $startTime
151
     * @param int $endTime
152
     * @return $this|PhinxAdapter
153
     */
154
    public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime)
155
    {
156
        if (strcasecmp($direction, MigrationInterface::UP) === 0) {
157
            $this->getMigrationCollection()->insertOne([
158
                'name' => $migration->getName(),
159
                'start_time' => $startTime,
160
                'end_time' => $endTime,
161
                'version' => $migration->getVersion(),
162
                'breakpoint' => false
163
            ]);
164
        } else {
165
            $this->getMigrationCollection()->deleteOne([
166
                'version' => $migration->getVersion()
167
            ]);
168
        }
169
        return $this;
170
    }
171
172
    /**
173
     * @param MigrationInterface $migration
174
     * @return $this|PhinxAdapter
175
     */
176
    public function toggleBreakpoint(MigrationInterface $migration)
177
    {
178
        return $this;
179
    }
180
181
    public function resetAllBreakpoints()
182
    {
183
        throw new \InvalidArgumentException("Not implemented");
184
    }
185
186
    /**
187
     * @return bool
188
     */
189
    public function hasSchemaTable()
190
    {
191
        return true;
192
    }
193
194
    public function createSchemaTable()
195
    {
196
        throw new \InvalidArgumentException("Not implemented");
197
    }
198
199
    public function getAdapterType()
200
    {
201
        return null;
202
    }
203
204
    public function connect()
205
    {
206
        $this->mongoClient = new Client($this->uri);
207
        $this->database = $this->mongoClient->selectDatabase($this->databaseName);
208
        $this->collection = $this->database->selectCollection($this->collectionName);
209
    }
210
211
    public function disconnect()
212
    {
213
        throw new \InvalidArgumentException("Not implemented");
214
    }
215
216
    public function hasTransactions()
217
    {
218
        return false;
219
    }
220
221
    public function beginTransaction()
222
    {
223
        throw new \InvalidArgumentException("Not implemented");
224
    }
225
226
    public function commitTransaction()
227
    {
228
        throw new \InvalidArgumentException("Not implemented");
229
    }
230
231
    public function rollbackTransaction()
232
    {
233
        throw new \InvalidArgumentException("Not implemented");
234
    }
235
236
    public function execute($sql)
237
    {
238
        throw new \InvalidArgumentException("Don't know how to execute");
239
    }
240
241
    public function executeActions(Table $table, array $actions)
242
    {
243
        foreach ($actions as $action) {
244
245
            if ($action instanceof AddIndex) {
246
                $columns = $action->getIndex()->getColumns();
247
                $options = [];
248
                $indexName = $action->getIndex()->getName();
249
                if (!empty($indexName)) {
250
                    $options['name'] = $indexName;
251
                }
252
                $type = $action->getIndex()->getType();
253
                if ($type == 'unique') {
254
                    $options['unique'] = true;
255
                }
256
                $this->getDatabase()
257
                    ->selectCollection($action->getTable()->getName())
258
                    ->createIndex($columns, $options);
259
            } elseif ($action instanceof DropIndex) {
260
                $indexName = $action->getIndex()->getName();
261
                if (!empty($indexName)) {
262
                    $this->getDatabase()->selectCollection($action->getTable()->getName())
263
                        ->dropIndex($indexName);
264
                }
265
            } elseif ($action instanceof DropTable) {
266
                $this->getDatabase()
267
                    ->dropCollection($action->getTable()->getName());
268
            } else {
269
                throw new \InvalidArgumentException(
270
                    sprintf("Don't know how to execute action: '%s'", get_class($action))
271
                );
272
            }
273
        }
274
    }
275
276
    public function getQueryBuilder()
277
    {
278
        return null;
279
    }
280
281
    public function query($sql)
282
    {
283
        throw new \InvalidArgumentException("Not implemented");
284
    }
285
286
    public function fetchRow($sql)
287
    {
288
        throw new \InvalidArgumentException("Not implemented");
289
    }
290
291
    public function fetchAll($sql)
292
    {
293
        throw new \InvalidArgumentException("Not implemented");
294
    }
295
296
    /**
297
     * @param Table $table
298
     * @param array $row
299
     */
300
    public function insert(Table $table, $row)
301
    {
302
        $this->getDatabase()->selectCollection($table->getName())->insertOne($row);
303
    }
304
305
    /**
306
     * @param Table $table
307
     * @param array $rows
308
     */
309
    public function bulkinsert(Table $table, $rows)
310
    {
311
        $this->getDatabase()->selectCollection($table->getName())->insertMany($rows);
312
    }
313
314
    /**
315
     * @param string $tableName
316
     * @return mixed|string
317
     */
318
    public function quoteTableName($tableName)
319
    {
320
        return str_replace('.', '`.`', $this->quoteColumnName($tableName));
321
    }
322
323
    /**
324
     * @param string $columnName
325
     * @return string
326
     */
327
    public function quoteColumnName($columnName)
328
    {
329
        return '`' . str_replace('`', '``', $columnName) . '`';
330
    }
331
332
    /**
333
     * @param string $tableName
334
     * @return bool
335
     */
336
    public function hasTable($tableName)
337
    {
338
        return true;
339
    }
340
341
    /**
342
     * @param Table $table
343
     * @param array $columns
344
     * @param array $indexes
345
     */
346
    public function createTable(Table $table, array $columns = [], array $indexes = [])
347
    {
348
    }
349
350
    /**
351
     * @param string $tableName
352
     */
353
    public function truncateTable($tableName)
354
    {
355
        $this->getDatabase()->dropCollection($tableName);
356
    }
357
358
    /**
359
     * @param string $tableName
360
     * @return array|Column[]
361
     */
362
    public function getColumns($tableName)
363
    {
364
        return [];
365
    }
366
367
    /**
368
     * @param string $tableName
369
     * @param string $columnName
370
     * @return bool
371
     */
372
    public function hasColumn($tableName, $columnName)
373
    {
374
        return true;
375
    }
376
377
    /**
378
     * @param string $tableName
379
     * @param mixed $columns
380
     * @return bool|void
381
     */
382
    public function hasIndex($tableName, $columns)
383
    {
384
        $indexes = $this->getDatabase()->selectCollection($tableName)->listIndexes();
385
        $this->hasValues($indexes, $columns);
0 ignored issues
show
Unused Code introduced by
The call to the method Phinx\Db\Adapter\MongoMi...ionAdapter::hasValues() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
386
    }
387
388
    /**
389
     * @param string $tableName
390
     * @param string $indexName
391
     * @return bool
392
     */
393
    public function hasIndexByName($tableName, $indexName)
394
    {
395
        $indexes = $this->getDatabase()->selectCollection($tableName)->listIndexes();
396
        return $this->hasValue($indexes, $indexName);
397
    }
398
399
    /**
400
     * @param string $tableName
401
     * @param string[] $columns
402
     * @param null $constraint
403
     * @return bool
404
     */
405
    public function hasPrimaryKey($tableName, $columns, $constraint = null)
406
    {
407
        return true;
408
    }
409
410
    /**
411
     * @param string $tableName
412
     * @param string[] $columns
413
     * @param null $constraint
414
     * @return bool
415
     */
416
    public function hasForeignKey($tableName, $columns, $constraint = null)
417
    {
418
        return false;
419
    }
420
421
    /**
422
     * @return array
423
     */
424
    public function getColumnTypes()
425
    {
426
        return [
427
            self::PHINX_TYPE_BIG_INTEGER,
428
            self::PHINX_TYPE_BOOLEAN,
429
            self::PHINX_TYPE_INTEGER,
430
            self::PHINX_TYPE_STRING,
431
            self::PHINX_TYPE_FLOAT
432
        ];
433
    }
434
435
    /**
436
     * @param Column $column
437
     * @return bool
438
     */
439
    public function isValidColumnType(Column $column)
440
    {
441
        return true;
442
    }
443
444
    /**
445
     * @param string $type
446
     * @param null $limit
447
     * @return array|string[]
448
     */
449
    public function getSqlType($type, $limit = null)
450
    {
451
        return [];
452
    }
453
454
    /**
455
     * @param string $name
456
     * @param array $options
457
     */
458
    public function createDatabase($name, $options = [])
459
    {
460
    }
461
462
    /**
463
     * @param string $name
464
     * @return bool
465
     */
466
    public function hasDatabase($name)
467
    {
468
        return true;
469
    }
470
471
    /**
472
     * @param string $name
473
     */
474
    public function dropDatabase($name)
475
    {
476
        $this->mongoClient->dropDatabase($name);
477
    }
478
479
    public function createSchema($schemaName = 'public')
480
    {
481
        throw new \InvalidArgumentException("Not implemented");
482
    }
483
484
    /**
485
     * @param string $schemaName
486
     */
487
    public function dropSchema($schemaName)
488
    {
489
        $this->getDatabase()->dropCollection($schemaName);
490
    }
491
492
    public function castToBool($value)
493
    {
494
        throw new \InvalidArgumentException("Not implemented");
495
    }
496
497
    protected function getMigrationCollection()
498
    {
499
        return $this->collection;
500
    }
501
502
    /**
503
     * @return Database
504
     */
505
    protected function getDatabase()
506
    {
507
        return $this->database;
508
    }
509
510
    /**
511
     * @param \Iterator $iterator
512
     * @param $value
513
     * @return bool
514
     */
515
    protected function hasValue(\Iterator $iterator, $value)
516
    {
517
        foreach ($iterator as $iteration) {
518
            if ($iteration == $value) {
519
                return true;
520
            }
521
        }
522
        return false;
523
    }
524
525
    /**
526
     * @param \Iterator $iterator
527
     * @param $values
528
     * @return bool
529
     */
530
    protected function hasValues(\Iterator $iterator, $values)
531
    {
532
        foreach ($iterator as $iteration) {
533
            if ($this->hasValue($iteration, $values)) {
534
                return true;
535
            }
536
        }
537
        return false;
538
    }
539
540
    /**
541
     * @return Client|\PDO
542
     */
543
    public function getConnection()
544
    {
545
        return $this->mongoClient;
546
    }
547
}