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

ColumnSanitizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 91
rs 10
c 0
b 0
f 0
ccs 17
cts 17
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 4 1
A getAllowedColumns() 0 18 2
A getKey() 0 4 1
A extractAllowedColumns() 0 10 1
A getSpecialColumns() 0 4 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
    /**
36
     * Holds already processed statement column data.
37
     *
38
     * @var array
39
     */
40
    protected $queryCache = [];
41
42
    /**
43
     * Sanitizes and returns row data for given statement.
44
     *
45
     * @param array  $row       The current row data
46
     * @param string $statement The SQL statement to sanitize for
47
     *
48
     * @return array The sanitized row data
49
     */
50 6
    public function execute(array $row, string $statement) : array
51
    {
52 6
        return array_intersect_key($row, $this->getAllowedColumns($statement));
53
    }
54
55
    /**
56
     * Determines allowed columns for current statement.
57
     *
58
     * @param  string $statement The statement to determine the allowed columns for
59
     *
60
     * @return array The allowed columns for this statement
61
     */
62 6
    protected function getAllowedColumns(string $statement): array
63
    {
64
65
        // load the cache key for the statement
66 6
        $queryCacheKey = $this->getKey($statement);
67
68
        // query whether or not the query has already been processed
69 6
        if (!isset($this->queryCache[$queryCacheKey])) {
70
            // determine the allowed columns
71 6
            $statementColumns = $this->extractAllowedColumns($statement);
72 6
            $allowedColumns = array_merge($statementColumns, $this->getSpecialColumns());
73
            // register the array with the columns in the query cache
74 6
            $this->queryCache[$queryCacheKey] = array_combine($allowedColumns, $allowedColumns);
75
        }
76
77
        // return the array with the allowed columns
78 6
        return $this->queryCache[$queryCacheKey];
79
    }
80
81
    /**
82
     * Calculate internal cache key.
83
     *
84
     * crc32 is used as performance is more important than cryptographic safety.
85
     *
86
     * @param string $statement The statement to create the key for
87
     *
88
     * @return int
89
     */
90 6
    protected function getKey(string $statement) : int
91
    {
92 6
        return crc32($statement);
93
    }
94
95
    /**
96
     * Extract allowed columns from statement.
97
     *
98
     * @param string $sql The SQL to extract the allowed columns from
99
     *
100
     * @return array The array with the allowed columns
101
     */
102 6
    protected function extractAllowedColumns(string $sql)
103
    {
104
105
        // initialize the array for the matches and invoke the PCRE regex
106 6
        $matches = [];
107 6
        preg_match_all('/:([^,\n )]*)/', $sql, $matches);
108
109
        // return the found columns
110 6
        return $matches[1];
111
    }
112
113
    /**
114
     * Return special column names, that are necessary additionally to statement columns.
115
     *
116
     * @return array Return an array with special columns, e. g. the entity status
117
     */
118 6
    protected function getSpecialColumns()
119
    {
120 6
        return [EntityStatus::MEMBER_NAME];
121
    }
122
}
123