Completed
Push — master ( 5bfb01...d72a06 )
by GBProd
02:17
created

PutIndexMappingsHandler::isInvalid()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 8.8571
cc 5
eloc 6
nc 5
nop 3
crap 5
1
<?php
2
3
namespace GBProd\ElasticsearchExtraBundle\Handler;
4
5
use GBProd\ElasticsearchExtraBundle\Repository\ClientRepository;
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 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 1
        $this->clientRepository        = $clientRepository;
34 1
        $this->configurationRepository = $configurationRepository;
35 1
    }
36
37
    /**
38
     * Handle index creation command
39
     *
40
     * @param string $clientId
41
     * @param string $indexId
42
     */
43 1
    public function handle($clientId, $indexId, $typeId)
44
    {
45 1
        $client = $this->clientRepository->get($clientId);
46 1
        $config = $this->configurationRepository->get($clientId, $indexId);
47
48 1
        if ($this->isInvalid($client, $config, $typeId)) {
49 1
            throw new \InvalidArgumentException();
50
        }
51
        
52
        $client
53 1
            ->indices()
54 1
            ->putMapping([
55 1
                'index' => $indexId,
56 1
                'type'  => $typeId,
57
                'body'  => [
58 1
                    $typeId => $config['mappings'][$typeId],
59 1
                ],
60 1
            ])
61
        ;
62 1
    }
63
    
64
    private function isInvalid($client, $config, $typeId)
65
    {
66 1
        return null === $client 
67 1
            || null === $config 
68 1
            || empty($typeId)
69 1
            || !isset($config['mappings'])
70 1
            || !isset($config['mappings'][$typeId])
71 1
        ;
72
    }
73
}
74