Passed
Push — master ( b6440b...bfbe07 )
by
unknown
02:23
created

Visits::increment()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 8
nc 7
nop 3
dl 0
loc 14
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace awssat\Visits;
4
5
use awssat\Visits\Traits\Lists;
6
use awssat\Visits\Traits\Periods;
7
use awssat\Visits\Traits\Record;
8
use awssat\Visits\Traits\Setters;
9
use Illuminate\Support\Carbon;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\Facades\Redis;
13
use Jaybizzle\CrawlerDetect\CrawlerDetect;
14
15
class Visits
16
{
17
    use Record, Lists, Periods, Setters;
0 ignored issues
show
introduced by
The trait awssat\Visits\Traits\Lists requires some properties which are not provided by awssat\Visits\Visits: $id, $primary
Loading history...
introduced by
The trait awssat\Visits\Traits\Record requires some properties which are not provided by awssat\Visits\Visits: $iso_code, $id
Loading history...
18
19
    /**
20
     * @var mixed
21
     */
22
    protected $ipSeconds;
23
    /**
24
     * @var null
25
     */
26
    protected $subject;
27
    /**
28
     * @var bool|mixed
29
     */
30
    protected $fresh = false;
31
    /**
32
     * @var null|string
33
     */
34
    protected $country = null;
35
    /**
36
     * @var null|string
37
     */
38
    protected $referer = null;
39
    /**
40
     * @var null|string
41
     */
42
    protected $operatingSystem = null;
43
    /**
44
     * @var null|string
45
     */
46
    protected $language = null;
47
    /**
48
     * @var mixed
49
     */
50
    protected $periods;
51
    /**
52
     * @var Keys
53
     */
54
    protected $keys;
55
    /**
56
     * @var Redis
57
     */
58
    public $redis;
59
    /**
60
     * @var boolean
61
     */
62
    public $ignoreCrawlers = false;
63
    /**
64
     * @var array
65
     */
66
    public $globalIgnore = [];
67
68
    /**
69
     * Visits constructor.
70
     * @param $subject
71
     * @param string $tag|null
72
     */
73
    public function __construct($subject = null, $tag = 'visits')
74
    {
75
        $config = config('visits');
76
        $this->redis = Redis::connection($config['connection']);
0 ignored issues
show
Documentation Bug introduced by
It seems like Illuminate\Support\Facad...($config['connection']) of type Illuminate\Redis\Connections\Connection is incompatible with the declared type Illuminate\Support\Facades\Redis of property $redis.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
        $this->periods = $config['periods'];
78
        $this->ipSeconds = $config['remember_ip'];
79
        $this->fresh = $config['always_fresh'];
80
        $this->ignoreCrawlers = $config['ignore_crawlers'];
81
        $this->globalIgnore = $config['global_ignore'];
82
        $this->subject = $subject;
83
        $this->keys = new Keys($subject, $tag);
84
85
        $this->periodsSync();
86
    }
87
88
    /**
89
     * @param $subject
90
     * @return $this
91
     */
92
    public function by($subject)
93
    {
94
        if($subject instanceof Model) {
95
            $this->keys->append($this->keys->modelName($subject), $subject->{$subject->getKeyName()});
96
        } else if (is_array($subject)) {
97
            $this->keys->append(array_keys($subject)[0], Arr::first($subject));
98
        }
99
100
        return $this;
101
    }
102
103
    /**
104
     * Reset methods
105
     *
106
     * @param $method
107
     * @param string $args
108
     * @return Reset
109
     */
110
    public function reset($method = 'visits', $args = '')
111
    {
112
        return new Reset($this, $method, $args);
113
    }
114
115
    /**
116
     * Check for the ip is has been recorded before
117
     *
118
     * @return bool
119
     * @internal param $subject
120
     */
121
    public function recordedIp()
122
    {
123
        return ! $this->redis->set($this->keys->ip(request()->ip()), true, 'EX', $this->ipSeconds, 'NX');
0 ignored issues
show
Bug introduced by
The method set() does not exist on Illuminate\Support\Facades\Redis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
        return ! $this->redis->/** @scrutinizer ignore-call */ set($this->keys->ip(request()->ip()), true, 'EX', $this->ipSeconds, 'NX');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
    }
125
126
    /**
127
     * Get visits of model instance.
128
     *
129
     * @return mixed
130
     * @internal param $subject
131
     */
132
    public function count()
133
    {
134
        if ($this->country) {
135
            return $this->redis->zscore($this->keys->visits . "_countries:{$this->keys->id}", $this->country);
0 ignored issues
show
Bug introduced by
The method zscore() does not exist on Illuminate\Support\Facades\Redis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
            return $this->redis->/** @scrutinizer ignore-call */ zscore($this->keys->visits . "_countries:{$this->keys->id}", $this->country);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
        } else if ($this->referer) {
137
            return $this->redis->zscore($this->keys->visits . "_referers:{$this->keys->id}", $this->referer);
138
        } else if ($this->operatingSystem) {
139
            return $this->redis->zscore($this->keys->visits . "_OSes:{$this->keys->id}", $this->operatingSystem);
140
        } else if ($this->language) {
141
            return $this->redis->zscore($this->keys->visits . "_languages:{$this->keys->id}", $this->language);
142
        }
143
144
        return intval(
145
            $this->keys->instanceOfModel ?
146
                $this->redis->zscore($this->keys->visits, $this->keys->id) :
147
                $this->redis->get($this->keys->visitsTotal())
0 ignored issues
show
Bug introduced by
The method get() does not exist on Illuminate\Support\Facades\Redis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
                $this->redis->/** @scrutinizer ignore-call */ 
148
                              get($this->keys->visitsTotal())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
        );
149
    }
150
151
    /**
152
     * use diffForHumans to show diff
153
     * @return Carbon
154
     */
155
    public function timeLeft()
156
    {
157
        return Carbon::now()->addSeconds($this->redis->ttl($this->keys->visits));
0 ignored issues
show
Bug introduced by
The method ttl() does not exist on Illuminate\Support\Facades\Redis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        return Carbon::now()->addSeconds($this->redis->/** @scrutinizer ignore-call */ ttl($this->keys->visits));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
    }
159
160
    /**
161
     * use diffForHumans to show diff
162
     * @return Carbon
163
     */
164
    public function ipTimeLeft()
165
    {
166
        return Carbon::now()->addSeconds($this->redis->ttl($this->keys->ip(request()->ip())));
167
    }
168
169
    protected function isCrawler()
170
    {
171
        return $this->ignoreCrawlers && app(CrawlerDetect::class)->isCrawler();
172
    }
173
174
    /**
175
     * Increment a new/old subject to the cache.
176
     *
177
     * @param int $inc
178
     * @param bool $force
179
     * @param bool $periods
180
     * @param array $ignore to ignore recording visits of periods, country, refer, language and operatingSystem. pass them on this array.
181
     */
182
    public function increment($inc = 1, $force = false, $ignore = [])
183
    {
184
        if ($force || (!$this->isCrawler() && !$this->recordedIp())) {
185
            $this->redis->zincrby($this->keys->visits, $inc, $this->keys->id);
0 ignored issues
show
Bug introduced by
The method zincrby() does not exist on Illuminate\Support\Facades\Redis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
            $this->redis->/** @scrutinizer ignore-call */ 
186
                          zincrby($this->keys->visits, $inc, $this->keys->id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
            $this->redis->incrby($this->keys->visitsTotal(), $inc);
0 ignored issues
show
Bug introduced by
The method incrby() does not exist on Illuminate\Support\Facades\Redis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
            $this->redis->/** @scrutinizer ignore-call */ 
187
                          incrby($this->keys->visitsTotal(), $inc);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
188
            if(is_array($this->globalIgnore) && sizeof($this->globalIgnore) > 0) {
189
                $ignore = array_merge($ignore, $this->globalIgnore);
190
            }
191
192
            //NOTE: $$method is parameter also .. ($periods,$country,$refer)
193
            foreach (['country', 'refer', 'periods', 'operatingSystem', 'language'] as $method) {
194
                if(! in_array($method, $ignore))  {
195
                    $this->{'record'.studly_case($method)}($inc);
196
                }
197
            }
198
        }
199
    }
200
201
    /**
202
     * @param int $inc
203
     * @param array $ignore to ignore recording visits like country, periods ...
204
     */
205
    public function forceIncrement($inc = 1, $ignore = [])
206
    {
207
        $this->increment($inc, true, $ignore);
208
    }
209
210
    /**
211
     * Decrement a new/old subject to the cache cache.
212
     *
213
     * @param int $dec
214
     * @param array $ignore to ignore recording visits like country, periods ...
215
     */
216
    public function decrement($dec = 1, $force = false, $ignore = [])
217
    {
218
        $this->increment(-$dec, $force, $ignore);
219
    }
220
221
    /**
222
     * @param int $dec
223
     * @param array $ignore to ignore recording visits like country, periods ...
224
     */
225
    public function forceDecrement($dec = 1, $ignore = [])
226
    {
227
        $this->decrement($dec, true, $ignore);
228
    }
229
230
    /**
231
     * @param $period
232
     * @param int $time
233
     * @return bool
234
     */
235
    public function expireAt($period, $time)
236
    {
237
        $periodKey = $this->keys->period($period);
238
        return $this->redis->expire($periodKey, $time);
0 ignored issues
show
Bug introduced by
The method expire() does not exist on Illuminate\Support\Facades\Redis. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

238
        return $this->redis->/** @scrutinizer ignore-call */ expire($periodKey, $time);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
239
    }
240
}
241