Passed
Push — dependabot/npm_and_yarn/@vue/t... ( 1a0b23...9776fc )
by
unknown
84:45 queued 64:29
created

Set::fromReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
nc 1
nop 1
crap 1
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\Snippets\WithSnippets;
10
use Illuminate\Contracts\Pagination\Paginator;
11
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract;
12
13
class Set extends Collection implements ViewableContract
14
{
15
    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...
16
        Viewable{
17
            renderView as baseRenderView;
18
        }
19
20 9
    protected $baseViewPath;
21
    protected $viewKey;
22 9
23
    public function __construct($items = [], string $viewKey)
24 9
    {
25
        $this->viewKey = $viewKey;
26 9
27 9
        if (!isset($this->baseViewPath)) {
28
            $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.sets', 'sets');
29 6
        }
30
31 6
        $this->constructWithSnippets();
32
33
        parent::__construct($items);
34 2
    }
35
36 2
    public static function fromReference(SetReference $setReference): Set
37
    {
38
        return $setReference->toSet();
39 6
    }
40
41 6
    public function renderView(): string
42
    {
43 6
        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

43
        if ($result = $this->/** @scrutinizer ignore-call */ baseRenderView()) {
Loading history...
44
            return $result;
45
        }
46
47 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
48
        // if no content value is present. We don't consider the 'content' attribute to be a default as we do for module.
49
        return $this->map(function ($item) {
50
            return ($this->viewParent && $item instanceof ViewableContract)
51
                ? $item->setViewParent($this->viewParent)->renderView()
52
                : ($item->content ?? '');
53
        })->implode('');
54
    }
55
56
    /**
57 6
     * Override the collection map function to include the key
58
     *
59
     * @param  callable  $callback
60 6
     * @return static
61 6
     */
62 6
    public function map(callable $callback)
63 6
    {
64
        $keys = array_keys($this->items);
65
66
        $items = array_map($callback, $this->items, $keys);
67
68 6
        return new static(array_combine($keys, $items), $this->viewKey);
69 6
    }
70 6
71 6
    /**
72 6
     * Paginate the collection with a simple navigation (prev and next)
73
     *
74
     * @param int $perPage
75 6
     * @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...
76 6
     * @return Paginator
77 6
     */
78
    public function simplePaginate($perPage = 12, $currentPage = null): Paginator
79
    {
80
        $currentPage = $currentPage ?? request()->get('page', 1);
81
        $path = request()->path();
82
        $items = array_slice($this->all(), ($currentPage - 1) * $perPage);
83
84
        return (new \Illuminate\Pagination\Paginator($items, $perPage, $currentPage))->setPath($path);
85
    }
86
87
    /**
88
     * Paginate the collection with a length aware pagination result which allows
89
     * to navigate to the first, last or any specific page
90
     *
91
     * @param int $perPage
92 6
     * @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...
93 6
     * @return Paginator
94 6
     */
95 6
    public function paginate($perPage = 12, $currentPage = null): Paginator
96
    {
97
        $currentPage = $currentPage ?? request()->get('page', 1);
98
        $path = request()->path();
99
        $items = array_slice($this->all(), ($currentPage - 1) * $perPage, $perPage);
100
101
        return (new \Illuminate\Pagination\LengthAwarePaginator($items, $this->count(), $perPage, $currentPage))->setPath($path);
102
    }
103
}
104