Passed
Branch master (a88228)
by Antonio Carlos
03:04
created

ServiceInstances::setInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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