Base   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 275
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 23
c 3
b 0
f 2
lcom 3
cbo 2
dl 0
loc 275
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getSettingsRepository() 0 4 1
A setSettingsRepository() 0 4 1
A getUsersRepository() 0 4 1
A setUsersRepository() 0 4 1
A getLogsRepository() 0 4 1
A setLogsRepository() 0 4 1
A getVisitsRepository() 0 4 1
A setVisitsRepository() 0 4 1
A getJobsRepository() 0 4 1
A setJobsRepository() 0 4 1
A getSettingsMethods() 0 12 2
A __call() 0 12 2
A Log() 0 10 3
A TargettedLog() 0 4 1
A RegisterVisit() 0 14 1
B CompleteVisits() 0 25 3
1
<?php namespace jlourenco\base;
2
3
use jlourenco\base\Repositories\SettingsRepositoryInterface;
4
use jlourenco\base\Repositories\UserRepositoryInterface;
5
use jlourenco\base\Repositories\LogRepositoryInterface;
6
use jlourenco\base\Repositories\VisitsRepositoryInterface;
7
use jlourenco\base\Repositories\JobsRepositoryInterface;
8
use BadMethodCallException;
9
use Request;
10
use Jenssegers\Agent\Agent;
11
use GeoIP;
12
use Sentinel;
13
14
class Base
15
{
16
17
    /**
18
     * The Settings repository.
19
     *
20
     * @var \jlourenco\base\Repositories\SettingsRepositoryInterface
21
     */
22
    protected $settings;
23
24
    /**
25
     * The User repository.
26
     *
27
     * @var \jlourenco\base\Repositories\UserRepositoryInterface
28
     */
29
    protected $user;
30
31
    /**
32
     * The Log repository.
33
     *
34
     * @var \jlourenco\base\Repositories\LogRepositoryInterface
35
     */
36
    protected $logs;
37
38
    /**
39
     * The Job repository.
40
     *
41
     * @var \jlourenco\base\Repositories\JobsRepositoryInterface
42
     */
43
    protected $jobs;
44
45
    /**
46
     * The Visits repository.
47
     *
48
     * @var \jlourenco\base\Repositories\VisitsRepositoryInterface
49
     */
50
    protected $visits;
51
52
    /**
53
     * Cached, available methods on the settings repository, used for dynamic calls.
54
     *
55
     * @var array
56
     */
57
    protected $settingsMethods = [];
58
59
    /**
60
     * Create a new Base instance.
61
     *
62
     * @param  \jlourenco\base\Repositories\SettingsRepositoryInterface  $settings
63
     * @param  \jlourenco\base\Repositories\UserRepositoryInterface  $user
64
     * @param  \jlourenco\base\Repositories\LogRepositoryInterface  $log
65
     * @param  \jlourenco\base\Repositories\VisitsRepositoryInterface  $visits
66
     */
67
    public function __construct(SettingsRepositoryInterface $settings, UserRepositoryInterface $user, LogRepositoryInterface $log, VisitsRepositoryInterface $visits, JobsRepositoryInterface $jobs)
68
    {
69
        $this->settings = $settings;
70
        $this->user = $user;
71
        $this->logs = $log;
72
        $this->visits = $visits;
73
        $this->jobs = $jobs;
74
    }
75
76
    /**
77
     * Returns the settings repository.
78
     *c
79
     * @return \jlourenco\base\Repositories\SettingsRepositoryInterface
80
     */
81
    public function getSettingsRepository()
82
    {
83
        return $this->settings;
84
    }
85
86
    /**
87
     * Sets the ettings repository.
88
     *
89
     * @param  \jlourenco\base\Repositories\SettingsRepositoryInterface $settings
90
     * @return void
91
     */
92
    public function setSettingsRepository(SettingsRepositoryInterface $settings)
93
    {
94
        $this->settings = $settings;
95
    }
96
97
    /**
98
     * Returns the ers repository.
99
     *c
100
     * @return \jlourenco\base\Repositories\UserRepositoryInterface
101
     */
102
    public function getUsersRepository()
103
    {
104
        return $this->user;
105
    }
106
107
    /**
108
     * Sets the ettings repository.
109
     *
110
     * @param  \jlourenco\base\Repositories\UserRepositoryInterface $user
111
     * @return void
112
     */
113
    public function setUsersRepository(UserRepositoryInterface $user)
114
    {
115
        $this->user = $user;
116
    }
117
118
    /**
119
     * Returns the log repository.
120
     *c
121
     * @return \jlourenco\base\Repositories\LogRepositoryInterface
122
     */
123
    public function getLogsRepository()
124
    {
125
        return $this->logs;
126
    }
127
128
    /**
129
     * Sets the log repository.
130
     *
131
     * @param  \jlourenco\base\Repositories\LogRepositoryInterface $log
132
     * @return void
133
     */
134
    public function setLogsRepository(LogRepositoryInterface $log)
135
    {
136
        $this->logs = $log;
137
    }
138
139
    /**
140
     * Returns the visits repository.
141
     *c
142
     * @return \jlourenco\base\Repositories\VisitsRepositoryInterface
143
     */
144
    public function getVisitsRepository()
145
    {
146
        return $this->visits;
147
    }
148
149
    /**
150
     * Sets the visits repository.
151
     *
152
     * @param  \jlourenco\base\Repositories\VisitsRepositoryInterface $visits
153
     * @return void
154
     */
155
    public function setVisitsRepository(VisitsRepositoryInterface $visits)
156
    {
157
        $this->visits = $visits;
158
    }
159
160
    /**
161
     * Returns the jobs repository.
162
     *c
163
     * @return \jlourenco\base\Repositories\JobsRepositoryInterface
164
     */
165
    public function getJobsRepository()
166
    {
167
        return $this->jobs;
168
    }
169
170
    /**
171
     * Sets the jobs repository.
172
     *
173
     * @param  \jlourenco\base\Repositories\JobsRepositoryInterface $jobs
174
     * @return void
175
     */
176
    public function setJobsRepository(JobsRepositoryInterface $jobs)
177
    {
178
        $this->jobs = $jobs;
179
    }
180
181
    /**
182
     * Returns all accessible methods on the associated settings repository.
183
     *
184
     * @return array
185
     */
186
    protected function getSettingsMethods()
187
    {
188
        if (empty($this->settingsMethods)) {
189
            $settings = $this->getSettingsRepository();
190
191
            $methods = get_class_methods($settings);
192
193
            $this->settingsMethods = array_diff($methods, ['__construct']);
194
        }
195
196
        return $this->settingsMethods;
197
    }
198
199
    /**
200
     * Dynamically pass missing methods to Base.
201
     *
202
     * @param  string  $method
203
     * @param  array  $parameters
204
     * @return mixed
205
     * @throws \BadMethodCallException
206
     */
207
    public function __call($method, $parameters)
208
    {
209
        $methods = $this->getSettingsMethods();
210
211
        if (in_array($method, $methods)) {
212
            $users = $this->getSettingsRepository();
213
214
            return call_user_func_array([$users, $method], $parameters);
215
        }
216
217
        throw new BadMethodCallException("Call to undefined method {$method}()");
218
    }
219
220
    /**
221
     * Create a log
222
     *
223
     * @param $logMessage
224
     */
225
    public function Log($logMessage)
226
    {
227
        $target = null;
228
229
        if ($user = Sentinel::getUser())
230
            if ($user != null)
231
                $target = $user->id;
232
233
        $log = $this->getLogsRepository()->create(['log' => $logMessage, 'ip' => Request::ip(), 'target' => $target]);
0 ignored issues
show
Bug introduced by
The method create() does not seem to exist on object<jlourenco\base\Re...LogRepositoryInterface>.

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...
Unused Code introduced by
$log is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
234
    }
235
236
    /**
237
     * Create a log and associates it to a user
238
     *
239
     * @param $logMessage
240
     * @param $userTarget
241
     */
242
    public function TargettedLog($logMessage, $userTarget)
243
    {
244
        $log = $this->getLogsRepository()->create(['log' => $logMessage, 'target' => $userTarget, 'ip' => Request::ip()]);
0 ignored issues
show
Bug introduced by
The method create() does not seem to exist on object<jlourenco\base\Re...LogRepositoryInterface>.

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...
Unused Code introduced by
$log is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
245
    }
246
247
    public function RegisterVisit()
248
    {
249
        $agent = new Agent();
250
        $browser = $agent->browser();
251
        $version = $agent->version($browser);
252
253
        $platform = $agent->platform();
254
        $pversion = $agent->version($platform);
255
256
        $browserString = $browser . ' version ' . $version . ' | ' . $platform . ' version ' . $pversion;
257
258
259
        $visit = $this->getVisitsRepository()->create(['url' => Request::url(), 'ip' => Request::ip(), 'browser' => $browserString]);
0 ignored issues
show
Bug introduced by
The method create() does not seem to exist on object<jlourenco\base\Re...itsRepositoryInterface>.

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...
Unused Code introduced by
$visit is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
260
    }
261
262
    public function CompleteVisits()
263
    {
264
        $visits = $this->getVisitsRepository()->getUnChecked();
0 ignored issues
show
Bug introduced by
The method getUnChecked() does not seem to exist on object<jlourenco\base\Re...itsRepositoryInterface>.

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...
265
266
        foreach ($visits as $visit)
267
        {
268
            $location = GeoIP::getLocation($visit->ip);
269
270
            if (!$location['default'])
271
            {
272
                $visit->isoCode = $location['isoCode'];
273
                $visit->country = $location['country'];
274
                $visit->city = $location['city'];
275
                $visit->state = $location['state'];
276
                $visit->postal_code = $location['postal_code'];
277
                $visit->lat = $location['lat'];
278
                $visit->lon = $location['lon'];
279
                $visit->timezone = $location['timezone'];
280
                $visit->continent = $location['continent'];
281
            }
282
283
            $visit->checked = 1;
284
            $visit->save();
285
        }
286
    }
287
288
}
289