| @@ 13-69 (lines=57) @@ | ||
| 10 | * | |
| 11 | * @author gbprod <[email protected]> | |
| 12 | */ | |
| 13 | class PutIndexMappingsHandler | |
| 14 | { | |
| 15 | /** | |
| 16 | * @var ClientRepository | |
| 17 | */ | |
| 18 | private $clientRepository; | |
| 19 | ||
| 20 | /** | |
| 21 | * @var IndexConfigurationRepository | |
| 22 | */ | |
| 23 | private $configurationRepository; | |
| 24 | ||
| 25 | /** | |
| 26 | * @param ClientRepository $clientRepository | |
| 27 | * @param IndexConfigurationRepository $configurationRepository | |
| 28 | */ | |
| 29 | public function __construct( | |
| 30 | ClientRepository $clientRepository, | |
| 31 | IndexConfigurationRepository $configurationRepository | |
| 32 |     ) { | |
| 33 | $this->clientRepository = $clientRepository; | |
| 34 | $this->configurationRepository = $configurationRepository; | |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * Handle index creation command | |
| 39 | * | |
| 40 | * @param string $clientId | |
| 41 | * @param string $indexId | |
| 42 | */ | |
| 43 | public function handle($clientId, $indexId) | |
| 44 |     { | |
| 45 | $client = $this->clientRepository->get($clientId); | |
| 46 | $config = $this->configurationRepository->get($clientId, $indexId); | |
| 47 | ||
| 48 |         if (null === $client || null === $config) { | |
| 49 | throw new \InvalidArgumentException(); | |
| 50 | } | |
| 51 | ||
| 52 | $client | |
| 53 | ->indices() | |
| 54 | ->putMapping([ | |
| 55 | 'index' => $indexId, | |
| 56 | 'body' => $this->extractMapping($config), | |
| 57 | ]) | |
| 58 | ; | |
| 59 | } | |
| 60 | ||
| 61 | private function extractMapping($config) | |
| 62 |     { | |
| 63 |         if (isset($config['mappings'])) { | |
| 64 | return $config['mappings']; | |
| 65 | } | |
| 66 | ||
| 67 | return []; | |
| 68 | } | |
| 69 | } | |
| @@ 13-71 (lines=59) @@ | ||
| 10 | * | |
| 11 | * @author gbprod <[email protected]> | |
| 12 | */ | |
| 13 | class PutIndexSettingsHandler | |
| 14 | { | |
| 15 | /** | |
| 16 | * @var ClientRepository | |
| 17 | */ | |
| 18 | private $clientRepository; | |
| 19 | ||
| 20 | /** | |
| 21 | * @var IndexConfigurationRepository | |
| 22 | */ | |
| 23 | private $configurationRepository; | |
| 24 | ||
| 25 | /** | |
| 26 | * @param ClientRepository $clientRepository | |
| 27 | * @param IndexConfigurationRepository $configurationRepository | |
| 28 | */ | |
| 29 | public function __construct( | |
| 30 | ClientRepository $clientRepository, | |
| 31 | IndexConfigurationRepository $configurationRepository | |
| 32 |     ) { | |
| 33 | $this->clientRepository = $clientRepository; | |
| 34 | $this->configurationRepository = $configurationRepository; | |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * Handle index creation command | |
| 39 | * | |
| 40 | * @param string $clientId | |
| 41 | * @param string $indexId | |
| 42 | */ | |
| 43 | public function handle($clientId, $indexId) | |
| 44 |     { | |
| 45 | $client = $this->clientRepository->get($clientId); | |
| 46 | $config = $this->configurationRepository->get($clientId, $indexId); | |
| 47 | ||
| 48 |         if (null === $client || null === $config) { | |
| 49 | throw new \InvalidArgumentException(); | |
| 50 | } | |
| 51 | ||
| 52 | $client | |
| 53 | ->indices() | |
| 54 | ->putSettings([ | |
| 55 | 'index' => $indexId, | |
| 56 | 'body' => [ | |
| 57 | 'settings' => $this->extractSettings($config), | |
| 58 | ], | |
| 59 | ]) | |
| 60 | ; | |
| 61 | } | |
| 62 | ||
| 63 | private function extractSettings($config) | |
| 64 |     { | |
| 65 |         if (isset($config['settings'])) { | |
| 66 | return $config['settings']; | |
| 67 | } | |
| 68 | ||
| 69 | return []; | |
| 70 | } | |
| 71 | } | |