TypeConverter   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 73.21%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 1
dl 0
loc 90
c 0
b 0
f 0
ccs 41
cts 56
cp 0.7321
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttributesDefinitions() 0 4 1
C convertToDatabase() 0 29 13
C convertToModel() 0 31 12
1
<?php
2
/**
3
 * TypeConverter.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.2.0
11
 * @link http://www.sweelix.net
12
 * @package sweelix\oauth2\server\traits\redis
13
 */
14
15
namespace sweelix\oauth2\server\traits\redis;
16
17
use yii\base\InvalidArgumentException;
18
use yii\helpers\Json;
19
20
/**
21
 * This trait convert data from the db to match original types
22
 *
23
 * @author Philippe Gaultier <[email protected]>
24
 * @copyright 2010-2017 Philippe Gaultier
25
 * @license http://www.sweelix.net/license license
26
 * @version 1.2.0
27
 * @link http://www.sweelix.net
28
 * @package modules\v1\traits\redis
29
 * @since 1.0.0
30
 */
31
trait TypeConverter
32
{
33
    /**
34
     * @var array attributes definitions declared in model
35
     */
36
    private $attributesDefinitions;
37
38
    /**
39
     * @param array $attributesDefinitions attributes definitions declared in the model
40
     * @since 1.0.0
41
     */
42 49
    public function setAttributesDefinitions($attributesDefinitions)
43
    {
44 49
        $this->attributesDefinitions = $attributesDefinitions;
45 49
    }
46
47
    /**
48
     * @param string $key attribute name
49
     * @param mixed $value attribute value
50
     * @return mixed type compliant with redis
51
     * @since 1.0.0
52
     */
53 49
    public function convertToDatabase($key, $value)
54
    {
55 49
        $bypassTypes = ['string'];
56 49
        if ((isset($this->attributesDefinitions[$key]) === true)
57 49
            && (in_array($this->attributesDefinitions[$key], $bypassTypes) === false)
58 49
        ) {
59 37
            switch($this->attributesDefinitions[$key]) {
60 37
                case 'bool':
61 37
                case 'boolean':
62 32
                    $value = $value ? 1 : 0;
63 32
                    break;
64 33
                case 'int':
65 33
                case 'integer':
66
                    break;
67 33
                case 'array':
68 33
                    if (is_array($value) === false) {
69
                        $value = [];
70
                    }
71 33
                    $value = Json::encode($value);
72 33
                    break;
73
                case 'real':
74
                case 'double':
75
                case 'float':
76
                    break;
77 37
            }
78
79 37
        }
80 49
        return $value;
81
    }
82
83
    /**
84
     * @param string $key attribute name
85
     * @param string $value attribute value
86
     * @return mixed value in original datatype
87
     * @since 1.0.0
88
     */
89 42
    public function convertToModel($key, $value)
90
    {
91 42
        $bypassTypes = ['string'];
92 42
        if ((isset($this->attributesDefinitions[$key]) === true)
93 42
            && (in_array($this->attributesDefinitions[$key], $bypassTypes) === false)
94 42
        ) {
95 31
            switch ($this->attributesDefinitions[$key]) {
96 31
                case 'bool':
97 31
                case 'boolean':
98 20
                    $value = (bool) $value;
99 20
                    break;
100 29
                case 'int':
101 29
                case 'integer':
102
                    $value = (int) $value;
103
                    break;
104 29
                case 'array':
105
                    try {
106 29
                        $value = Json::decode($value);
107 29
                    } catch (InvalidArgumentException $e) {
108
                        $value = [];
109
                    }
110 29
                    break;
111
                case 'real':
112
                case 'double':
113
                case 'float':
114
                    $value = (float) $value;
115
                    break;
116 31
            }
117 31
        }
118 42
        return $value;
119
    }
120
}
121