Completed
Pull Request — 16.x (#176)
by
unknown
02:18
created

ColumnSanitizer::extractAllowedColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
    public function execute(array $row, \PDOStatement $statement)
45
    {
46
        $allowedColumns = $this->getAllowedColumns($statement);
47
48
        return array_intersect_key($row, $allowedColumns);
49
    }
50
51
    /**
52
     * Determines allowed columns for current statement.
53
     *
54
     * @param \PDOStatement $statement
55
     * @return array The allowed columns for this statement
56
     */
57
    protected function getAllowedColumns(\PDOStatement $statement): array
58
    {
59
        $sql = $statement->queryString;
60
        $queryCacheKey = $this->getKey($sql);
61
        if (!isset($this->queryCache[$queryCacheKey])) {
62
63
            $statementColumns = $this->extractAllowedColumns($sql);
64
            $allowedColumns = array_merge($statementColumns, $this->getProtectedColumns());
65
66
            $this->queryCache[$queryCacheKey] = array_combine($allowedColumns, $allowedColumns);
67
        }
68
69
        return $this->queryCache[$queryCacheKey];
70
    }
71
72
    /**
73
     * Calculate internal cache key.
74
     *
75
     * crc32 is used as performance is more important than cryptographic safety.
76
     *
77
     * @param string $statement
78
     * @return int
79
     */
80
    protected function getKey(string $statement)
81
    {
82
        return crc32($statement);
83
    }
84
85
    /**
86
     * Extract allowed columns from statement
87
     *
88
     * @param string $sql
89
     * @return array
90
     */
91
    protected function extractAllowedColumns(string $sql)
92
    {
93
        $matches = [];
94
        preg_match_all('/:([^,\n )]*)/', $sql, $matches);
95
96
        return $matches[1];
97
    }
98
99
    /**
100
     * Return special column names, that are necessary additionally to statement columns.
101
     *
102
     * @return array
103
     */
104
    protected function getProtectedColumns()
105
    {
106
        return [EntityStatus::MEMBER_NAME];
107
    }
108
}
109