Completed
Push — master ( b9a79e...b69f9e )
by André
52:20 queued 36:03
created

DbBasedInstaller::getKernelSQLFileForDBMS()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace EzSystems\PlatformInstallerBundle\Installer;
10
11
use Doctrine\DBAL\Connection;
12
use Symfony\Component\Filesystem\Filesystem;
13
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
14
15
class DbBasedInstaller
16
{
17
    /** @var Connection */
18
    protected $db;
19
20
    /** @var \Symfony\Component\Console\Output\OutputInterface */
21
    protected $output;
22
23
    /** @var string */
24
    protected $baseDataDir;
25
26
    public function __construct(Connection $db)
27
    {
28
        $this->db = $db;
29
        // parametrized so other installer implementations can override this
30
        $this->baseDataDir = __DIR__ . '/../../../../../data';
31
    }
32
33
    /**
34
     * @param \Symfony\Component\Console\Output\OutputInterface $output
35
     */
36
    public function setOutput($output)
37
    {
38
        $this->output = $output;
39
    }
40
41
    /**
42
     * Copy and override configuration file.
43
     *
44
     * @param string $source
45
     * @param string $target
46
     */
47
    protected function copyConfigurationFile($source, $target)
48
    {
49
        $fs = new Filesystem();
50
        $fs->copy($source, $target, true);
51
52
        if (!$this->output->isQuiet()) {
53
            $this->output->writeln("Copied $source to $target");
54
        }
55
    }
56
57
    protected function runQueriesFromFile($file)
58
    {
59
        $queries = array_filter(preg_split('(;\\s*$)m', file_get_contents($file)));
60
61
        if (!$this->output->isQuiet()) {
62
            $this->output->writeln(
63
                sprintf(
64
                    'Executing %d queries from %s on database %s',
65
                    count($queries),
66
                    $file,
67
                    $this->db->getDatabase()
68
                )
69
            );
70
        }
71
72
        foreach ($queries as $query) {
73
            $this->db->exec($query);
74
        }
75
    }
76
77
    /**
78
     * Get DBMS-specific SQL data file path.
79
     *
80
     * @param string $relativeFilePath SQL file path relative to {baseDir}/{dbms} directory
81
     *
82
     * @return string absolute existing file path
83
     *
84
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
85
     *
86
     * @since 6.13
87
     */
88
    final protected function getKernelSQLFileForDBMS($relativeFilePath)
89
    {
90
        $databasePlatform = $this->db->getDatabasePlatform()->getName();
91
        $filePath = "{$this->baseDataDir}/{$databasePlatform}/{$relativeFilePath}";
92
93
        if (!is_readable($filePath)) {
94
            throw new InvalidArgumentException(
95
                '$relativeFilePath',
96
                sprintf(
97
                    'DBMS-specific file for %s database platform does not exist or is not readable: %s',
98
                    $databasePlatform,
99
                    $filePath
100
                )
101
            );
102
        }
103
104
        // apply realpath for more user-friendly Console output
105
        return realpath($filePath);
106
    }
107
}
108