Passed
Pull Request — main (#69)
by
unknown
06:17 queued 03:11
created

PostService::trackView()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 21
c 2
b 0
f 1
nc 7
nop 2
dl 0
loc 37
rs 9.2728
1
<?php
2
3
namespace CSlant\Blog\Api\Services;
4
5
use Carbon\Carbon;
6
use CSlant\Blog\Api\Models\PostView;
7
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse;
8
use CSlant\Blog\Core\Models\Post;
9
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
10
use Illuminate\Database\Eloquent\Builder;
11
use Illuminate\Support\Arr;
12
13
/**
14
 * Class PostService
15
 *
16
 * @package CSlant\Blog\Api\Services
17
 *
18
 * @method BaseHttpResponse httpResponse()
19
 */
20
class PostService
21
{
22
    /**
23
     * Get posts by filters.
24
     *
25
     * @param  array<string, mixed>  $filters
26
     *
27
     * @return LengthAwarePaginator<Post>
28
     */
29
    public function getCustomFilters(array $filters): LengthAwarePaginator
30
    {
31
        $data = Post::query();
32
33
        if ($filters['categories'] !== null) {
34
            $categories = array_filter((array) $filters['categories']);
35
36
            $data = $data->whereHas('categories', function (Builder $query) use ($categories): void {
37
                $query->whereIn('categories.id', $categories);
38
            });
39
        }
40
41
        if ($filters['tags'] !== null) {
42
            $tags = array_filter((array) $filters['tags']);
43
44
            $data = $data->whereHas('tags', function (Builder $query) use ($tags): void {
45
                $query->whereIn('tags.id', $tags);
46
            });
47
        }
48
49
        $orderBy = Arr::get($filters, 'order_by', 'updated_at');
50
        $order = Arr::get($filters, 'order', 'desc');
51
52
        $data = $data
53
            ->wherePublished()
54
            ->orderBy($orderBy, $order);
55
56
        return $data->paginate((int) $filters['per_page']);
57
    }
58
59
    public function trackView(int $postId, string $ipAddress): bool
60
    {
61
        $post = Post::find($postId);
62
        if (!$post) {
0 ignored issues
show
introduced by
$post is of type Illuminate\Database\Eloquent\Builder, thus it always evaluated to true.
Loading history...
63
            return false;
64
        }
65
66
        $postView = PostView::where('post_id', $postId)
67
            ->where('ip_address', $ipAddress)
68
            ->first();
69
70
        $shouldIncrementView = false;
71
72
        if (!$postView) {
73
            // Access this post for the first time from this IP
74
            PostView::create([
75
                'post_id' => $postId,
76
                'ip_address' => $ipAddress,
77
                'time_check' => Carbon::now()->addHours(),
78
            ]);
79
            $shouldIncrementView = true;
80
        } else {
81
            // Check if field time_check is passed
82
            if (Carbon::now()->isAfter($postView->time_check)) {
83
                // Update field time_check
84
                $postView->update([
85
                    'time_check' => Caton::now()->addHours(),
0 ignored issues
show
Bug introduced by
The type CSlant\Blog\Api\Services\Caton 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...
86
                ]);
87
                $shouldIncrementView = true;
88
            }
89
        }
90
91
        if ($shouldIncrementView) {
92
            $post->increment('views');
93
        }
94
95
        return $shouldIncrementView;
96
    }
97
}
98