Test Failed
Push — master ( b78772...2b811b )
by Mr
03:11
created

CouchDbMigrationTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 36.62 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 26
loc 71
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createDatabase() 13 13 4
A deleteDatabase() 13 13 4
A createDesignDoc() 0 17 2
A deleteDesignDoc() 0 14 2
A getDatabaseName() 0 5 1

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 Daikon\CouchDb\Migration;
4
5
use Daikon\Dbal\Exception\MigrationException;
6
use Daikon\Dbal\Migration\MigrationTrait;
7
use GuzzleHttp\Exception\RequestException;
8
9
trait CouchDbMigrationTrait
10
{
11
    use MigrationTrait;
12
13 View Code Duplication
    private function createDatabase(): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method 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...
14
    {
15
        $client = $this->connector->getConnection();
16
        $databaseName = $this->getDatabaseName();
17
18
        try {
19
            $client->put('/'.$databaseName);
20
        } catch (RequestException $error) {
21
            if (!$error->hasResponse() || !$error->getResponse()->getStatusCode() === 409) {
22
                throw new MigrationException($error->getMessage());
23
            }
24
        }
25
    }
26
27 View Code Duplication
    private function deleteDatabase(): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method 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...
28
    {
29
        $client = $this->connector->getConnection();
30
        $databaseName = $this->getDatabaseName();
31
32
        try {
33
            $client->delete('/'.$databaseName);
34
        } catch (RequestException $error) {
35
            if (!$error->hasResponse() || !$error->getResponse()->getStatusCode() === 404) {
36
                throw new MigrationException($error->getMessage());
37
            }
38
        }
39
    }
40
41
    private function createDesignDoc(string $name, array $views): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
42
    {
43
        $client = $this->connector->getConnection();
44
        $databaseName = $this->getDatabaseName();
45
46
        $body = [
47
            'language' => 'javascript',
48
            'views' => $views
49
        ];
50
51
        try {
52
            $requestPath = sprintf('/%s/_design/%s', $databaseName, $name);
53
            $client->put($requestPath, ['body' => json_encode($body)]);
54
        } catch (RequestException $error) {
55
            throw new MigrationException($error->getMessage());
56
        }
57
    }
58
59
    private function deleteDesignDoc(string $name): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
60
    {
61
        $client = $this->connector->getConnection();
62
        $databaseName = $this->getDatabaseName();
63
64
        try {
65
            $requestPath = sprintf('/%s/_design/%s', $databaseName, $name);
66
            $response = $client->head($requestPath);
67
            $revision = trim(current($response->getHeader('ETag')), '"');
68
            $client->delete(sprintf('%s?rev=%s', $requestPath, $revision));
69
        } catch (RequestException $error) {
70
            throw new MigrationException($error->getMessage());
71
        }
72
    }
73
74
    private function getDatabaseName(): string
75
    {
76
        $connectorSettings = $this->connector->getSettings();
77
        return $connectorSettings['database'];
78
    }
79
}
80