artem-prozorov /
repos
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Prozorov\Repositories; |
||
| 6 | |||
| 7 | use Prozorov\Repositories\Contracts\RepositoryInterface; |
||
| 8 | use Prozorov\Repositories\Exceptions\DataNotFound; |
||
| 9 | use Prozorov\Repositories\Exceptions\NotImplemented; |
||
| 10 | |||
| 11 | abstract class FixturableRepository implements RepositoryInterface |
||
| 12 | { |
||
| 13 | abstract protected function doGet($params): ?iterable; |
||
| 14 | |||
| 15 | abstract protected function doFirst(array $filter); |
||
| 16 | |||
| 17 | abstract protected function doGetById(int $id, array $select = null); |
||
| 18 | |||
| 19 | abstract protected function doGetByIdOrFail(int $id, array $select = null); |
||
| 20 | |||
| 21 | abstract protected function doCreate(array $data); |
||
| 22 | |||
| 23 | abstract protected function doUpdate($model, array $data): void; |
||
| 24 | |||
| 25 | abstract protected function doDelete($model): void; |
||
| 26 | |||
| 27 | abstract protected function doExists(array $filter): bool; |
||
| 28 | |||
| 29 | abstract protected function doCount(array $filter = []): int; |
||
| 30 | |||
| 31 | abstract protected function doOpenTransaction(): void; |
||
| 32 | |||
| 33 | abstract protected function doCommitTransaction(): void; |
||
| 34 | |||
| 35 | abstract protected function doRollbackTransaction(): void; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * isFake. |
||
| 39 | * |
||
| 40 | * @access protected |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | 1 | protected function isFake(): bool |
|
| 44 | { |
||
| 45 | // Fixtures mode is disabled by default. Please refer to the documentation |
||
| 46 | 1 | return false; |
|
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * getFixtures. |
||
| 51 | * |
||
| 52 | * @access protected |
||
| 53 | * @return ArrayRepository |
||
| 54 | */ |
||
| 55 | protected function fixtures(): ArrayRepository |
||
| 56 | { |
||
| 57 | throw new RuntimeException('Fixtures mode is disabled. Please refer to the documentation'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritDoc} |
||
| 62 | */ |
||
| 63 | 1 | public function get($params): ?iterable |
|
| 64 | { |
||
| 65 | 1 | if ($this->isFake()) { |
|
| 66 | 1 | return $this->fixtures()->get($params); |
|
| 67 | } |
||
| 68 | |||
| 69 | 1 | return $this->doGet($params); |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritDoc} |
||
| 74 | */ |
||
| 75 | public function first(array $filter) |
||
| 76 | { |
||
| 77 | if ($this->isFake()) { |
||
| 78 | return $this->fixtures()->first($filter); |
||
| 79 | } |
||
| 80 | |||
| 81 | return $this->doFirst($filter); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritDoc} |
||
| 86 | */ |
||
| 87 | 1 | public function getById(int $id, array $select = null) |
|
| 88 | { |
||
| 89 | 1 | if ($this->isFake()) { |
|
| 90 | 1 | return $this->fixtures()->getById($id, $select); |
|
| 91 | } |
||
| 92 | |||
| 93 | return $this->doGetById($id, $select); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritDoc} |
||
| 98 | */ |
||
| 99 | public function getByIdOrFail(int $id, array $select = null) |
||
| 100 | { |
||
| 101 | if ($this->isFake()) { |
||
| 102 | return $this->fixtures()->getByIdOrFail($id, $select); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $this->doGetByIdOrFail($id, $select); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritDoc} |
||
| 110 | */ |
||
| 111 | public function create(array $data) |
||
| 112 | { |
||
| 113 | if ($this->isFake()) { |
||
| 114 | return $this->fixtures()->create($data); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $this->doCreate($data); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritDoc} |
||
| 122 | */ |
||
| 123 | public function update($model, array $data): void |
||
| 124 | { |
||
| 125 | if ($this->isFake()) { |
||
| 126 | $this->fixtures()->update($model, $data); |
||
| 127 | |||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->doUpdate($model, $data); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritDoc} |
||
| 136 | */ |
||
| 137 | public function delete($model): void |
||
| 138 | { |
||
| 139 | if ($this->isFake()) { |
||
| 140 | $this->fixtures()->delete($model); |
||
| 141 | |||
| 142 | return; |
||
| 143 | } |
||
| 144 | |||
| 145 | $this->doDelete($model); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritDoc} |
||
| 150 | */ |
||
| 151 | public function exists(array $filter): bool |
||
| 152 | { |
||
| 153 | if ($this->isFake()) { |
||
| 154 | return $this->fixtures()->exists($filter); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $this->doExists($filter); |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritDoc} |
||
| 162 | */ |
||
| 163 | public function count(array $filter = []): int |
||
| 164 | { |
||
| 165 | if ($this->isFake()) { |
||
| 166 | return $this->fixtures()->count($filter); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $this->doCount($filter); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritDoc} |
||
| 174 | */ |
||
| 175 | public function openTransaction(): void |
||
| 176 | { |
||
| 177 | if ($this->isFake()) { |
||
| 178 | $this->fixtures()->openTransaction(); |
||
| 179 | |||
| 180 | return; |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->doOpenTransaction(); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritDoc} |
||
| 188 | */ |
||
| 189 | public function commitTransaction(): void |
||
| 190 | { |
||
| 191 | if ($this->isFake()) { |
||
| 192 | $this->fixtures()->commitTransaction(); |
||
| 193 | |||
| 194 | return; |
||
| 195 | } |
||
| 196 | |||
| 197 | $this->doCommitTransaction(); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritDoc} |
||
| 202 | */ |
||
| 203 | public function rollbackTransaction(): void |
||
| 204 | { |
||
| 205 | if ($this->isFake()) { |
||
| 206 | $this->fixtures()->rollbackTransaction(); |
||
| 207 | |||
| 208 | return; |
||
| 209 | } |
||
| 210 | |||
| 211 | $this->doRollbackTransaction(); |
||
| 212 | } |
||
| 213 | } |
||
| 214 |