Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
8 / 9 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SchemaCollection | |
88.89% |
8 / 9 |
|
75.00% |
3 / 4 |
6.05 | |
0.00% |
0 / 1 |
| addSchema | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getOriginalSchema | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getSchema | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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 | * @license GPLv2 |
| 21 | */ |
| 22 | |
| 23 | namespace oat\generis\persistence\sql; |
| 24 | |
| 25 | use Doctrine\DBAL\Schema\Schema; |
| 26 | use IteratorAggregate; |
| 27 | use ArrayIterator; |
| 28 | use common_exception_InconsistentData; |
| 29 | |
| 30 | /** |
| 31 | * A collection of multiple schemas, in order to accomodate for the possibility |
| 32 | * of multiple RDS databases with different schemas |
| 33 | */ |
| 34 | class SchemaCollection implements IteratorAggregate |
| 35 | { |
| 36 | /** |
| 37 | * Schemas as they were originally added, prior to modifications |
| 38 | * @var Schema[] |
| 39 | */ |
| 40 | private $originals; |
| 41 | |
| 42 | /** |
| 43 | * Schemas per database, might have been modified |
| 44 | * @var Schema[] |
| 45 | */ |
| 46 | private $schemas; |
| 47 | |
| 48 | /** |
| 49 | * Add a schema to the collection |
| 50 | * @param string $id |
| 51 | * @param Schema $schema |
| 52 | */ |
| 53 | public function addSchema($id, Schema $schema): void |
| 54 | { |
| 55 | $this->originals[$id] = clone $schema; |
| 56 | $this->schemas[$id] = $schema; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get a schema as it was when it was added |
| 61 | * @param string $id |
| 62 | * @throws common_exception_InconsistentData |
| 63 | */ |
| 64 | public function getOriginalSchema($id): Schema |
| 65 | { |
| 66 | if (!isset($this->originals[$id])) { |
| 67 | throw new common_exception_InconsistentData('Expected original schema ' . $id . ' not found'); |
| 68 | } |
| 69 | return $this->originals[$id]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get a schema in its current form, might have been changed |
| 74 | * @param string $id |
| 75 | * @throws common_exception_InconsistentData |
| 76 | * @return Schema |
| 77 | */ |
| 78 | public function getSchema($id): Schema |
| 79 | { |
| 80 | if (!isset($this->schemas[$id])) { |
| 81 | throw new common_exception_InconsistentData('Expected schema ' . $id . ' not found'); |
| 82 | } |
| 83 | return $this->schemas[$id]; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * {@inheritDoc} |
| 88 | * @see IteratorAggregate::getIterator() |
| 89 | */ |
| 90 | public function getIterator() |
| 91 | { |
| 92 | return new ArrayIterator($this->schemas); |
| 93 | } |
| 94 | } |