ArrayParameter::getWhereInParameters()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 1
nop 1
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Foo\Pdo\Statement\Preprocessor;
6
7
use Foo\Pdo\Statement\IPreprocessor;
8
use Foo\Pdo\Statement\Preprocessor\ArrayParameter\Associative;
9
use Foo\Pdo\Statement\Preprocessor\ArrayParameter\Numeric;
10
11
class ArrayParameter implements IPreprocessor
12
{
13
    const PARAM_INT_ARRAY = 101;
14
    const PARAM_STR_ARRAY = 102;
15
16
    /** @var Numeric */
17
    protected $numeric;
18
19
    /** @var Associative */
20
    protected $assocative;
21
22
    /**
23
     * Preprocessor constructor.
24
     *
25
     * @param Numeric     $numeric
26
     * @param Associative $associative
27
     */
28 5
    public function __construct(Numeric $numeric, Associative $associative)
29
    {
30 5
        $this->numeric    = $numeric;
31 5
        $this->assocative = $associative;
32 5
    }
33
34
    /**
35
     * @param string $query
36
     * @param array  $parameters
37
     */
38 5
    public function process(string &$query, array &$parameters)
39
    {
40 5
        $whereInParameters = $this->getWhereInParameters($parameters);
41 5
        if (empty($whereInParameters)) {
42 4
            return;
43
        }
44
45 1
        $this->numeric->process($query, $parameters, $whereInParameters);
46 1
        $this->assocative->process($query, $parameters, $whereInParameters);
47 1
    }
48
49
    /**
50
     * @param array $parameters
51
     *
52
     * @return array
53
     */
54 5
    private function getWhereInParameters(array $parameters)
55
    {
56 5
        $whereInParameters = array_filter(
57
            $parameters,
58 5
            function ($parameter) {
59 5
                if (!is_array($parameter) || !array_key_exists(1, $parameter)) {
60 3
                    return false;
61
                }
62
63 2
                return in_array($parameter[1], [static::PARAM_INT_ARRAY, static::PARAM_STR_ARRAY], true);
64 5
            }
65
        );
66
67 5
        return $whereInParameters;
68
    }
69
}
70