Filter
1 month ago
Rule
1 month ago
Sort
1 month ago
Storage
1 month ago
Base.php
1 month ago
Database.php
1 month ago
Filter.php
1 month ago
Rule.php
1 month ago
Rules.php
1 month ago
Sort.php
1 month ago
Storage.php
1 month ago
Types.php
1 month ago
Storage.php
173 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\ListScreenRepository; |
| 6 | |
| 7 | use AC\ListScreen; |
| 8 | use AC\ListScreenCollection; |
| 9 | use AC\ListScreenRepository; |
| 10 | use AC\ListScreenRepositoryWritable; |
| 11 | use AC\Type\ListScreenId; |
| 12 | use AC\Type\ListScreenStatus; |
| 13 | use AC\Type\TableId; |
| 14 | use LogicException; |
| 15 | |
| 16 | final class Storage extends Base implements ListScreenRepositoryWritable |
| 17 | { |
| 18 | |
| 19 | /** |
| 20 | * @var Storage\ListScreenRepository[] |
| 21 | */ |
| 22 | private array $repositories = []; |
| 23 | |
| 24 | /** |
| 25 | * @return Storage\ListScreenRepository[] |
| 26 | */ |
| 27 | public function get_repositories(): array |
| 28 | { |
| 29 | return array_reverse($this->repositories); |
| 30 | } |
| 31 | |
| 32 | public function set_repositories(array $repositories): void |
| 33 | { |
| 34 | foreach ($repositories as $repository) { |
| 35 | if ( ! $repository instanceof ListScreenRepository\Storage\ListScreenRepository) { |
| 36 | throw new LogicException('Expected a Storage\ListScreenRepository object.'); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | $this->repositories = array_reverse($repositories); |
| 41 | } |
| 42 | |
| 43 | public function with_repository(string $name, ListScreenRepository\Storage\ListScreenRepository $repository): self |
| 44 | { |
| 45 | $repositories = $this->get_repositories(); |
| 46 | $repositories[$name] = $repository; |
| 47 | |
| 48 | $storage = new self(); |
| 49 | $storage->set_repositories($repositories); |
| 50 | |
| 51 | return $storage; |
| 52 | } |
| 53 | |
| 54 | public function has_repository($key): bool |
| 55 | { |
| 56 | return array_key_exists($key, $this->repositories); |
| 57 | } |
| 58 | |
| 59 | public function get_repository($key): Storage\ListScreenRepository |
| 60 | { |
| 61 | if ( ! $this->has_repository($key)) { |
| 62 | throw new LogicException(sprintf('Repository with key %s not found.', $key)); |
| 63 | } |
| 64 | |
| 65 | return $this->repositories[$key]; |
| 66 | } |
| 67 | |
| 68 | public function find(ListScreenId $id): ?ListScreen |
| 69 | { |
| 70 | foreach ($this->repositories as $repository) { |
| 71 | $list_screen = $repository->get_list_screen_repository()->find($id); |
| 72 | |
| 73 | if ($list_screen) { |
| 74 | $list_screen->set_read_only(! $repository->is_writable()); |
| 75 | |
| 76 | return $list_screen; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | public function find_all(?Sort $sort = null): ListScreenCollection |
| 84 | { |
| 85 | $collection = new ListScreenCollection(); |
| 86 | |
| 87 | foreach ($this->repositories as $repository) { |
| 88 | foreach ($repository->get_list_screen_repository()->find_all() as $list_screen) { |
| 89 | if ( ! $collection->contains($list_screen)) { |
| 90 | $list_screen->set_read_only(! $repository->is_writable()); |
| 91 | |
| 92 | $collection->add($list_screen); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $this->sort($collection, $sort); |
| 98 | } |
| 99 | |
| 100 | public function find_all_by_table_id( |
| 101 | TableId $table_id, |
| 102 | ?Sort $sort = null, |
| 103 | ?ListScreenStatus $status = null |
| 104 | ): ListScreenCollection { |
| 105 | $collection = new ListScreenCollection(); |
| 106 | |
| 107 | foreach ($this->repositories as $repository) { |
| 108 | $list_screens = $repository |
| 109 | ->get_list_screen_repository() |
| 110 | ->find_all_by_table_id($table_id, null, $status); |
| 111 | |
| 112 | foreach ($list_screens as $list_screen) { |
| 113 | if ( ! $collection->contains($list_screen)) { |
| 114 | $list_screen->set_read_only(! $repository->is_writable()); |
| 115 | |
| 116 | $collection->add($list_screen); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return $this->sort($collection, $sort); |
| 122 | } |
| 123 | |
| 124 | public function save(ListScreen $list_screen): void |
| 125 | { |
| 126 | $repository = $this->get_writable_repository($list_screen); |
| 127 | |
| 128 | if ($repository) { |
| 129 | $repository->save($list_screen); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public function delete(ListScreen $list_screen): void |
| 134 | { |
| 135 | foreach ($this->get_writable_repositories($list_screen) as $repository) { |
| 136 | if ($repository->find($list_screen->get_id())) { |
| 137 | $repository->delete($list_screen); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private function get_writable_repository(ListScreen $list_screen): ?ListScreenRepositoryWritable |
| 144 | { |
| 145 | return $this->get_writable_repositories($list_screen)[0] ?? null; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @return ListScreenRepositoryWritable[] |
| 150 | */ |
| 151 | private function get_writable_repositories(ListScreen $list_screen): array |
| 152 | { |
| 153 | $repositories = []; |
| 154 | |
| 155 | foreach ($this->repositories as $repository) { |
| 156 | $match = true; |
| 157 | |
| 158 | if ($repository->has_rules()) { |
| 159 | $match = $repository->get_rules()->match([ |
| 160 | Rule::ID => $list_screen->get_id(), |
| 161 | Rule::TYPE => (string)$list_screen->get_table_id(), |
| 162 | ]); |
| 163 | } |
| 164 | |
| 165 | if ($match && $repository->is_writable()) { |
| 166 | $repositories[] = $repository->get_list_screen_repository(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return $repositories; |
| 171 | } |
| 172 | |
| 173 | } |