Issues (6)

src/Traits/Visitable.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Hayrullah\LaravelVisits\Traits;
4
5
use Hayrullah\LaravelVisits\Models\Like;
0 ignored issues
show
The type Hayrullah\LaravelVisits\Models\Like was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Relations\MorphMany;
8
use Illuminate\Support\Facades\Auth;
9
10
/**
11
 * This file is part of Laravel Visit,.
12
 *
13
 * @license MIT
14
 */
15
trait Visitable
16
{
17
    /**
18
     * Define a polymorphic one-to-many relationship.
19
     *
20
     * @return MorphMany
21
     */
22
    public function visits()
23
    {
24
        return $this->morphMany(Like::class, 'visitable');
0 ignored issues
show
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

24
        return $this->/** @scrutinizer ignore-call */ morphMany(Like::class, 'visitable');
Loading history...
25
    }
26
27
    /**
28
     * Add this Object to the user visits.
29
     *
30
     * @param int $user_id [if  null its added to the auth user]
31
     */
32
    public function addVisit($user_id = null)
33
    {
34
        $user_id = $this->getUserId($user_id);
35
36
        $visit = new Like(['user_id' => $user_id]);
37
38
        $visit->ip = $this->getIp();
39
        $visit->previous = request()->previous;
40
41
        $details = $this->getIpDetails($visit->ip);
42
        $visit->domain = url('/');
43
        $visit->iso_code = $details->country ?? null;
44
        $visit->country = $details->country ?? null;
45
        $visit->city = $details->city ?? null;
46
        $visit->state = $details->region ?? null;
47
        $visit->postal_code = $details->postal ?? null;
48
        $visit->location = $details->loc ?? null;
49
        $visit->lat = explode(',', $details->loc)[0] ?? null;
50
        $visit->lon = explode(',', $details->loc)[1] ?? null;
51
        $visit->timezone = $details->timezone ?? null;
52
        $visit->org = $details->org ?? null;
53
54
        // Get Full Src
55
        //$visit->src = $this->getFullSrc($request['fullSrc']);
56
        //parse_str(parse_url($request['full_src'], PHP_URL_QUERY), $params);
57
        //$visit->device = get_item_if_exists($params, 'device');
58
        //$visit->device_model = get_item_if_exists($params, 'devicemodel');
59
        //$visit->utm_campaign = get_item_if_exists($params, 'utm_campaign');
60
        //$visit->utm_content = get_item_if_exists($params, 'utm_content');
61
        //$visit->utm_medium = get_item_if_exists($params, 'utm_medium');
62
        //$visit->utm_source = get_item_if_exists($params, 'utm_source');
63
        //$visit->keyword = get_item_if_exists($params, 'keyword');
64
        //$visit->placement = get_item_if_exists($params, 'placement');
65
        //$visit->ad_position = get_item_if_exists($params, 'adposition');
66
        //$visit->match_type = get_item_if_exists($params, 'matchtype');
67
        $this->visits()->save($visit);
68
69
        return $visit;
70
    }
71
72
    /**
73
     * Remove this Object from the user visits.
74
     *
75
     * @param int $user_id [if  null its added to the auth user]
76
     */
77
    public function removeVisit($user_id = null)
78
    {
79
        $this->visits()->where('user_id', $this->getUserId($user_id))->delete();
80
    }
81
82
    /**
83
     * Toggle the favorite status from this Object.
84
     *
85
     * @param int $user_id [if  null its added to the auth user]
86
     */
87
    public function toggleVisit($user_id = null)
88
    {
89
        $this->isVisited($user_id) ? $this->removeVisit($user_id) : $this->addVisit($user_id);
90
    }
91
92
    /**
93
     * Check if the user has favorited this Object.
94
     *
95
     * @param int $user_id [if  null its added to the auth user]
96
     *
97
     * @return bool
98
     */
99
    public function isVisited($user_id = null)
100
    {
101
        return $this->visits()->where('user_id', ($user_id) ? $user_id : Auth::id())->exists();
102
    }
103
104
    /**
105
     * Return a collection with the Users who marked as favorite this Object.
106
     *
107
     * @return Collection
108
     */
109
    public function visitedBy()
110
    {
111
        $user = $this->visits()->with('user')->get();
112
113
        return $user->mapWithKeys(function ($item) {
114
            return [$item['user']->id => $item['user']];
115
        });
116
    }
117
118
    /**
119
     * Count the number of visits.
120
     *
121
     * @return int
122
     */
123
    public function getVisitsCountAttribute()
124
    {
125
        return $this->visits()->count();
126
    }
127
128
    /**
129
     * @return visitsCount attribute
0 ignored issues
show
The type Hayrullah\LaravelVisits\Traits\visitsCount was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
130
     */
131
    public function visitsCount()
132
    {
133
        return $this->visitsCount;
134
    }
135
136
    /**
137
     * Add deleted observer to delete visits registers.
138
     *
139
     * @return void
140
     */
141
    public static function bootVisitable()
142
    {
143
        static::deleted(
144
            function ($model) {
145
                $model->visits()->delete();
146
            }
147
        );
148
    }
149
150
    private function getUserId($user_id = null)
151
    {
152
        $user_id = ($user_id) ? $user_id : null;
153
        if (!$user_id) {
154
            \auth()->check() ? Auth::id() : null;
155
        }
156
157
        return $user_id;
158
    }
159
160
    private function getIp()
161
    {
162
        if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
163
            return $_SERVER['HTTP_CF_CONNECTING_IP'];
164
        }
165
166
        return request()->ip();
167
    }
168
169
    private function getIpDetails($ip)
170
    {
171
        $details = json_decode(file_get_contents("https://ipinfo.io/{$ip}?token=f5e0d2a13bc766"));
172
        if (!$details) {
173
            $details = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
174
        }
175
176
//        $details = @json_decode(@file_get_contents('http://ipinfo.io/'.$geo->ip));
177
178
        return $details;
179
    }
180
}
181