Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| MigrationsService | |
0.00% |
0 / 14 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| getPersistence | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| migrate | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| extensionInstalled | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getPersistenceId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\migrations; |
| 24 | |
| 25 | use oat\generis\persistence\PersistenceManager; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use common_ext_event_ExtensionInstalled as ExtensionInstalled; |
| 28 | use oat\tao\scripts\tools\Migrations; |
| 29 | |
| 30 | /** |
| 31 | * Class MigrationsService |
| 32 | * |
| 33 | * @package oat\tao\model\migrations |
| 34 | */ |
| 35 | class MigrationsService extends ConfigurableService |
| 36 | { |
| 37 | public const SERVICE_ID = 'tao/MigrationsService'; |
| 38 | public const OPTION_PERSISTENCE_ID = 'persistence_id'; |
| 39 | private const DEFAULT_PERSISTENCE_ID = 'default'; |
| 40 | |
| 41 | /** |
| 42 | * @return \common_persistence_Persistence |
| 43 | */ |
| 44 | public function getPersistence() |
| 45 | { |
| 46 | /** @var PersistenceManager $persistenceManager */ |
| 47 | $persistenceManager = $this->getServiceLocator()->get(PersistenceManager::SERVICE_ID); |
| 48 | return $persistenceManager->getPersistenceById($this->getPersistenceId()); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Apply all migrations |
| 53 | * |
| 54 | * @return \common_report_Report |
| 55 | */ |
| 56 | public function migrate() |
| 57 | { |
| 58 | $migrations = new Migrations(); |
| 59 | $migrations->setServiceLocator($this->getServiceLocator()); |
| 60 | return $migrations(['-c', 'migrate']); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Skip extension migrations after installation |
| 65 | * common_ext_event_ExtensionInstalled event callback |
| 66 | * |
| 67 | * @param ExtensionInstalled $event |
| 68 | */ |
| 69 | public function extensionInstalled(ExtensionInstalled $event) |
| 70 | { |
| 71 | $migrations = new Migrations(); |
| 72 | $migrations->setServiceLocator($this->getServiceLocator()); |
| 73 | if ($event->getExtension()->getId() === 'tao') { |
| 74 | //supposedly application initial install and generis just have been installed. |
| 75 | $migrations->__invoke(['-c', 'init']); |
| 76 | $migrations->__invoke(['-c', 'add', '-e', 'generis']); |
| 77 | } |
| 78 | $migrations->__invoke(['-c', 'add', '-e', $event->getExtension()->getId()]); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return string |
| 83 | */ |
| 84 | private function getPersistenceId() |
| 85 | { |
| 86 | return $this->hasOption(self::OPTION_PERSISTENCE_ID) ? |
| 87 | $this->getOption(self::OPTION_PERSISTENCE_ID) : |
| 88 | self::DEFAULT_PERSISTENCE_ID; |
| 89 | } |
| 90 | } |