Passed
Pull Request — main (#69)
by
unknown
03:08
created

PostService::getCustomFilters()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 1
dl 0
loc 28
rs 9.7666
c 0
b 0
f 0
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) return false;
0 ignored issues
show
introduced by
$post is of type Illuminate\Database\Eloquent\Builder, thus it always evaluated to true.
Loading history...
63
64
        $postView = PostView::where('post_id', $postId)
65
            ->where('ip_address', $ipAddress)
66
            ->first();
67
68
        $shouldIncrementView = false;
69
70
        if(!$postView)
71
        {
72
            // Access this post for the first time from this IP
73
            PostView::create([
74
                'post_id' => $postId,
75
                'ip_address' => $ipAddress,
76
                'time_check' => Carbon::now()->addHours(),
77
            ]);
78
            $shouldIncrementView = true;
79
        }
80
        else {
81
            // Check if field time_check is passed
82
            if(Carbon::now()->isAfter($postView->time_check))
83
            {
84
                // Update field time_check
85
                $postView->update([
86
                    '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...
87
                ]);
88
                $shouldIncrementView = true;
89
            }
90
        }
91
92
        if($shouldIncrementView)
93
        {
94
            $post->increment('views');
95
        }
96
        return $shouldIncrementView;
97
    }
98
}
99