PutIndexMappingsHandler::isInvalid()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 4
nop 2
crap 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