Passed
Push — master ( 8994c2...30162a )
by bader
03:20
created

Visits::ipTimeLeft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 Carbon\Carbon;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Support\Facades\Redis;
12
use Jaybizzle\CrawlerDetect\CrawlerDetect;
13
14
class Visits
15
{
16
    use Record, Lists, Periods, Setters;
0 ignored issues
show
introduced by
The trait awssat\Visits\Traits\Record requires some properties which are not provided by awssat\Visits\Visits: $iso_code, $id
Loading history...
introduced by
The trait awssat\Visits\Traits\Lists requires some properties which are not provided by awssat\Visits\Visits: $id, $primary
Loading history...
17
18
    /**
19
     * @var mixed
20
     */
21
    protected $ipSeconds;
22
    /**
23
     * @var null
24
     */
25
    protected $subject;
26
    /**
27
     * @var bool|mixed
28
     */
29
    protected $fresh = false;
30
    /**
31
     * @var null
32
     */
33
    protected $country = null;
34
    /**
35
     * @var null
36
     */
37
    protected $referer = null;
38
    /**
39
     * @var mixed
40
     */
41
    protected $periods;
42
    /**
43
     * @var Keys
44
     */
45
    protected $keys;
46
    /**
47
     * @var Redis
48
     */
49
    public $redis;
50
    /**
51
     * @var boolean
52
     */
53
    public $ignoreCrawlers = false;
54
55
    /**
56
     * Visits constructor.
57
     * @param $subject
58
     * @param string $tag|null
59
     */
60
    public function __construct($subject = null, $tag = 'visits')
61
    {
62
        $config = config('visits');
63
        $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...
64
        $this->periods = $config['periods'];
65
        $this->ipSeconds = $config['remember_ip'];
66
        $this->fresh = $config['always_fresh'];
67
        $this->ignoreCrawlers = $config['ignore_crawlers'];
68
        $this->subject = $subject;
69
        $this->keys = new Keys($subject, $tag);
70
71
        $this->periodsSync();
72
    }
73
74
    /**
75
     * @param $subject
76
     * @return $this
77
     */
78
    public function by($subject)
79
    {
80
        if($subject instanceof Model) {
81
            $this->keys->append($this->keys->modelName($subject), $subject->{$subject->getKeyName()});
82
        } else if (is_array($subject)) {
83
            $this->keys->append(array_keys($subject)[0], array_first($subject));
84
        }
85
86
        return $this;
87
    }
88
89
    /**
90
     * Reset methods
91
     *
92
     * @param $method
93
     * @param string $args
94
     * @return Reset
95
     */
96
    public function reset($method = 'visits', $args = '')
97
    {
98
        return new Reset($this, $method, $args);
99
    }
100
101
    /**
102
     * Check for the ip is has been recorded before
103
     *
104
     * @return bool
105
     * @internal param $subject
106
     */
107
    public function recordedIp()
108
    {
109
        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

109
        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...
110
    }
111
112
    /**
113
     * Get visits of model instance.
114
     *
115
     * @return mixed
116
     * @internal param $subject
117
     */
118
    public function count()
119
    {
120
        if ($this->country) {
121
            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

121
            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...
122
        } else if ($this->referer) {
123
            return $this->redis->zscore($this->keys->visits . "_referers:{$this->keys->id}", $this->referer);
124
        }
125
126
        return intval(
127
            $this->keys->instanceOfModel ?
128
                $this->redis->zscore($this->keys->visits, $this->keys->id) :
129
                $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

129
                $this->redis->/** @scrutinizer ignore-call */ 
130
                              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...
130
        );
131
    }
132
133
    /**
134
     * use diffForHumans to show diff
135
     * @return Carbon
136
     */
137
    public function timeLeft()
138
    {
139
        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

139
        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...
140
    }
141
142
    /**
143
     * use diffForHumans to show diff
144
     * @return Carbon
145
     */
146
    public function ipTimeLeft()
147
    {
148
        return Carbon::now()->addSeconds($this->redis->ttl($this->keys->ip(request()->ip())));
149
    }
150
151
    protected function isCrawler()
152
    {
153
        return $this->ignoreCrawlers && app(CrawlerDetect::class)->isCrawler();
154
    }
155
156
    /**
157
     * Increment a new/old subject to the cache.
158
     *
159
     * @param int $inc
160
     * @param bool $force
161
     * @param bool $periods
162
     * @param bool $country
163
     * @param bool $refer
164
     */
165
    public function increment($inc = 1, $force = false, $periods = true, $country = true, $refer = true)
166
    {
167
        if ($force OR !$this->isCrawler() && !$this->recordedIp()) {
168
            $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

168
            $this->redis->/** @scrutinizer ignore-call */ 
169
                          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...
169
            $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

169
            $this->redis->/** @scrutinizer ignore-call */ 
170
                          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...
170
171
            //NOTE: $method is parameter also .. ($periods,$country,$refer)
172
            foreach (['country', 'refer', 'periods'] as $method) {
173
                $$method && $this->{'record' . studly_case($method)}($inc);
174
            }
175
        }
176
    }
177
178
    /**
179
     * @param int $inc
180
     * @param bool $periods
181
     */
182
    public function forceIncrement($inc = 1, $periods = true)
183
    {
184
        $this->increment($inc, true, $periods);
185
    }
186
187
    /**
188
     * Decrement a new/old subject to the cache cache.
189
     *
190
     * @param int $dec
191
     * @param bool $force
192
     */
193
    public function decrement($dec = 1, $force = false)
194
    {
195
        $this->increment(-$dec, $force);
196
    }
197
198
    /**
199
     * @param int $dec
200
     * @param bool $periods
201
     */
202
    public function forceDecrement($dec = 1, $periods = true)
203
    {
204
        $this->increment(-$dec, true, $periods);
205
    }
206
207
    /**
208
     * @param $period
209
     * @param int $time
210
     * @return bool
211
     */
212
    public function expireAt($period, $time)
213
    {
214
        $periodKey = $this->keys->period($period);
215
        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

215
        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...
216
    }
217
}
218