Skip to content

Commit dd514d1

Browse files
author
Martynas Sudintas
committed
abstracted warmers
1 parent 13a112f commit dd514d1

File tree

6 files changed

+89
-58
lines changed

6 files changed

+89
-58
lines changed

Client/Connection.php

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public function clearCache()
457457

458458
/**
459459
* Adds warmer to conatiner.
460-
*
460+
*
461461
* @param WarmerInterface $warmer
462462
*/
463463
public function addWarmer(WarmerInterface $warmer)
@@ -467,39 +467,58 @@ public function addWarmer(WarmerInterface $warmer)
467467

468468
/**
469469
* Loads warmers into elasticseach.
470-
*
470+
*
471471
* @param array $names Warmers names to put.
472472
*/
473473
public function putWarmers(array $names = [])
474474
{
475-
foreach ($this->warmers->getWarmers() as $name => $body) {
476-
if (empty($names) || in_array($name, $names)) {
477-
$this->getClient()->indices()->putWarmer(
478-
[
479-
'index' => $this->getIndexName(),
480-
'name' => $name,
481-
'body' => $body,
482-
]
483-
);
484-
}
485-
}
475+
$this->warmersAction('put', $names);
486476
}
487477

488478
/**
489479
* Deletes warmers from elasticsearch index.
490-
*
480+
*
491481
* @param array $names Warmers names to delete.
492482
*/
493483
public function deleteWarmers(array $names = [])
484+
{
485+
$this->warmersAction('delete', $names);
486+
}
487+
488+
/**
489+
* Executes warmers actions.
490+
*
491+
* @param string $action Action name.
492+
* @param array $names Warmers names.
493+
*
494+
* @throws \LogicException
495+
*/
496+
private function warmersAction($action, $names = [])
494497
{
495498
foreach ($this->warmers->getWarmers() as $name => $body) {
496499
if (empty($names) || in_array($name, $names)) {
497-
$this->getClient()->indices()->deleteWarmer(
498-
[
499-
'index' => $this->getIndexName(),
500-
'name' => $name,
501-
]
502-
);
500+
switch($action)
501+
{
502+
case 'put':
503+
$this->getClient()->indices()->putWarmer(
504+
[
505+
'index' => $this->getIndexName(),
506+
'name' => $name,
507+
'body' => $body,
508+
]
509+
);
510+
break;
511+
case 'delete':
512+
$this->getClient()->indices()->deleteWarmer(
513+
[
514+
'index' => $this->getIndexName(),
515+
'name' => $name,
516+
]
517+
);
518+
break;
519+
default:
520+
throw new \LogicException('Unknown warmers action');
521+
}
503522
}
504523
}
505524
}

Command/AbstractElasticsearchCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function getManager($name)
4848

4949
/**
5050
* Returns elasticsearch connection by name.
51-
*
51+
*
5252
* @param string $name
5353
*
5454
* @return \ONGR\ElasticsearchBundle\Client\Connection
@@ -64,7 +64,7 @@ protected function getConnection($name)
6464
* @param string $name Connection name.
6565
*
6666
* @return string
67-
*
67+
*
6868
* @throws \RuntimeException
6969
*/
7070
private function getManagerNameByConnection($name)

Command/AbstractWarmerCommand.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Command;
13+
14+
use Symfony\Component\Console\Input\InputArgument;
15+
use Symfony\Component\Console\Input\InputOption;
16+
17+
/**
18+
* Class AbstractWarmerCommand.
19+
*/
20+
abstract class AbstractWarmerCommand extends AbstractElasticsearchCommand
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
protected function configure()
26+
{
27+
$this
28+
->addArgument(
29+
'names',
30+
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
31+
'Warmers names.',
32+
[]
33+
)
34+
->addOption(
35+
'connection',
36+
'c',
37+
InputOption::VALUE_REQUIRED,
38+
'Connection name.',
39+
'default'
40+
);
41+
}
42+
}

Command/WarmerDeleteCommand.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,15 @@
1919
/**
2020
* This command removes warmers from elasticsearch index.
2121
*/
22-
class WarmerDeleteCommand extends AbstractElasticsearchCommand
22+
class WarmerDeleteCommand extends AbstractWarmerCommand
2323
{
2424
/**
2525
* {@inheritdoc}
2626
*/
2727
protected function configure()
2828
{
29-
$this
30-
->setName('es:warmer:delete')
31-
->addArgument(
32-
'names',
33-
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
34-
'Warmers names to delete from index.',
35-
[]
36-
)
37-
->addOption(
38-
'connection',
39-
'c',
40-
InputOption::VALUE_REQUIRED,
41-
'Connection name to delete warmers from.',
42-
'default'
43-
);
29+
parent::configure();
30+
$this->setName('es:warmer:delete');
4431
}
4532

4633
/**
@@ -51,7 +38,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
5138
$names = $input->getArgument('names');
5239
$this->getConnection($input->getOption('connection'))->deleteWarmers($names);
5340

54-
$message = '';
5541
if (empty($names)) {
5642
$message = 'All warmers have been deleted from <info>%s</info> index.';
5743
} else {

Command/WarmerPutCommand.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,15 @@
1919
/**
2020
* This command puts warmers into elasticsearch index.
2121
*/
22-
class WarmerPutCommand extends AbstractElasticsearchCommand
22+
class WarmerPutCommand extends AbstractWarmerCommand
2323
{
2424
/**
2525
* {@inheritdoc}
2626
*/
2727
protected function configure()
2828
{
29-
$this
30-
->setName('es:warmer:put')
31-
->addArgument(
32-
'names',
33-
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
34-
'Warmers names to put into index.',
35-
[]
36-
)
37-
->addOption(
38-
'connection',
39-
'c',
40-
InputOption::VALUE_REQUIRED,
41-
'Connection name to put warmers to.',
42-
'default'
43-
);
29+
parent::configure();
30+
$this->setName('es:warmer:put');
4431
}
4532

4633
/**
@@ -51,7 +38,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
5138
$names = $input->getArgument('names');
5239
$this->getConnection($input->getOption('connection'))->putWarmers($names);
5340

54-
$message = '';
5541
if (empty($names)) {
5642
$message = 'All warmers have been put into <info>%s</info> index.';
5743
} else {

Tests/app/fixture/Acme/TestBundle/Document/Product.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
*/
2525
class Product extends Item implements DocumentInterface
2626
{
27-
use DocumentTrait;
28-
2927
/**
3028
* @var string
3129
*

0 commit comments

Comments
 (0)