Completed
Push — master ( 560631...f58007 )
by
unknown
02:33
created

FeaturedFlags::_getStmt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace FeaturedFlags;
4
5
use PDO;
6
use Redis;
7
8
class FeaturedFlags
9
{
10
    protected $_pdo;
11
    protected $_redis;
12
13
    CONST TABLE_NAME = 'featured_flags';
14
15
    public function __construct(PDO $pdo, Redis $redis = null)
16
    {
17
        $this->_pdo = $pdo;
18
        if ($redis) {
19
            $this->_redis = $redis;
20
        }
21
    }
22
23
    /**
24
     * @param string $query
25
     * @param array $params
26
     * @return \mysqli_stmt
27
     */
28
    private function _getStmt($query, $params)
29
    {
30
        $stmt = $this->_pdo->prepare($query);
31
        foreach ($params as $field => $value) {
32
            $stmt->bindValue(":$field", $value);
33
        }
34
        return $stmt;
35
    }
36
37
    /**
38
     * @param string $query
39
     * @param array $params
40
     * @return array
41
     */
42
    private function _executeForSelect($query, $params = array())
43
    {
44
        $stmt = $this->_getStmt($query, $params);
45
        $stmt->execute();
46
        return $stmt->fetchAll();
47
    }
48
49
    /**
50
     * @param array $flagParams
51
     * @param array $filterParams
52
     * @return boolean
53
     */
54
    private function _compareParams($flagParams, $filterParams)
55
    {
56
        $checkParams = true;
57
        foreach($filterParams as $key => $value)
58
        {
59
            $checkParams = $checkParams && isset($flagParams[$key]) && $flagParams[$key] == $value;
60
        }
61
62
        return $checkParams;
63
    }
64
65
    /**
66
     * @param string $flagParamsJson
67
     * @param array $filterParams
68
     * @return boolean
69
     */
70
    private function _checkParams($flagParamsJson, $filterParams = null)
71
    {
72
        if (!$filterParams) {
73
            return true;
74
        }
75
76
        $flagParamsData = json_decode($flagParamsJson, true);
77
        if (!is_array($flagParamsData) || count($flagParamsData) === 0) {
78
            return false;
79
        }
80
81
        return $this->_compareParams($flagParamsData, $filterParams);
82
    }
83
84
    /**
85
     * @param $flagName
86
     * @param $filterParams
87
     * @return boolean
88
     */
89
    public function isEnabled($flagName, $filterParams = null)
90
    {
91
        $query = "SELECT * FROM ".self::TABLE_NAME." WHERE name = :flag";
92
        $params = array(
93
            'flag' => $flagName
94
        );
95
        $flagsData = $this->_executeForSelect($query, $params);
96
97
        foreach($flagsData as $flag){
98
            if (((boolean) $flag['status']) && $this->_checkParams($flag['params'], $filterParams))
99
            {
100
                return true;
101
            }
102
        }
103
104
        return false;
105
    }
106
107
108
    /**
109
     * @param $flag
110
     * @param $params
111
     * @return null|array
112
     */
113
    public function getEnabledValues($flag, $params = null)
0 ignored issues
show
Unused Code introduced by
The parameter $flag is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
    {
115
        return null;
116
    }
117
}
118