IndexTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 87
rs 10
c 1
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPagination() 0 3 1
A getIndexData() 0 24 1
A applyPostTypeFilter() 0 6 2
A applyFiltersToQuery() 0 10 1
A applySorting() 0 12 5
1
<?php
2
3
namespace Jidaikobo\Kontiki\Models\Traits;
4
5
use Illuminate\Database\Query\Builder;
6
use Jidaikobo\Kontiki\Utils\Pagination;
7
8
trait IndexTrait
9
{
10
    protected Pagination $pagination;
11
12
    public function getIndexData(
13
        string $context = '',
14
        array $queryParams = []
15
    ): array {
16
        $query = $this->applyFiltersToQuery($context, $queryParams);
17
18
        // Set up pagination
19
        $totalItems = $query->count();
20
        $paged = (int)($queryParams['paged'] ?? 1);
21
        $perPage = (int)($queryParams['perPage'] ?? 10);
22
        $this->pagination = new Pagination($paged, $perPage);
23
        $this->pagination->setTotalItems($totalItems);
24
25
        // Fetch and process data
26
        $data = $query->limit($this->pagination->getLimit())
27
                      ->offset($this->pagination->getOffset())
28
                      ->get()
29
                      ->map(fn($item) => (array) $item)
30
                      ->toArray();
31
32
        // process data (ex: UTC to JST)
33
        $processedData = array_map(fn($item) => $this->processDataBeforeGet($item), $data);
0 ignored issues
show
Bug introduced by
It seems like processDataBeforeGet() 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

33
        $processedData = array_map(fn($item) => $this->/** @scrutinizer ignore-call */ processDataBeforeGet($item), $data);
Loading history...
34
35
        return $processedData;
36
    }
37
38
    /**
39
     * Applies all filters and conditions to the query builder.
40
     *
41
     * @param string $context context text.
42
     * @param array $queryParams The query parameters.
43
     * @return Builder The modified query.
44
     */
45
    private function applyFiltersToQuery(
46
        string $context = '',
47
        array $queryParams = []
48
    ): Builder {
49
        $query = $this->getQuery();
0 ignored issues
show
Bug introduced by
It seems like getQuery() 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

49
        /** @scrutinizer ignore-call */ 
50
        $query = $this->getQuery();
Loading history...
50
        $query = $this->buildSearchConditions($query, $queryParams['s'] ?? '');
0 ignored issues
show
Bug introduced by
It seems like buildSearchConditions() 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

50
        /** @scrutinizer ignore-call */ 
51
        $query = $this->buildSearchConditions($query, $queryParams['s'] ?? '');
Loading history...
51
        $query = $this->getAdditionalConditions($query, $context);
0 ignored issues
show
Bug introduced by
It seems like getAdditionalConditions() 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

51
        /** @scrutinizer ignore-call */ 
52
        $query = $this->getAdditionalConditions($query, $context);
Loading history...
52
        $query = $this->applySorting($query, $queryParams);
53
        $query = $this->applyPostTypeFilter($query);
54
        return $query;
55
    }
56
57
    /**
58
     * Applies sorting conditions to the query.
59
     *
60
     * @param Builder $query The query builder instance.
61
     * @param array $queryParams The query parameters containing sorting details.
62
     * @return Builder The modified query.
63
     */
64
    private function applySorting(Builder $query, array $queryParams): Builder
65
    {
66
        if (!empty($queryParams['orderby']) && !empty($queryParams['order'])) {
67
            $validColumns = array_keys($this->getFields());
0 ignored issues
show
Bug introduced by
It seems like getFields() 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

67
            $validColumns = array_keys($this->/** @scrutinizer ignore-call */ getFields());
Loading history...
68
            $column = in_array($queryParams['orderby'], $validColumns, true) ?
69
                $queryParams['orderby'] :
70
                'id';
71
            $direction = strtoupper($queryParams['order']) === 'DESC' ? 'DESC' : 'ASC';
72
            return $query->orderBy($column, $direction);
0 ignored issues
show
Bug introduced by
It seems like $column can also be of type string; however, parameter $column of Illuminate\Database\Query\Builder::orderBy() does only seem to accept Closure|Illuminate\Datab...\Database\Query\Builder, maybe add an additional type check? ( Ignorable by Annotation )

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

72
            return $query->orderBy(/** @scrutinizer ignore-type */ $column, $direction);
Loading history...
73
        }
74
75
        return $query->orderBy('id', 'DESC');
76
    }
77
78
    /**
79
     * Applies the post_type filter to the query.
80
     *
81
     * @param Builder $query The query builder instance.
82
     * @return Builder The modified query.
83
     */
84
    private function applyPostTypeFilter(Builder $query): Builder
85
    {
86
        if (!empty($this->getPostType())) {
0 ignored issues
show
Bug introduced by
It seems like getPostType() 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

86
        if (!empty($this->/** @scrutinizer ignore-call */ getPostType())) {
Loading history...
87
            return $query->where('post_type', '=', $this->getPostType());
88
        }
89
        return $query;
90
    }
91
92
    public function getPagination(): Pagination
93
    {
94
        return $this->pagination;
95
    }
96
}
97