Passed
Push — master ( fdb26f...810f98 )
by Christian
23:44 queued 11s
created

BlueGreenDeploymentService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkIfMayCreateTrigger() 0 12 2
A __construct() 0 3 1
A getTriggerQuery() 0 3 1
A getCreateTableQuery() 0 3 1
A setEnvironmentVariable() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Recovery\Install\Service;
4
5
use Doctrine\DBAL\Connection;
6
7
class BlueGreenDeploymentService
8
{
9
    public const ENV_NAME = 'BLUE_GREEN_DEPLOYMENT';
10
11
    /**
12
     * @var Connection
13
     */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    public function setEnvironmentVariable(): void
22
    {
23
        $_ENV[self::ENV_NAME] = $_SESSION[self::ENV_NAME] = $this->checkIfMayCreateTrigger();
24
    }
25
26
    private function checkIfMayCreateTrigger(): bool
27
    {
28
        try {
29
            $this->connection->executeQuery($this->getCreateTableQuery());
30
            $this->connection->executeQuery($this->getTriggerQuery());
31
        } catch (\Doctrine\DBAL\DBALException $exception) {
32
            return false;
33
        } finally {
34
            $this->connection->executeQuery('DROP TABLE IF EXISTS example');
35
        }
36
37
        return true;
38
    }
39
40
    private function getCreateTableQuery(): string
41
    {
42
        return <<<SQL
43
            CREATE TABLE IF NOT EXISTS `example` (
44
              `id` int NOT NULL
45
            );
46
SQL;
47
    }
48
49
    private function getTriggerQuery(): string
50
    {
51
        return <<<SQL
52
            CREATE TRIGGER example_trigger BEFORE UPDATE ON `example`
53
                FOR EACH ROW
54
                BEGIN
55
                END;
56
SQL;
57
    }
58
}
59