Passed
Push — master ( 65709d...74d483 )
by bader
04:24 queued 10s
created

Keys::period()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Awssat\Visits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Str;
7
8
class Keys
9
{
10
    public $modelName = false;
11
    public $id;
12
    public $visits;
13
    public $primary = 'id';
14
    public $instanceOfModel = false;
15
    public $tag;
16
17
    public function __construct($subject, $tag)
18
    {
19
        $this->modelName = $this->pluralModelName($subject);
20
        $this->primary = (new $subject)->getKeyName();
21
        $this->tag = $tag;
22
        $this->visits = $this->visits();
23
24
        if ($subject instanceof Model) {
25
            $this->instanceOfModel = true;
26
            $this->modelName = $this->modelName($subject);
27
            $this->id = $subject->{$subject->getKeyName()};
28
        }
29
    }
30
31
    /**
32
     * Get cache key
33
     */
34
    public function visits()
35
    {
36
        return (app()->environment('testing') ? 'testing:' : '').$this->modelName."_{$this->tag}";
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

36
        return (app()->/** @scrutinizer ignore-call */ environment('testing') ? 'testing:' : '').$this->modelName."_{$this->tag}";
Loading history...
37
    }
38
39
    /**
40
     * Get cache key for total values
41
     */
42
    public function visitsTotal()
43
    {
44
        return "{$this->visits}_total";
45
    }
46
47
    /**
48
     * ip key
49
     */
50
    public function ip($ip)
51
    {
52
        return $this->visits.'_'.Str::snake(
53
            'recorded_ips:'.($this->instanceOfModel ? "{$this->id}:" : '') . $ip
54
        );
55
    }
56
57
    /**
58
     * list cache key
59
     */
60
    public function cache($limit = '*', $isLow = false)
61
    {
62
        $key = $this->visits.'_lists';
63
64
        if ($limit == '*') {
65
            return "{$key}:*";
66
        }
67
68
        return "{$key}:".($isLow ? 'low' : 'top').$limit;
69
    }
70
71
    /**
72
     * period key
73
     */
74
    public function period($period)
75
    {
76
        return "{$this->visits}_{$period}";
77
    }
78
79
    public function append($relation, $id)
80
    {
81
        $this->visits .= "_{$relation}_{$id}";
82
    }
83
84
    public function modelName($subject)
85
    {
86
        return strtolower(Str::singular(class_basename(get_class($subject))));
87
    }
88
89
    public function pluralModelName($subject)
90
    {
91
        return strtolower(Str::plural(class_basename(is_string($subject) ? $subject : get_class($subject))));
92
    }
93
}
94