Completed
Push — 16.x ( 78d55e...ce5281 )
by Tim
02:16 queued 11s
created

ColumnSanitizer::getSpecialColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Utils\ColumnSanitizer
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 7
13
 *
14
 * @author    Team CSI <[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
/**
24
 * Utility class for statement query sanitizing.
25
 *
26
 * @author    Team CSI <[email protected]>
27
 * @copyright 2020 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class ColumnSanitizer implements SanitizerInterface
33
{
34
    /**
35
     * Holds already processed statement column data.
36
     *
37
     * @var array
38
     */
39
    protected $queryCache = [];
40
41
    /**
42
     * @inheritDoc
43
     */
44 6
    public function execute(array $row, string $statement)
45
    {
46 6
        $allowedColumns = $this->getAllowedColumns($statement);
47
48 6
        return array_intersect_key($row, $allowedColumns);
49
    }
50
51
    /**
52
     * Determines allowed columns for current statement.
53
     *
54
     * @param string $statement
55
     * @return array The allowed columns for this statement
56
     */
57 6
    protected function getAllowedColumns(string $statement): array
58
    {
59 6
        $queryCacheKey = $this->getKey($statement);
60 6
        if (!isset($this->queryCache[$queryCacheKey])) {
61
62 6
            $statementColumns = $this->extractAllowedColumns($statement);
63 6
            $allowedColumns = array_merge($statementColumns, $this->getSpecialColumns());
64
65 6
            $this->queryCache[$queryCacheKey] = array_combine($allowedColumns, $allowedColumns);
66
        }
67
68 6
        return $this->queryCache[$queryCacheKey];
69
    }
70
71
    /**
72
     * Calculate internal cache key.
73
     *
74
     * crc32 is used as performance is more important than cryptographic safety.
75
     *
76
     * @param string $statement
77
     * @return int
78
     */
79 6
    protected function getKey(string $statement)
80
    {
81 6
        return crc32($statement);
82
    }
83
84
    /**
85
     * Extract allowed columns from statement
86
     *
87
     * @param string $sql
88
     * @return array
89
     */
90 6
    protected function extractAllowedColumns(string $sql)
91
    {
92 6
        $matches = [];
93 6
        preg_match_all('/:([^,\n )]*)/', $sql, $matches);
94
95 6
        return $matches[1];
96
    }
97
98
    /**
99
     * Return special column names, that are necessary additionally to statement columns.
100
     *
101
     * @return array
102
     */
103 6
    protected function getSpecialColumns()
104
    {
105 6
        return [EntityStatus::MEMBER_NAME];
106
    }
107
}
108