ArrayParameter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 59
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 10 2
A getWhereInParameters() 0 15 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