Completed
Push — master ( d72a06...ba0ad0 )
by GBProd
04:38
created

CreateIndexHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
lcom 1
cbo 0
dl 68
loc 68
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testHandle() 21 21 1
A newIndices() 7 7 1
A newClient() 13 13 1
A newConfigRepository() 21 21 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace GBProd\Tests\Units\ElasticsearchExtraBundle\Handler;
4
5
use atoum;
6
use mock\Elasticsearch\Client;
7
use mock\Elasticsearch\Namespaces\IndicesNamespace;
8
use mock\GBProd\ElasticsearchExtraBundle\Repository\ClientRepository;
9
use mock\GBProd\ElasticsearchExtraBundle\Repository\IndexConfigurationRepository;
10
11
/**
12
 * Tests for CreateIndexHandler
13
 *
14
 * @author gbprod <[email protected]>
15
 */
16 View Code Duplication
class CreateIndexHandler extends atoum
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    public function testHandle()
19
    {
20
        $this
21
            ->given($config = ['my' => ['awesome' => 'config']])
22
                ->and($indices = $this->newIndices())
23
                ->and($client = $this->newClient($indices))
24
                ->and($configRepository = $this->newConfigRepository('my_index', $config))
25
                ->and($this->newTestedInstance($configRepository))
26
            ->if($this->testedInstance->handle($client, 'my_index'))
27
            ->then
28
                ->mock($indices)
29
                    ->call('create')
30
                        ->withArguments(
31
                            [
32
                                'index' => 'my_index',
33
                                'body'  => $config,
34
                            ]
35
                        )
36
                        ->once()
37
        ;
38
    }
39
40
    private function newIndices()
41
    {
42
        $this->mockGenerator->shuntParentClassCalls();
43
        $this->mockGenerator->orphanize('__construct');
44
45
        return new IndicesNamespace();
46
    }
47
48
    private function newClient($indices)
49
    {
50
        $this->mockGenerator->shuntParentClassCalls();
51
        $this->mockGenerator->orphanize('__construct');
52
53
        $client = new Client();
54
55
        $this->calling($client)->indices = function() use ($indices) {
56
            return $indices;
57
        };
58
59
        return $client;
60
    }
61
62
    private function newConfigRepository($index, $config)
63
    {
64
        $this->mockGenerator->shuntParentClassCalls();
65
        $this->mockGenerator->orphanize('__construct');
66
67
        $configRepository = new IndexConfigurationRepository();
68
69
        $this->calling($configRepository)->get =
70
            function($indexParam) use ($index, $config) {
71
                if ($index == $indexParam) {
72
                    return $config;
73
                }
74
75
                return null;
76
            }
77
        ;
78
79
        $this->mockGenerator->unshuntParentClassCalls();
80
81
        return $configRepository;
82
    }
83
}
84