Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
99.06% |
105 / 106 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| DependencyRepository | |
99.06% |
105 / 106 |
|
83.33% |
5 / 6 |
9 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findChildListIds | |
96.30% |
26 / 27 |
|
0.00% |
0 / 1 |
3 | |||
| findAll | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
2 | |||
| findChildListUris | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
1 | |||
| findChildListItemsUris | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
1 | |||
| getQueryBuilder | |
100.00% |
4 / 4 |
|
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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Lists\DataAccess\Repository; |
| 24 | |
| 25 | use Doctrine\DBAL\FetchMode; |
| 26 | use Doctrine\DBAL\Connection; |
| 27 | use InvalidArgumentException; |
| 28 | use Doctrine\DBAL\Query\QueryBuilder; |
| 29 | use oat\tao\model\Context\ContextInterface; |
| 30 | use oat\generis\persistence\PersistenceManager; |
| 31 | use oat\tao\model\Lists\Business\Domain\Dependency; |
| 32 | use oat\tao\model\Lists\Business\Domain\DependencyCollection; |
| 33 | use oat\tao\model\Lists\Business\Domain\DependencyRepositoryContext; |
| 34 | use oat\tao\model\Lists\Business\Contract\DependencyRepositoryInterface; |
| 35 | |
| 36 | class DependencyRepository implements DependencyRepositoryInterface |
| 37 | { |
| 38 | public const OPTION_PARENT_LIST_URIS = 'parentListUris'; |
| 39 | public const OPTION_PARENT_LIST_VALUES = 'parentListValues'; |
| 40 | public const OPTION_LIST_URI = 'listUri'; |
| 41 | public const OPTION_PARENT_LIST_URI = 'parentListUri'; |
| 42 | |
| 43 | /** @var PersistenceManager */ |
| 44 | private $persistenceManager; |
| 45 | |
| 46 | public function __construct(PersistenceManager $persistenceManager) |
| 47 | { |
| 48 | $this->persistenceManager = $persistenceManager; |
| 49 | } |
| 50 | |
| 51 | public function findChildListIds(array $options): array |
| 52 | { |
| 53 | if (empty($options[self::OPTION_PARENT_LIST_URIS]) || empty($options[self::OPTION_PARENT_LIST_VALUES])) { |
| 54 | throw new InvalidArgumentException('Parameters (parentListUris, parentListValues) are required'); |
| 55 | } |
| 56 | |
| 57 | $query = $this->getQueryBuilder(); |
| 58 | $expressionBuilder = $query->expr(); |
| 59 | |
| 60 | return $query |
| 61 | ->select('list_item_id') |
| 62 | ->from(RdsValueCollectionRepository::TABLE_LIST_ITEMS_DEPENDENCIES, 'dependencies') |
| 63 | ->innerJoin( |
| 64 | 'dependencies', |
| 65 | RdsValueCollectionRepository::TABLE_LIST_ITEMS, |
| 66 | 'items', |
| 67 | $expressionBuilder->eq('dependencies.value', 'items.uri') |
| 68 | ) |
| 69 | ->andWhere($expressionBuilder->in('items.list_uri', ':parent_list_uri')) |
| 70 | ->andWhere($expressionBuilder->in('dependencies.value', ':parent_list_value')) |
| 71 | ->setParameter( |
| 72 | 'parent_list_uri', |
| 73 | $options[self::OPTION_PARENT_LIST_URIS], |
| 74 | Connection::PARAM_STR_ARRAY |
| 75 | ) |
| 76 | ->setParameter( |
| 77 | 'parent_list_value', |
| 78 | $options[self::OPTION_PARENT_LIST_VALUES], |
| 79 | Connection::PARAM_STR_ARRAY |
| 80 | ) |
| 81 | ->execute() |
| 82 | ->fetchAll(FetchMode::COLUMN); |
| 83 | } |
| 84 | |
| 85 | public function findAll(array $options): DependencyCollection |
| 86 | { |
| 87 | $remoteListUri = $options[self::OPTION_LIST_URI]; |
| 88 | |
| 89 | $query = $this->getQueryBuilder(); |
| 90 | $expressionBuilder = $query->expr(); |
| 91 | |
| 92 | $query |
| 93 | ->select('value') |
| 94 | ->from(RdsValueCollectionRepository::TABLE_LIST_ITEMS_DEPENDENCIES, 'dependencies') |
| 95 | ->innerJoin( |
| 96 | 'dependencies', |
| 97 | RdsValueCollectionRepository::TABLE_LIST_ITEMS, |
| 98 | 'items', |
| 99 | $expressionBuilder->eq( |
| 100 | 'dependencies.' . RdsValueCollectionRepository::FIELD_LIST_ITEM_ID, |
| 101 | 'items.' . RdsValueCollectionRepository::FIELD_ITEM_ID |
| 102 | ) |
| 103 | ) |
| 104 | ->andWhere($expressionBuilder->eq('items.list_uri', ':label_uri')) |
| 105 | ->setParameter('label_uri', $remoteListUri); |
| 106 | |
| 107 | $collection = new DependencyCollection(); |
| 108 | |
| 109 | foreach ($query->execute()->fetchAll(FetchMode::COLUMN) as $column) { |
| 110 | $collection->append(new Dependency($column)); |
| 111 | } |
| 112 | |
| 113 | return $collection; |
| 114 | } |
| 115 | |
| 116 | public function findChildListUris(array $options): array |
| 117 | { |
| 118 | $query = $this->getQueryBuilder(); |
| 119 | $expressionBuilder = $query->expr(); |
| 120 | |
| 121 | return $query |
| 122 | ->select(RdsValueCollectionRepository::FIELD_ITEM_LIST_URI) |
| 123 | ->from(RdsValueCollectionRepository::TABLE_LIST_ITEMS, 'items') |
| 124 | ->where( |
| 125 | $expressionBuilder->in( |
| 126 | 'items.id', |
| 127 | $this->getQueryBuilder() |
| 128 | ->select('dependencies.' . RdsValueCollectionRepository::FIELD_LIST_ITEM_ID) |
| 129 | ->from(RdsValueCollectionRepository::TABLE_LIST_ITEMS_DEPENDENCIES, 'dependencies') |
| 130 | ->innerJoin( |
| 131 | 'dependencies', |
| 132 | RdsValueCollectionRepository::TABLE_LIST_ITEMS, |
| 133 | 'items', |
| 134 | $expressionBuilder->eq( |
| 135 | 'dependencies.' . RdsValueCollectionRepository::FIELD_LIST_ITEM_VALUE, |
| 136 | 'items.' . RdsValueCollectionRepository::FIELD_ITEM_URI |
| 137 | ) |
| 138 | ) |
| 139 | ->andWhere($expressionBuilder->eq('items.list_uri', ':list_uri')) |
| 140 | ->getSQL() |
| 141 | ) |
| 142 | ) |
| 143 | ->setParameter('list_uri', $options[self::OPTION_PARENT_LIST_URI]) |
| 144 | ->groupBy(RdsValueCollectionRepository::FIELD_ITEM_LIST_URI) |
| 145 | ->execute() |
| 146 | ->fetchAll(FetchMode::COLUMN); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * {@inheritdoc} |
| 151 | */ |
| 152 | public function findChildListItemsUris(ContextInterface $context): array |
| 153 | { |
| 154 | $parentListUris = $context->getParameter(DependencyRepositoryContext::PARAM_LIST_URIS); |
| 155 | $parentListValues = $context->getParameter( |
| 156 | DependencyRepositoryContext::PARAM_DEPENDENCY_LIST_VALUES, |
| 157 | [] |
| 158 | ); |
| 159 | |
| 160 | $childListIds = $this->findChildListIds( |
| 161 | [ |
| 162 | self::OPTION_PARENT_LIST_URIS => $parentListUris, |
| 163 | self::OPTION_PARENT_LIST_VALUES => $parentListValues, |
| 164 | ] |
| 165 | ); |
| 166 | |
| 167 | $query = $this->getQueryBuilder(); |
| 168 | $expressionBuilder = $query->expr(); |
| 169 | |
| 170 | return $query |
| 171 | ->select(RdsValueCollectionRepository::FIELD_ITEM_URI) |
| 172 | ->from(RdsValueCollectionRepository::TABLE_LIST_ITEMS, 'items') |
| 173 | ->where( |
| 174 | $expressionBuilder->in( |
| 175 | 'items.' . RdsValueCollectionRepository::FIELD_ITEM_ID, |
| 176 | ':child_list_ids' |
| 177 | ) |
| 178 | ) |
| 179 | ->setParameter('child_list_ids', $childListIds, Connection::PARAM_STR_ARRAY) |
| 180 | ->execute() |
| 181 | ->fetchAll(FetchMode::COLUMN); |
| 182 | } |
| 183 | |
| 184 | private function getQueryBuilder(): QueryBuilder |
| 185 | { |
| 186 | return $this->persistenceManager |
| 187 | ->getPersistenceById('default') |
| 188 | ->getPlatform() |
| 189 | ->getQueryBuilder(); |
| 190 | } |
| 191 | } |