CursorPaginationServiceProvider::registerMacro()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 7.3389
c 0
b 0
f 0
cc 9
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace DutchCodingCompany\CursorPagination;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
7
use Illuminate\Database\Query\Builder as QueryBuilder;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\ServiceProvider;
11
12
class CursorPaginationServiceProvider extends ServiceProvider
13
{
14
    /**
15
     * Bootstrap the application services.
16
     */
17
    public function boot()
18
    {
19
20
        if ($this->app->runningInConsole()) {
21
            $this->publishes([
22
                __DIR__ . '/../config/config.php' => config_path('cursor-pagination.php'),
23
            ], 'config');
24
        }
25
26
        $this->registerMacro();
27
    }
28
29
    /**
30
     * Create Macros for the Builders.
31
     */
32
    public function registerMacro()
33
    {
34
        $macro = function ($first, $currentCursor = null) {
35
            $queryOrders = isset($this->query) ? collect($this->query->orders) : collect($this->orders);
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property orders does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
            $cursor = $currentCursor instanceof Cursor ? $currentCursor : Cursor::decode($currentCursor);
37
38
            if ($cursor->isPresent()) {
39
                $apply = function ($query, $queryOrders, $cursor) use (&$apply) {
40
                    $query->where(function ($query) use ($queryOrders, $cursor, $apply) {
41
                        $order = array_shift($queryOrders);
42
                        $column = Arr::get($order, 'column');
43
                        $direction = Arr::get($order, 'direction');
44
45
                        $value = $cursor->get($column);
46
47
                        $query->where($column, $direction === 'asc' ? '>' : '<', $value);
48
49
                        if (!empty($queryOrders)) {
50
                            $query->orWhere($column, $value);
51
                            $apply($query, $queryOrders, $cursor);
52
                        }
53
                    });
54
                };
55
56
                $apply($this, $queryOrders->toArray(), collect($cursor->getAfterCursor()));
57
            }
58
59
            $items = $this->limit($first + 1)->get();
0 ignored issues
show
Bug introduced by
The method limit() does not seem to exist on object<DutchCodingCompan...inationServiceProvider>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61
            $hasAfter = false;
62
            // Check if there are items
63
            if ($items->count() === 0) {
64
                return new CursorPaginator($items, $currentCursor, $hasAfter);
65
            }
66
            // Check if there is a next page
67
            if ($items->count() > $first) {
68
                $items->pop();
69
                $hasAfter = true;
70
            }
71
72
            // Get last item and calculate afterCursor
73
            $lastItem = $items->last();
74
            $afterCursor = $queryOrders->mapWithKeys(function ($item) use ($lastItem) {
75
                $column = Arr::get($item, 'column');
76
                $value =  $lastItem->{$column};
77
                if ($value instanceof Carbon) {
78
                    $value = $value->toIso8601String();
79
                }
80
                return [$column => $value];
81
            })->toArray();
82
            $afterCursor = new Cursor($afterCursor);
83
84
            return new CursorPaginator($items, $afterCursor, $hasAfter);
85
        };
86
87
        // Register macros
88
        QueryBuilder::macro('cursorPaginate', $macro);
89
        EloquentBuilder::macro('cursorPaginate', $macro);
90
    }
91
92
    /**
93
     * Register the application services.
94
     */
95
    public function register()
96
    {
97
        // Automatically apply the package configuration
98
        $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'cursor-pagination');
99
100
        // Register the main class to use with the facade
101
        $this->app->singleton('cursor-pagination', function () {
102
            return new CursorPaginator;
103
        });
104
    }
105
}
106