Completed
Push — 15.x ( 39dd4d...e89609 )
by Tim
02:40
created

ColumnPlaceholdersUtil   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 65
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getColumnPlaceholders() 0 14 1
A compile() 0 6 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Utils\ColumnPlaceholdersUtil
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 2020 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\Loaders\LoaderInterface;
24
25
/**
26
 * Utility class for dynamic column placeholder handling.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2020 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 ColumnPlaceholdersUtil implements ColumnPlaceholdersUtiInterface
35
{
36
37
    /**
38
     * The column name loader instance.
39
     *
40
     * @var \TechDivision\Import\Loaders\LoaderInterface
41
     */
42
    protected $columnNameLoader;
43
44
    /**
45
     * The table prefix utility instance.
46
     *
47
     * @var \TechDivision\Import\Utils\TablePrefixUtilInterface
48
     */
49
    protected $tablePrefixUtil;
50
51
    /**
52
     * Construct a new instance.
53
     *
54
     * @param \TechDivision\Import\Loaders\LoaderInterface        $columnNameLoader The column name loader instance
55
     * @param \TechDivision\Import\Utils\TablePrefixUtilInterface $tablePrefixUtil  The table prefix utility instance
56
     */
57 1
    public function __construct(LoaderInterface $columnNameLoader, TablePrefixUtilInterface $tablePrefixUtil)
58
    {
59 1
        $this->columnNameLoader = $columnNameLoader;
60 1
        $this->tablePrefixUtil = $tablePrefixUtil;
61 1
    }
62
63
    /**
64
     * Returns a concatenated list with column names of the passed table.
65
     *
66
     * @param string $tableName The table name to return the list for
67
     *
68
     * @return string The concatenated list of column names
69
     */
70 1
    public function getColumnPlaceholders($tableName)
71
    {
72
73
        // load the column names from the loader
74 1
        $columnNames = $this->columnNameLoader->load($this->tablePrefixUtil->getPrefixedTableName($tableName));
0 ignored issues
show
Unused Code introduced by
The call to LoaderInterface::load() has too many arguments starting with $this->tablePrefixUtil->...edTableName($tableName).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
76
        // add the double colon (:) for the placeholder
77 1
        array_walk($columnNames, function (&$value) {
78 1
            $value = sprintf(':%s', $value);
79 1
        });
80
81
        // implode and return the column names
82 1
        return implode(',', $columnNames);
83
    }
84
85
    /**
86
     * Compiles the passed SQL statement.
87
     *
88
     * @param string $statement The SQL statement to compile
89
     *
90
     * @return string The compiled SQL statement
91
     */
92
    public function compile($statement)
93
    {
94 1
        return preg_replace_callback(sprintf('/\$\{%s:(.*)\}/U', ColumnPlaceholdersUtiInterface::TOKEN), function (array $matches) {
95 1
            return $this->getColumnPlaceholders($matches[1]);
96 1
        }, $statement);
97
    }
98
}
99