Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.

Commit a46a511

Browse files
committed
Added RawPayload, fixed unit test
1 parent a8ed3dd commit a46a511

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

src/Console/ElasticIndexUpdateCommand.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use ScoutElastic\Console\Features\requiresIndexConfiguratorArgument;
99
use ScoutElastic\Facades\ElasticClient;
1010
use ScoutElastic\Payloads\IndexPayload;
11+
use ScoutElastic\Payloads\RawPayload;
1112

1213
class ElasticIndexUpdateCommand extends Command
1314
{
@@ -75,15 +76,19 @@ protected function createWriteAlias()
7576

7677
$indices = ElasticClient::indices();
7778

78-
if ($indices->existsAlias(['name' => $configurator->getWriteAlias()])) {
79+
$existsPayload = (new RawPayload())
80+
->set('name', $configurator->getWriteAlias())
81+
->get();
82+
83+
if ($indices->existsAlias($existsPayload)) {
7984
return;
8085
}
8186

82-
$payload = (new IndexPayload($configurator))
87+
$putPayload = (new IndexPayload($configurator))
8388
->set('name', $configurator->getWriteAlias())
8489
->get();
8590

86-
$indices->putAlias($payload);
91+
$indices->putAlias($putPayload);
8792

8893
$this->info(sprintf(
8994
'The %s alias for the %s index was created!',

src/Payloads/IndexPayload.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
use Exception;
66
use ScoutElastic\IndexConfigurator;
77

8-
class IndexPayload
8+
class IndexPayload extends RawPayload
99
{
10-
protected $payload = [];
11-
1210
protected $protectedKeys = [
1311
'index'
1412
];
@@ -41,24 +39,10 @@ public function useAlias($alias)
4139

4240
public function set($key, $value)
4341
{
44-
if (!is_null($key) && !in_array($key, $this->protectedKeys)) {
45-
array_set($this->payload, $key, $value);
46-
}
47-
48-
return $this;
49-
}
50-
51-
public function setIfNotEmpty($key, $value)
52-
{
53-
if (empty($value)) {
42+
if (in_array($key, $this->protectedKeys)) {
5443
return $this;
5544
}
5645

57-
return $this->set($key, $value);
58-
}
59-
60-
public function get($key = null)
61-
{
62-
return array_get($this->payload, $key);
46+
return parent::set($key, $value);
6347
}
6448
}

src/Payloads/RawPayload.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace ScoutElastic\Payloads;
4+
5+
class RawPayload
6+
{
7+
protected $payload = [];
8+
9+
public function set($key, $value)
10+
{
11+
if (!is_null($key)) {
12+
array_set($this->payload, $key, $value);
13+
}
14+
15+
return $this;
16+
}
17+
18+
public function setIfNotEmpty($key, $value)
19+
{
20+
if (empty($value)) {
21+
return $this;
22+
}
23+
24+
return $this->set($key, $value);
25+
}
26+
27+
public function get($key = null)
28+
{
29+
return array_get($this->payload, $key);
30+
}
31+
}

tests/ElasticEngineTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function test_if_the_update_method_builds_correct_payload()
2424
$this->mockClient()
2525
->shouldReceive('index')
2626
->with([
27-
'index' => 'test_index',
27+
'index' => 'test_index_write',
2828
'type' => 'test_table',
2929
'id' => 1,
3030
'body' => [

0 commit comments

Comments
 (0)