DoctrineDBALCleaner::clear()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
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