WidgetCollection::pinned()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
c 1
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace Anomaly\DashboardModule\Widget;
2
3
use Anomaly\DashboardModule\Widget\Contract\WidgetInterface;
4
use Anomaly\Streams\Platform\Entry\EntryCollection;
5
use Anomaly\UsersModule\User\Contract\UserInterface;
6
7
/**
8
 * Class WidgetCollection
9
 *
10
 * @link          http://pyrocms.com/
11
 * @author        PyroCMS, Inc. <[email protected]>
12
 * @author        Ryan Thompson <[email protected]>
13
 */
14
class WidgetCollection extends EntryCollection
15
{
16
17
    /**
18
     * Return only allowed widgets.
19
     *
20
     * @return WidgetCollection
21
     */
22
    public function allowed()
23
    {
24
        /* @var UserInterface $user */
25
        if (!$user = app('auth')->user()) {
26
            return $this->make([]);
27
        }
28
29
        return $this->filter(
30
            function ($widget) use ($user) {
31
32
                /* @var WidgetInterface $widget */
33
                return $user->hasAnyRole($widget->getAllowedRoles());
34
            }
35
        );
36
    }
37
38
    /**
39
     * Return only widgets that
40
     * are pinned to the top.
41
     *
42
     * @return static
43
     */
44
    public function pinned()
45
    {
46
        return $this->filter(
47
            function ($widget) {
48
49
                /* @var WidgetInterface $widget */
50
                return $widget->isPinned();
51
            }
52
        );
53
    }
54
55
    /**
56
     * Return only widgets in
57
     * the provided column.
58
     *
59
     * @param $column
60
     * @return static
61
     */
62
    public function column($column, $over = false)
63
    {
64
        return $this->filter(
65
            function ($widget) use ($column, $over) {
66
67
                /* @var WidgetInterface $widget */
68
                if ($widget->isPinned()) {
69
                    return false;
70
                }
71
72
                if ($widget->getColumn() == $column) {
73
                    return true;
74
                }
75
76
                if ($over && $widget->getColumn() > $column) {
77
                    return true;
78
                }
79
80
                return false;
81
            }
82
        );
83
    }
84
}
85