Completed
Pull Request — master (#150)
by
unknown
06:02
created

ServiceInstances::cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PragmaRX\Firewall\Support;
4
5
trait ServiceInstances
6
{
7
    public $instances = [];
8
9
    /**
10
     * Get an instance.
11
     *
12
     * @param $binding
13
     * @param $instance
14
     */
15 65
    public function getInstance($binding)
16
    {
17 65
        if (is_null($instance = $this->getFromInstanceCache($binding))) {
18 65
            $instance = app('firewall.'.$binding);
19
        }
20
21 65
        $this->setInstance($binding, $instance);
22
23 65
        return $instance;
24
    }
25
26
    /**
27
     * Get an instance from the instance memory cache.
28
     *
29
     * @param $binding
30
     * @param $instance
31
     */
32 65
    public function getFromInstanceCache($binding)
33
    {
34 65
        if (isset($this->instances[$binding])) {
35 60
            return $this->instances[$binding];
36
        }
37 65
    }
38
39
    /**
40
     * Set the instance.
41
     *
42
     * @param $binding
43
     * @param $instance
44
     */
45 65
    public function setInstance($binding, $instance)
46
    {
47 65
        $this->instances[$binding] = $instance;
48 65
    }
49
50
    /**
51
     * Get the Countries instance.
52
     *
53
     * @return \PragmaRX\Firewall\Repositories\Countries
54
     */
55 35
    public function countries()
56
    {
57 35
        return $this->getInstance('countries');
58
    }
59
60
    /**
61
     * Get the IpList instance.
62
     *
63
     * @return \PragmaRX\Firewall\Repositories\IpList
64
     */
65 60
    public function ipList()
66
    {
67 60
        return $this->getInstance('iplist');
68
    }
69
70
    /**
71
     * Get the MessageRepository instance.
72
     *
73
     * @return \PragmaRX\Firewall\Repositories\Message
74
     */
75 46
    public function messages()
76
    {
77 46
        return $this->getInstance('messages');
78
    }
79
80
    /**
81
     * Get the Cache instance.
82
     *
83
     * @return \PragmaRX\Firewall\Repositories\Cache\Cache
84
     */
85 52
    public function cache()
86
    {
87 52
        return $this->getInstance('cache');
88
    }
89
90
    /**
91
     * Get the Config instance.
92
     *
93
     * @return \PragmaRX\Support\Config
94
     */
95 65
    public function config()
96
    {
97 65
        return $this->getInstance('config');
98
    }
99
100
    /**
101
     * Get the FileSystem instance.
102
     *
103
     * @return \PragmaRX\Support\Filesystem
104
     */
105
    public function fileSystem()
106
    {
107
        return $this->getInstance('filesystem');
108
    }
109
110
    /**
111
     * Get the GeoIp instance.
112
     *
113
     * @return \PragmaRX\Support\GeoIp\GeoIp
114
     */
115 28
    public function geoIp()
116
    {
117 28
        return $this->getInstance('geoip');
118
    }
119
120
    /**
121
     * Get the IpAddress instance.
122
     *
123
     * @return \PragmaRX\Firewall\Support\IpAddress
124
     */
125 57
    public function ipAddress()
126
    {
127 57
        return $this->getInstance('ipaddress');
128
    }
129
130
    /**
131
     * Get the DataRepository instance.
132
     *
133
     * @return \PragmaRX\Firewall\Repositories\DataRepository
134
     */
135 27
    public function dataRepository()
136
    {
137 27
        return $this->getInstance('datarepository');
138
    }
139
}
140