Completed
Push — dependabot/npm_and_yarn/tippy.... ( 40037f...deaa3c )
by
unknown
400:02 queued 379:38
created

Set::presentRawValueForParent()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.2935

Importance

Changes 6
Bugs 1 Features 0
Metric Value
eloc 23
c 6
b 1
f 0
dl 0
loc 39
ccs 17
cts 22
cp 0.7727
rs 9.2408
cc 5
nc 6
nop 1
crap 5.2935

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Set::map() 0 7 1
A Set::simplePaginate() 0 7 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
    protected $baseViewPath;
21
    protected $viewKey;
22
23 9
    public function __construct($items = [], string $viewKey)
24
    {
25 9
        $this->viewKey = $viewKey;
26
27 9
        if (!isset($this->baseViewPath)) {
28 9
            $this->baseViewPath = config('thinktomorrow.chief.base-view-paths.sets', 'sets');
29
        }
30
31 9
        $this->constructWithSnippets();
32
33 9
        parent::__construct($items);
34 9
    }
35
36 2
    public static function fromReference(SetReference $setReference): Set
37
    {
38 2
        return $setReference->toSet();
39
    }
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
        // 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 6
            return ($this->viewParent && $item instanceof ViewableContract)
51 6
                ? $item->setViewParent($this->viewParent)->renderView()
52 6
                : ($item->content ?? '');
53 6
        })->implode('');
54
    }
55
56
    /**
57
     * Override the collection map function to include the key
58
     *
59
     * @param  callable  $callback
60
     * @return static
61
     */
62 6
    public function map(callable $callback)
63
    {
64 6
        $keys = array_keys($this->items);
65
66 6
        $items = array_map($callback, $this->items, $keys);
67
68 6
        return new static(array_combine($keys, $items), $this->viewKey);
69
    }
70
71
    /**
72
     * Paginate the collection with a simple navigation (prev and next)
73
     *
74
     * @param int $perPage
75
     * @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
     * @return Paginator
77
     */
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
     * @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
     * @return Paginator
94
     */
95
    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