|
4 | 4 |
|
5 | 5 | namespace Limenet\LaravelElasticaBridge\Index; |
6 | 6 |
|
| 7 | +use Elastic\Elasticsearch\Exception\ClientResponseException; |
7 | 8 | use Elastica\Document; |
8 | 9 | use Elastica\Exception\InvalidException; |
9 | 10 | use Elastica\Exception\NotFoundException; |
|
14 | 15 | use Illuminate\Database\Eloquent\Model; |
15 | 16 | use Illuminate\Support\Facades\Cache; |
16 | 17 | use Limenet\LaravelElasticaBridge\Client\ElasticaClient; |
| 18 | +use Limenet\LaravelElasticaBridge\Enum\IndexBlueGreenSuffix; |
17 | 19 | use Limenet\LaravelElasticaBridge\Exception\Index\BlueGreenIndicesIncorrectlySetupException; |
18 | 20 | use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface; |
| 21 | +use Limenet\LaravelElasticaBridge\Util\ElasticsearchResponse; |
19 | 22 | use RuntimeException; |
20 | 23 |
|
21 | 24 | abstract class AbstractIndex implements IndexInterface |
@@ -114,61 +117,54 @@ final public function hasBlueGreenIndices(): bool |
114 | 117 | { |
115 | 118 | return array_reduce( |
116 | 119 | array_map( |
117 | | - fn (string $suffix): bool => $this->client->getIndex($this->getName().$suffix)->exists(), |
118 | | - self::INDEX_SUFFIXES |
| 120 | + fn (IndexBlueGreenSuffix $suffix): bool => $this->client->getIndex($this->getName().$suffix->value)->exists(), |
| 121 | + IndexBlueGreenSuffix::cases() |
119 | 122 | ), |
120 | 123 | fn (bool $carry, bool $item): bool => $item && $carry, |
121 | 124 | true |
122 | 125 | ); |
123 | 126 | } |
124 | 127 |
|
125 | | - final public function getBlueGreenActiveSuffix(): string |
| 128 | + final public function getBlueGreenActiveSuffix(): IndexBlueGreenSuffix |
126 | 129 | { |
127 | 130 | if (! $this->hasBlueGreenIndices()) { |
128 | 131 | throw new BlueGreenIndicesIncorrectlySetupException; |
129 | 132 | } |
130 | 133 |
|
131 | | - $aliases = array_filter( |
132 | | - $this->client->getClient()->request('_aliases')->getData(), |
133 | | - fn (array $datum): bool => in_array($this->getName(), array_keys($datum['aliases']), true) |
134 | | - ); |
135 | | - |
136 | | - if (count($aliases) !== 1) { |
| 134 | + try { |
| 135 | + $aliases = array_filter( |
| 136 | + ElasticsearchResponse::getResponse($this->getElasticaIndex()->getClient()->indices()->getAlias(['name' => $this->getName()]))->asArray(), |
| 137 | + fn (array $datum): bool => array_key_exists($this->getName(), $datum['aliases']) |
| 138 | + ); |
| 139 | + } catch (ClientResponseException) { |
137 | 140 | throw new BlueGreenIndicesIncorrectlySetupException; |
138 | 141 | } |
139 | 142 |
|
140 | | - $suffix = substr((string) array_keys($aliases)[0], strlen($this->getName())); |
141 | | - |
142 | | - if (! in_array($suffix, self::INDEX_SUFFIXES, true)) { |
| 143 | + if (count($aliases) !== 1) { |
143 | 144 | throw new BlueGreenIndicesIncorrectlySetupException; |
144 | 145 | } |
145 | 146 |
|
146 | | - return $suffix; |
| 147 | + $suffix = substr(array_keys($aliases)[0], strlen($this->getName())); |
| 148 | + |
| 149 | + return IndexBlueGreenSuffix::tryFrom($suffix) ?? throw new BlueGreenIndicesIncorrectlySetupException; |
147 | 150 | } |
148 | 151 |
|
149 | | - final public function getBlueGreenInactiveSuffix(): string |
| 152 | + final public function getBlueGreenInactiveSuffix(): IndexBlueGreenSuffix |
150 | 153 | { |
151 | | - $active = $this->getBlueGreenActiveSuffix(); |
152 | | - |
153 | | - if ($active === self::INDEX_SUFFIX_BLUE) { |
154 | | - return self::INDEX_SUFFIX_GREEN; |
155 | | - } |
156 | | - |
157 | | - if ($active === self::INDEX_SUFFIX_GREEN) { |
158 | | - return self::INDEX_SUFFIX_BLUE; |
159 | | - } |
160 | | - |
161 | | - throw new BlueGreenIndicesIncorrectlySetupException; |
| 154 | + return match ($this->getBlueGreenActiveSuffix()) { |
| 155 | + IndexBlueGreenSuffix::BLUE => IndexBlueGreenSuffix::GREEN, |
| 156 | + IndexBlueGreenSuffix::GREEN => IndexBlueGreenSuffix::BLUE, |
| 157 | + }; |
162 | 158 | } |
163 | 159 |
|
164 | 160 | final public function getBlueGreenActiveElasticaIndex(): Index |
165 | 161 | { |
166 | | - return $this->client->getIndex($this->getName().$this->getBlueGreenActiveSuffix()); |
| 162 | + return $this->client->getIndex($this->getName().$this->getBlueGreenActiveSuffix()->value); |
167 | 163 | } |
168 | 164 |
|
169 | 165 | final public function getBlueGreenInactiveElasticaIndex(): Index |
170 | 166 | { |
171 | | - return $this->client->getIndex($this->getName().$this->getBlueGreenInactiveSuffix()); |
| 167 | + return $this->client->getIndex($this->getName().$this->getBlueGreenInactiveSuffix()->value); |
172 | 168 | } |
173 | 169 |
|
174 | 170 | final public function indexingLock(): Lock |
|
0 commit comments