DoctrineDBALCleaner   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 46
ccs 18
cts 18
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A executeQueries() 0 11 3
A clear() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\Cleaner\DB;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DBALException;
9
use Lamoda\Cleaner\CleanerInterface;
10
use Lamoda\Cleaner\DB\Config\DBCleanerConfig;
11
use Lamoda\Cleaner\Exception\CleanerException;
12
13
class DoctrineDBALCleaner implements CleanerInterface
14
{
15
    /**
16
     * @var Connection
17
     */
18
    private $connection;
19
20
    /**
21
     * @var DBCleanerConfig
22
     */
23
    private $config;
24
25 5
    public function __construct(Connection $connection, DBCleanerConfig $config)
26
    {
27 5
        $this->connection = $connection;
28 5
        $this->config = $config;
29 5
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 5
    public function clear(): void
35
    {
36 5
        if ($this->config->isTransactional()) {
37
            $this->connection->transactional(function () {
38 2
                $this->executeQueries();
39 2
            });
40
        } else {
41 3
            $this->executeQueries();
42
        }
43 3
    }
44
45
    /**
46
     * @throws CleanerException
47
     */
48 5
    private function executeQueries(): void
49
    {
50 5
        foreach ($this->config->getQueries() as $query) {
51
            try {
52 5
                $this->connection->executeQuery(
53 5
                    $query->getQuery(),
54 5
                    $query->getParameters(),
55 5
                    $query->getTypes()
56
                );
57 2
            } catch (DBALException $e) {
58 4
                throw new CleanerException($e->getMessage(), $e->getCode(), $e);
59
            }
60
        }
61 3
    }
62
}
63