Passed
Push — master ( 3f23ea...c594c9 )
by Jan
05:15 queued 11s
created

MigrationListener::onMigrationsMigrated()   A

Complexity

Conditions 5
Paths 13

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 13
nop 1
dl 0
loc 19
rs 9.6111
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\EventSubscriber;
23
24
25
use App\Entity\LogSystem\DatabaseUpdatedLogEntry;
26
use App\Services\LogSystem\EventLogger;
27
use Doctrine\Common\EventSubscriber;
28
use Doctrine\Migrations\Event\MigrationsEventArgs;
29
use Doctrine\Migrations\Event\MigrationsVersionEventArgs;
30
use Doctrine\Migrations\Events;
31
32
class MigrationListener implements EventSubscriber
33
{
34
    protected $old_version = null;
35
    protected $new_version = null;
36
37
    protected $eventLogger;
38
39
    public function __construct(EventLogger $eventLogger)
40
    {
41
        $this->eventLogger = $eventLogger;
42
    }
43
44
    public function onMigrationsMigrated(MigrationsEventArgs $args): void
45
    {
46
        //Dont do anything if this was a dry run
47
        if ($args->isDryRun()) {
48
            return;
49
        }
50
51
        //Save the version after the migration
52
        $this->new_version = $args->getConfiguration()->getCurrentVersion();
53
54
        //After everything is done, write the results to DB log
55
        $this->old_version = empty($this->old_version) ? 'legacy/empty' : $this->old_version;
56
        $this->new_version = empty($this->new_version) ? 'unknown' : $this->new_version;
57
58
59
        try {
60
            $log = new DatabaseUpdatedLogEntry($this->old_version, $this->new_version);
61
            $this->eventLogger->logAndFlush($log);
62
        } catch (\Exception $exception) {
63
            //Ignore any exception occuring here...
64
        }
65
66
    }
67
68
69
    public function onMigrationsMigrating(MigrationsEventArgs $args): void
70
    {
71
        // Save the version before any migration
72
        if ($this->old_version == null) {
73
            $this->old_version = $args->getConfiguration()->getCurrentVersion();
74
        }
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function getSubscribedEvents()
81
    {
82
        return [
83
            Events::onMigrationsMigrated,
84
            Events::onMigrationsMigrating,
85
            ];
86
    }
87
}