Tracker::increment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Tequilarapido\TrackIt;
4
5
use Tequilarapido\TrackIt\Store\Store;
6
7
abstract class Tracker
8
{
9
    protected $uid = 'uid';
10
11
    protected $prefix = 'prefix';
12
13
    /** @var Store */
14
    protected $store;
15
16 8
    public function setUid($uid)
17
    {
18 8
        $this->uid = $uid;
19
20 8
        return $this;
21
    }
22
23 8
    public function setPrefix($prefix)
24
    {
25 8
        $this->prefix = $prefix;
26
27 8
        return $this;
28
    }
29
30 7
    protected function getFullKey($key)
31
    {
32 7
        return "{$this->prefix}:{$this->uid}:{$key}";
33
    }
34
35 6
    public function get($key)
36
    {
37 6
        return $this->store()->get($this->getFullKey($key));
38
    }
39
40 7
    public function set($key, $value)
41
    {
42 7
        $this->store()->set($this->getFullKey($key), $value);
43 7
    }
44
45 2
    public function jsonGet($key)
46
    {
47 2
        return json_decode($this->get($key), true);
48
    }
49
50 2
    public function jsonSet($key, $value)
51
    {
52 2
        $this->set($key, json_encode($value));
53 2
    }
54
55 3
    public function increment($key, $increment = 1)
56
    {
57 3
        $this->store()->increment($this->getFullKey($key), $increment);
58 3
    }
59
60 1
    public function exists($key)
61
    {
62 1
        return $this->store()->exists($key);
63
    }
64
65
    /** @return Store */
66 7
    public function store()
67
    {
68 7
        if (! $this->store) {
69 7
            $this->store = app()->make(Store::class);
70
        }
71
72 7
        return $this->store;
73
    }
74
75 3
    public function __call($method, $parameters)
76
    {
77
        // getSomething() -> get(const::SOMETHING)
78 3
        if (starts_with($method, 'get')) {
79 1
            return $this->dynamicGet($method);
80
        }
81
82
        // setSomething(value)
83 3
        if (starts_with($method, 'set')) {
84 2
            $this->dynamicSet($method, $parameters[0]);
85
86 1
            return;
87
        }
88
89
        // incrementSomething(value)
90 2
        if (starts_with($method, 'increment')) {
91 1
            $this->dynamicIncrement($method, $parameters);
92
93 1
            return;
94
        }
95
96
        // jsonGetSomething()
97 1
        if (starts_with($method, 'jsonGet')) {
98 1
            return $this->dynamicJsonGet($method);
99
        }
100
101
        // jsonSetSomething(value)
102 1
        if (starts_with($method, 'jsonSet')) {
103 1
            $this->dynamicJsonSet($method, $parameters[0]);
104
105 1
            return;
106
        }
107
108
        throw new UndefinedTrackerConstant("Uknown method called on tracker. [$method]");
109
    }
110
111 1 View Code Duplication
    protected function dynamicGet($method)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
    {
113 1
        $key = strtoupper(str_after($method, 'get'));
114 1
        if (! defined("static::{$key}")) {
115
            throw new UndefinedTrackerConstant("Tracker: Undefined key constant [{$key}]");
116
        }
117
118 1
        return $this->get(constant("static::{$key}"));
119
    }
120
121 2 View Code Duplication
    protected function dynamicSet($method, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
    {
123 2
        $key = strtoupper(str_after($method, 'set'));
124 2
        if (! defined("static::{$key}")) {
125 1
            throw new UndefinedTrackerConstant("Tracker: Undefined key constant [{$key}]");
126
        }
127
128 1
        $this->set(constant("static::{$key}"), $value);
129 1
    }
130
131 1 View Code Duplication
    protected function dynamicJsonGet($method)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133 1
        $key = strtoupper(str_after($method, 'jsonGet'));
134 1
        if (! defined("static::{$key}")) {
135
            throw new UndefinedTrackerConstant("Tracker: Undefined key constant [{$key}]");
136
        }
137
138 1
        return $this->jsonGet(constant("static::{$key}"));
139
    }
140
141 1 View Code Duplication
    protected function dynamicJsonSet($method, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143 1
        $key = strtoupper(str_after($method, 'jsonSet'));
144 1
        if (! defined("static::{$key}")) {
145
            throw new UndefinedTrackerConstant("Tracker: Undefined key constant [{$key}]");
146
        }
147
148 1
        $this->jsonSet(constant("static::{$key}"), $value);
149 1
    }
150
151 1
    protected function dynamicIncrement($method, $parameters)
152
    {
153 1
        $key = strtoupper(str_after($method, 'increment'));
154 1
        if (! defined("static::{$key}")) {
155
            throw new UndefinedTrackerConstant("Tracker: Undefined key constant [{$key}]");
156
        }
157
158 1
        if (isset($parameters[0])) {
159 1
            $this->increment(constant("static::{$key}"), $parameters[0]);
160
        } else {
161 1
            $this->increment(constant("static::{$key}"));
162
        }
163 1
    }
164
}
165