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

Commit a78fd3e

Browse files
committed
- Added payload builders
- Added unit tests for engine explain and profile methods - Covered artisan commands with unit tests - Made refactoring
1 parent 4f2f73a commit a78fd3e

17 files changed

+680
-300
lines changed

src/Console/ElasticIndexCommand.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Console/ElasticIndexCreateCommand.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,32 @@
22

33
namespace ScoutElastic\Console;
44

5+
use Illuminate\Console\Command;
6+
use ScoutElastic\Console\Features\requiresIndexConfiguratorArgument;
57
use ScoutElastic\Facades\ElasticClient;
8+
use ScoutElastic\Payloads\IndexPayload;
69

7-
class ElasticIndexCreateCommand extends ElasticIndexCommand
10+
class ElasticIndexCreateCommand extends Command
811
{
12+
use requiresIndexConfiguratorArgument;
13+
914
protected $name = 'elastic:create-index';
1015

1116
protected $description = 'Create an Elasticsearch index';
1217

13-
protected function buildPayload()
18+
public function fire()
1419
{
15-
$configurator = $this->getConfigurator();
16-
17-
$body = [];
18-
19-
if ($settings = $configurator->getSettings()) {
20-
$body['settings'] = $settings;
20+
if (!$configurator = $this->getIndexConfigurator()) {
21+
return;
2122
}
2223

23-
if ($defaultMappings = $configurator->getDefaultMapping()) {
24-
$body['mappings'] = ['_default_' => $defaultMappings];
25-
}
26-
27-
$payload = $this->buildBasePayload();
28-
29-
if ($body) {
30-
$payload['body'] = $body;
31-
}
32-
33-
return $payload;
34-
}
35-
36-
public function fire()
37-
{
38-
$configurator = $this->getConfigurator();
24+
$payload = (new IndexPayload($configurator))
25+
->setIfNotEmpty('body.settings', $configurator->getSettings())
26+
->setIfNotEmpty('body.mappings._default_', $configurator->getDefaultMapping())
27+
->get();
3928

4029
ElasticClient::indices()
41-
->create($this->buildPayload());
30+
->create($payload);
4231

4332
$this->info(sprintf(
4433
'The index %s was created!',

src/Console/ElasticIndexDropCommand.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,30 @@
22

33
namespace ScoutElastic\Console;
44

5+
use Illuminate\Console\Command;
6+
use ScoutElastic\Console\Features\requiresIndexConfiguratorArgument;
57
use ScoutElastic\Facades\ElasticClient;
8+
use ScoutElastic\Payloads\IndexPayload;
69

7-
class ElasticIndexDropCommand extends ElasticIndexCommand
10+
class ElasticIndexDropCommand extends Command
811
{
12+
use requiresIndexConfiguratorArgument;
13+
914
protected $name = 'elastic:drop-index';
1015

1116
protected $description = 'Drop an Elasticsearch index';
1217

1318
public function fire()
1419
{
15-
$configurator = $this->getConfigurator();
20+
if (!$configurator = $this->getIndexConfigurator()) {
21+
return;
22+
}
23+
24+
$payload = (new IndexPayload($configurator))
25+
->get();
1626

1727
ElasticClient::indices()
18-
->delete($this->buildBasePayload());
28+
->delete($payload);
1929

2030
$this->info(sprintf(
2131
'The index %s was deleted!',

src/Console/ElasticIndexUpdateCommand.php

Lines changed: 29 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,95 +2,69 @@
22

33
namespace ScoutElastic\Console;
44

5+
use Illuminate\Console\Command;
6+
use ScoutElastic\Console\Features\requiresIndexConfiguratorArgument;
57
use ScoutElastic\Facades\ElasticClient;
68
use Exception;
9+
use ScoutElastic\Payloads\IndexPayload;
710

8-
class ElasticIndexUpdateCommand extends ElasticIndexCommand
11+
class ElasticIndexUpdateCommand extends Command
912
{
13+
use requiresIndexConfiguratorArgument;
14+
1015
protected $name = 'elastic:update-index';
1116

1217
protected $description = 'Update settings and mappings of an Elasticsearch index';
1318

14-
protected function buildMappingPayload()
15-
{
16-
$configurator = $this->getConfigurator();
17-
18-
$defaultMapping = $configurator->getDefaultMapping();
19-
20-
if (!$defaultMapping) {
21-
return null;
22-
}
23-
24-
return array_merge(
25-
$this->buildBasePayload(),
26-
[
27-
'type' => '_default_',
28-
'body' => [
29-
'_default_' => $defaultMapping
30-
]
31-
]
32-
);
33-
}
34-
35-
protected function buildSettingsPayload()
36-
{
37-
$configurator = $this->getConfigurator();
38-
39-
$settings = $configurator->getSettings();
40-
41-
if (!$settings) {
42-
return null;
43-
}
44-
45-
return array_merge(
46-
$this->buildBasePayload(),
47-
[
48-
'body' => [
49-
'settings' => $settings
50-
]
51-
]
52-
);
53-
}
54-
5519
public function fire()
5620
{
57-
$configurator = $this->getConfigurator();
21+
if (!$configurator = $this->getIndexConfigurator()) {
22+
return;
23+
}
5824

59-
$indexName = $configurator->getName();
60-
$basePayload = $this->buildBasePayload();
25+
$indexPayload = (new IndexPayload($configurator))->get();
6126

6227
$indices = ElasticClient::indices();
6328

64-
if (!$indices->exists($basePayload)) {
29+
if (!$indices->exists($indexPayload)) {
6530
$this->error(sprintf(
6631
'Index %s doesn\'t exist',
67-
$indexName
32+
$configurator->getName()
6833
));
6934

7035
return;
7136
}
7237

7338
try {
74-
$indices->close($basePayload);
39+
$indices->close($indexPayload);
7540

76-
if ($settingsPayload = $this->buildSettingsPayload()) {
77-
$indices->putSettings($settingsPayload);
41+
if ($settings = $configurator->getSettings()) {
42+
$indexSettingsPayload = (new IndexPayload($configurator))
43+
->set('body.settings', $settings)
44+
->get();
45+
46+
$indices->putSettings($indexSettingsPayload);
7847
}
7948

80-
if ($mappingPayload = $this->buildMappingPayload()) {
81-
$indices->putMapping($mappingPayload);
49+
if ($defaultMapping = $configurator->getDefaultMapping()) {
50+
$indexMappingPayload = (new IndexPayload($configurator))
51+
->set('type', '_default_')
52+
->set('body._default_', $defaultMapping)
53+
->get();
54+
55+
$indices->putMapping($indexMappingPayload);
8256
}
8357

84-
$indices->open($basePayload);
58+
$indices->open($indexPayload);
8559
} catch (Exception $exception) {
86-
$indices->open($basePayload);
60+
$indices->open($indexPayload);
8761

8862
throw $exception;
8963
}
9064

9165
$this->info(sprintf(
9266
'The index %s was updated!',
93-
$indexName
67+
$configurator->getName()
9468
));
9569
}
9670
}

src/Console/ElasticUpdateMappingCommand.php

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,41 @@
33
namespace ScoutElastic\Console;
44

55
use Illuminate\Console\Command;
6+
use ScoutElastic\Console\Features\requiresModelArgument;
67
use ScoutElastic\Facades\ElasticClient;
7-
use ScoutElastic\SearchableModel;
8-
use Symfony\Component\Console\Input\InputArgument;
8+
use ScoutElastic\Payloads\TypePayload;
99

1010
class ElasticUpdateMappingCommand extends Command
1111
{
12+
use requiresModelArgument;
13+
1214
protected $name = 'elastic:update-mapping';
1315

1416
protected $description = 'Update a model mapping';
1517

16-
/**
17-
* @return SearchableModel
18-
*/
19-
protected function getModel()
20-
{
21-
$model = trim($this->argument('model'));
22-
return (new $model);
23-
}
24-
25-
protected function buildPayload()
26-
{
18+
public function fire() {
2719
$model = $this->getModel();
20+
2821
$configurator = $model->getIndexConfigurator();
2922

30-
$mapping = [];
23+
$mapping = array_merge_recursive($configurator->getDefaultMapping(), $model->getMapping());
3124

32-
if ($defaultMapping = $configurator->getDefaultMapping()) {
33-
$mapping = array_merge($mapping, $defaultMapping);
34-
}
25+
if (empty($mapping)) {
26+
$this->error('Nothing to update: the mapping is not specified.');
3527

36-
if ($modelMapping = $model->getMapping()) {
37-
$mapping = array_merge($mapping, $modelMapping);
28+
return;
3829
}
3930

40-
if (!$mapping) {
41-
return null;
42-
}
31+
$payload = (new TypePayload($model))
32+
->set('body.'.$model->searchableAs(), $mapping)
33+
->get();
4334

44-
return [
45-
'index' => $configurator->getName(),
46-
'type' => $model->searchableAs(),
47-
'body' => [$model->searchableAs() => $mapping]
48-
];
49-
}
50-
51-
public function fire() {
52-
$model = $this->getModel();
53-
54-
if ($payload = $this->buildPayload()) {
55-
ElasticClient::indices()
56-
->putMapping($payload);
57-
}
35+
ElasticClient::indices()
36+
->putMapping($payload);
5837

5938
$this->info(sprintf(
6039
'The %s mapping was updated!',
6140
$model->searchableAs()
6241
));
6342
}
64-
65-
protected function getArguments()
66-
{
67-
return [
68-
['model', InputArgument::REQUIRED, 'The model class'],
69-
];
70-
}
7143
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace ScoutElastic\Console\Features;
4+
5+
use ScoutElastic\IndexConfigurator;
6+
use Symfony\Component\Console\Input\InputArgument;
7+
8+
trait requiresIndexConfiguratorArgument
9+
{
10+
/**
11+
* @return IndexConfigurator
12+
*/
13+
protected function getIndexConfigurator()
14+
{
15+
$configuratorClass = trim($this->argument('index-configurator'));
16+
17+
$configuratorInstance = new $configuratorClass;
18+
19+
if (!($configuratorInstance instanceof IndexConfigurator)) {
20+
$this->error(sprintf(
21+
'The class %s must extend %s.',
22+
$configuratorClass,
23+
IndexConfigurator::class
24+
));
25+
26+
return null;
27+
}
28+
29+
return (new $configuratorClass);
30+
}
31+
32+
protected function getArguments()
33+
{
34+
return [
35+
['index-configurator', InputArgument::REQUIRED, 'The index configurator class'],
36+
];
37+
}
38+
}

0 commit comments

Comments
 (0)