Passed
Pull Request — main (#69)
by
unknown
05:50 queued 02:54
created

VisitorLogsService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 49
rs 10
c 3
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A trackPostView() 0 39 3
1
<?php
2
3
namespace CSlant\Blog\Api\Services;
4
5
use Carbon\Carbon;
6
use CSlant\Blog\Api\Models\VisitorLogs;
7
use CSlant\Blog\Core\Models\Post;
8
9
class VisitorLogsService
10
{
11
    /**
12
     * Track a view for a post by ID.
13
     *
14
     * @param int $postId The post ID
15
     * @param null|string $ipAddress The viewer's IP address
16
     * @param null|string $userAgent The viewer's user agent
17
     * @return Post The updated post
18
     */
19
    public function trackPostView(int $postId, ?string $ipAddress, ?string $userAgent = null): Post
20
    {
21
        $expirationMinutes = (int) config('blog-core.expiration_view_time', 60);
0 ignored issues
show
Unused Code introduced by
The call to config() has too many arguments starting with 60. ( Ignorable by Annotation )

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

21
        $expirationMinutes = (int) /** @scrutinizer ignore-call */ config('blog-core.expiration_view_time', 60);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
22
        $ipAddress = $ipAddress ?? '';
23
24
        $post = Post::findOrFail($postId);
25
        $entityType = get_class($post);
26
        $entityId = $post->getKey();
27
        $now = Carbon::now();
28
29
        /** @var null|VisitorLogs $existingView */
30
        $existingView = VisitorLogs::query()
31
            ->where('viewable_id', '=', $entityId)
32
            ->where('viewable_type', '=', $entityType)
33
            ->where('ip_address', '=', $ipAddress)
34
            ->first();
35
36
        if ($existingView === null) {
37
            VisitorLogs::create([
38
                'viewable_id' => $entityId,
39
                'viewable_type' => $entityType,
40
                'ip_address' => $ipAddress,
41
                'user_agent' => $userAgent,
42
                'expired_at' => $now->copy()->addMinutes($expirationMinutes),
43
            ]);
44
45
            $post->increment('views');
46
        } else {
47
            if ($now->isAfter($existingView->expired_at)) {
48
                $existingView->update([
49
                    'user_agent' => $userAgent,
50
                    'expired_at' => $now->copy()->addMinutes($expirationMinutes),
51
                ]);
52
53
                $post->increment('views');
54
            }
55
        }
56
57
        return $post->refresh();
58
    }
59
}
60