Passed
Push — master ( 949a9d...145e5a )
by Ben
06:50
created

Set::paginateSetting()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\Sets;
6
7
use Illuminate\Support\Collection;
8
use Thinktomorrow\Chief\Concerns\Viewable\Viewable;
9
use Thinktomorrow\Chief\Relations\ActsAsParent;
10
use Thinktomorrow\Chief\Snippets\WithSnippets;
11
use Illuminate\Contracts\Pagination\Paginator;
12
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract;
13
14
class Set extends Collection implements ViewableContract
15
{
16
    use WithSnippets,
0 ignored issues
show
Bug introduced by
The trait Thinktomorrow\Chief\Concerns\Viewable\Viewable requires the property $content which is not provided by Thinktomorrow\Chief\Sets\Set.
Loading history...
17
        Viewable{
18
            renderView as baseRenderView;
19
        }
20
21
    protected $baseViewPath;
22
    protected $viewKey;
23 11
24
    /** @var array */
25 11
    private $settings;
26
27 11
    public function __construct($items = [], string $viewKey, array $settings = [])
28 11
    {
29
        $this->viewKey = $viewKey;
30
31 11
        if (!isset($this->baseViewPath)) {
32
            $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.sets', 'sets');
33 11
        }
34 11
35
        $this->constructWithSnippets();
36 2
37
        parent::__construct($items);
38 2
        $this->settings = $settings;
39
    }
40
41 6
    public static function fromReference(SetReference $setReference, ActsAsParent $parent): Set
42
    {
43 6
        return $setReference->toSet($parent);
44
    }
45
46
    public function renderView(): string
47
    {
48
        if ($result = $this->baseRenderView()) {
0 ignored issues
show
Bug introduced by
The method baseRenderView() does not exist on Thinktomorrow\Chief\Sets\Set. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

48
        if ($result = $this->/** @scrutinizer ignore-call */ baseRenderView()) {
Loading history...
49
            return $result;
50 6
        }
51 6
52 6
        // If no view has been created for this page collection, we try once again to fetch the content value if any. This will silently fail
53 6
        // if no content value is present. We don't consider the 'content' attribute to be a default as we do for module.
54
        return $this->map(function ($item) {
55
            return ($this->viewParent && $item instanceof ViewableContract)
56
                ? $item->setViewParent($this->viewParent)->renderView()
57
                : ($item->content ?? '');
58
        })->implode('');
59
    }
60
61
    /**
62 6
     * Override the collection map function to include the key
63
     *
64 6
     * @param  callable  $callback
65
     * @return static
66 6
     */
67
    public function map(callable $callback)
68 6
    {
69
        $keys = array_keys($this->items);
70
71
        $items = array_map($callback, $this->items, $keys);
72
73
        return new static(array_combine($keys, $items), $this->viewKey);
74
    }
75
76
    /**
77
     * Paginate the collection with a simple navigation (prev and next)
78 1
     *
79
     * @param int $perPage
80 1
     * @param null $currentPage
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $currentPage is correct as it would always require null to be passed?
Loading history...
81 1
     * @return Paginator
82 1
     */
83
    public function simplePaginate($perPage = null, $currentPage = null): Paginator
84 1
    {
85
        $currentPage = $currentPage ?? request()->get('page', 1);
86
        $path = request()->path();
87
        $items = array_slice($this->all(), ($currentPage - 1) * $perPage);
88
89
        return (new \Illuminate\Pagination\Paginator($items, $perPage ?? $this->paginateSetting('perPage', '12'), $currentPage))->setPath($path);
0 ignored issues
show
Bug introduced by
It seems like $perPage ?? $this->pagin...etting('perPage', '12') can also be of type string; however, parameter $perPage of Illuminate\Pagination\Paginator::__construct() does only seem to accept integer, 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

89
        return (new \Illuminate\Pagination\Paginator($items, /** @scrutinizer ignore-type */ $perPage ?? $this->paginateSetting('perPage', '12'), $currentPage))->setPath($path);
Loading history...
90
    }
91
92
    /**
93
     * Paginate the collection with a length aware pagination result which allows
94
     * to navigate to the first, last or any specific page
95 1
     *
96
     * @param int $perPage
97 1
     * @param null $currentPage
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $currentPage is correct as it would always require null to be passed?
Loading history...
98 1
     * @return Paginator
99 1
     */
100
    public function paginate($perPage = null, $currentPage = null): Paginator
101 1
    {
102
        $currentPage = $currentPage ?? request()->get('page', 1);
103
        $path = '/'.request()->path();
104
        $items = array_slice($this->all(), ($currentPage - 1) * $perPage, $perPage);
105
106
        return (new \Illuminate\Pagination\LengthAwarePaginator($items, $this->paginateSetting('total', $this->count()), $perPage ?? $this->paginateSetting('perPage', '12'), $currentPage))->setPath($path);
0 ignored issues
show
Bug introduced by
It seems like $perPage ?? $this->pagin...etting('perPage', '12') can also be of type string; however, parameter $perPage of Illuminate\Pagination\Le...aginator::__construct() does only seem to accept integer, 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

106
        return (new \Illuminate\Pagination\LengthAwarePaginator($items, $this->paginateSetting('total', $this->count()), /** @scrutinizer ignore-type */ $perPage ?? $this->paginateSetting('perPage', '12'), $currentPage))->setPath($path);
Loading history...
107
    }
108
109
    private function paginateSetting($key, $default = null)
110
    {
111
        if(!isset($this->settings['paginate']) || !isset($this->settings['paginate'][$key])) return $default;
112
113
        return $this->settings['paginate'][$key];
114
    }
115
}
116