Passed
Push — master ( e8f83f...f116c2 )
by Jan
05:37
created

LogDBMigrationSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 47
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onMigrationsMigrated() 0 18 5
A onMigrationsMigrating() 0 5 2
A getSubscribedEvents() 0 5 1
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 modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (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 Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\EventSubscriber\LogSystem;
44
45
use App\Entity\LogSystem\DatabaseUpdatedLogEntry;
46
use App\Services\LogSystem\EventLogger;
47
use Doctrine\Common\EventSubscriber;
48
use Doctrine\Migrations\Event\MigrationsEventArgs;
49
use Doctrine\Migrations\Events;
50
51
/**
52
 * This subscriber logs databaseMigrations to Event log.
53
 * @package App\EventSubscriber
54
 */
55
class LogDBMigrationSubscriber implements EventSubscriber
56
{
57
    protected $old_version = null;
58
    protected $new_version = null;
59
60
    protected $eventLogger;
61
62
    public function __construct(EventLogger $eventLogger)
63
    {
64
        $this->eventLogger = $eventLogger;
65
    }
66
67
    public function onMigrationsMigrated(MigrationsEventArgs $args): void
68
    {
69
        //Dont do anything if this was a dry run
70
        if ($args->isDryRun()) {
71
            return;
72
        }
73
74
        //Save the version after the migration
75
        $this->new_version = $args->getConfiguration()->getCurrentVersion();
76
77
        //After everything is done, write the results to DB log
78
        $this->old_version = empty($this->old_version) ? 'legacy/empty' : $this->old_version;
79
        $this->new_version = empty($this->new_version) ? 'unknown' : $this->new_version;
80
81
        try {
82
            $log = new DatabaseUpdatedLogEntry($this->old_version, $this->new_version);
83
            $this->eventLogger->logAndFlush($log);
84
        } catch (\Throwable $exception) {
85
            //Ignore any exception occuring here...
86
        }
87
    }
88
89
    public function onMigrationsMigrating(MigrationsEventArgs $args): void
90
    {
91
        // Save the version before any migration
92
        if (null === $this->old_version) {
93
            $this->old_version = $args->getConfiguration()->getCurrentVersion();
94
        }
95
    }
96
97
    public function getSubscribedEvents()
98
    {
99
        return [
100
            Events::onMigrationsMigrated,
101
            Events::onMigrationsMigrating,
102
        ];
103
    }
104
}
105