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

CouchDbMigrationTrait::deleteDatabase()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 0
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