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