BaseService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 50
ccs 4
cts 10
cp 0.4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A computeEtag() 0 4 1
A encodeAttributes() 0 6 1
1
<?php
2
/**
3
 * BearerService.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\services\redis
13
 */
14
15
namespace sweelix\oauth2\server\services\redis;
16
17
use sweelix\oauth2\server\interfaces\BaseModelInterface;
18
use sweelix\oauth2\server\models\BaseModel;
19
use sweelix\oauth2\server\Module;
20
use sweelix\oauth2\server\traits\redis\TypeConverter;
21
use yii\base\BaseObject;
22
use yii\di\Instance;
23
use yii\helpers\Json;
24
use yii\redis\Connection;
25
use Yii;
26
27
/**
28
 * This is the base service for redis
29
 *
30
 * @author Philippe Gaultier <[email protected]>
31
 * @copyright 2010-2017 Philippe Gaultier
32
 * @license http://www.sweelix.net/license license
33
 * @version 1.2.0
34
 * @link http://www.sweelix.net
35
 * @package modules\v1\services\redis
36
 * @since 1.0.0
37
 */
38
class BaseService extends BaseObject
39
{
40
    use TypeConverter;
41
42
    /**
43
     * @var string namespace used for key generation
44
     */
45
    public $namespace = '';
46
47
    /**
48
     * @var Connection|array|string the Redis DB connection object or the application component ID of the DB connection.
49
     */
50
    protected $db;
51
52
    /**
53
     * @inheritdoc
54
     */
55 6
    public function init()
56
    {
57 6
        parent::init();
58 6
        $this->db = Instance::ensure(Module::getInstance()->db, Connection::class);
59 6
    }
60
61
    /**
62
     * Compute etag based on model attributes
63
     * @param BaseModelInterface $model
64
     * @return string
65
     * @since 1.0.0
66
     */
67
    protected function computeEtag(BaseModelInterface $model)
68
    {
69
        return $this->encodeAttributes($model->attributes);
0 ignored issues
show
Bug introduced by
Accessing attributes on the interface sweelix\oauth2\server\in...aces\BaseModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
70
    }
71
72
    /**
73
     * Encode attributes array
74
     *
75
     * @param array $attributes
76
     *
77
     * @return string
78
     * @since  1.0.0
79
     */
80
    protected function encodeAttributes(Array $attributes)
81
    {
82
        $data = Json::encode($attributes);
83
        $etag = '"' . rtrim(base64_encode(sha1($data, true)), '=') . '"';
84
        return $etag;
85
    }
86
87
}
88