Completed
Push — master ( b5c840...3d5461 )
by Marcus
04:46
created

TablePrefixUtil::getPrefixedTableName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 4
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Utils\TablePrefixUtil
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Utils;
22
23
use TechDivision\Import\Configuration\ConfigurationInterface;
24
25
/**
26
 * Utility class for table prefix handling.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class TablePrefixUtil implements TablePrefixUtilInterface
35
{
36
37
    /**
38
     * The configuration instance.
39
     *
40
     * @var \TechDivision\Import\Configuration\ConfigurationInterface
41
     */
42
    protected $configuration;
43
44
    /**
45
     * Construct a new instance.
46
     *
47
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
48
     */
49
    public function __construct(ConfigurationInterface $configuration)
50
    {
51
        $this->configuration = $configuration;
52
    }
53
54
    /**
55
     * Returns the prefixed table name.
56
     *
57
     * @param string $tableName The table name to prefix
58
     *
59
     * @return string The prefixed table name
60
     * @throws \Exception Is thrown if the table name can't be prefixed
61
     */
62
    public function getPrefixedTableName($tableName)
63
    {
64
65
        // try to load the table prefix from the configuration
66
        if ($tablePrefix = $this->configuration->getDatabase()->getTablePrefix()) {
67
            $tableName = sprintf('%s%s', $tablePrefix, $tableName);
68
        }
69
70
        // return the prefixed table name
71
        return $tableName;
72
    }
73
74
    /**
75
     * Compiles the passed SQL statement.
76
     *
77
     * @param string $statement The SQL statement to compile
78
     *
79
     * @return string The compiled SQL statement
80
     */
81
    public function compile($statement)
82
    {
83 2
        return preg_replace_callback(sprintf('/\$\{%s:(.*)\}/U', TablePrefixUtilInterface::TOKEN), function (array $matches) {
84 2
            return $this->getPrefixedTableName($matches[1]);
85 2
        }, $statement);
86
    }
87
}
88