BaseService::computeEtag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 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\mySql
13
 */
14
15
namespace sweelix\oauth2\server\services\mySql;
16
17
use sweelix\oauth2\server\interfaces\BaseModelInterface;
18
use sweelix\oauth2\server\Module;
19
use sweelix\oauth2\server\traits\mySql\TypeConverter;
20
use yii\base\BaseObject;
21
use yii\di\Instance;
22
use yii\helpers\Json;
23
use yii\db\Connection;
24
25
/**
26
 * This is the base service for mySql
27
 *
28
 * @author Philippe Gaultier <[email protected]>
29
 * @copyright 2010-2017 Philippe Gaultier
30
 * @license http://www.sweelix.net/license license
31
 * @version 1.2.0
32
 * @link http://www.sweelix.net
33
 * @package modules\v1\services\mySql
34
 * @since 1.0.0
35
 */
36
class BaseService extends BaseObject
37
{
38
    use TypeConverter;
39
40
    /**
41
     * @var string namespace used for key generation
42
     */
43
    public $namespace = '';
44
45
    /**
46
     * @var Connection|array|string the MySql DB connection object or the application component ID of the DB connection.
47
     */
48
    protected $db;
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function init()
54
    {
55
        parent::init();
56
        $this->db = Instance::ensure(Module::getInstance()->db, Connection::class);
57
    }
58
59
    /**
60
     * Compute etag based on model attributes
61
     * @param BaseModelInterface $model
62
     * @return string
63
     * @since 1.0.0
64
     */
65
    protected function computeEtag(BaseModelInterface $model)
66
    {
67
        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...
68
    }
69
70
    /**
71
     * Encode attributes array
72
     *
73
     * @param array $attributes
74
     *
75
     * @return string
76
     * @since  1.0.0
77
     */
78
    protected function encodeAttributes(Array $attributes)
79
    {
80
        $data = Json::encode($attributes);
81
        $etag = '"' . rtrim(base64_encode(sha1($data, true)), '=') . '"';
82
        return $etag;
83
    }
84
85
}
86