PutIndexMappingsHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 50
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 19 2
A isInvalid() 0 8 4
1
<?php
2
3
namespace GBProd\ElasticsearchExtraBundle\Handler;
4
5
use Elasticsearch\Client;
6
use GBProd\ElasticsearchExtraBundle\Repository\IndexConfigurationRepository;
7
8
/**
9
 * Handler to put index mappings command
10
 *
11
 * @author gbprod <[email protected]>
12
 */
13
class PutIndexMappingsHandler
14
{
15
    /**
16
     * @var IndexConfigurationRepository
17
     */
18
    private $configurationRepository;
19
20
    /**
21
     * @param IndexConfigurationRepository $configurationRepository
22
     */
23
    public function __construct(IndexConfigurationRepository $configurationRepository)
24
    {
25 1
        $this->configurationRepository = $configurationRepository;
26 1
    }
27
28
    /**
29
     * Handle index creation command
30
     *
31
     * @param Client $client
32
     * @param string $index
33
     */
34
    public function handle(Client $client, $index, $type)
35
    {
36 1
        $config = $this->configurationRepository->get($index);
37
38 1
        if ($this->isInvalid($config, $type)) {
39
            throw new \InvalidArgumentException();
40
        }
41
42
        $client
43 1
            ->indices()
44 1
            ->putMapping([
45 1
                'index' => $index,
46 1
                'type'  => $type,
47
                'body'  => [
48 1
                    $type => $config['mappings'][$type],
49 1
                ],
50 1
            ])
51
        ;
52 1
    }
53
54
    private function isInvalid($config, $type)
55
    {
56 1
        return null === $config
57 1
            || empty($type)
58 1
            || !isset($config['mappings'])
59 1
            || !isset($config['mappings'][$type])
60 1
        ;
61
    }
62
}
63