Passed
Push — master ( 84eed8...6d6bcb )
by Alexey
02:52
created

FollowersWidget::getDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace dominus77\maintenance\widgets\followers;
4
5
use Exception;
6
use yii\base\Widget;
7
use yii\data\ArrayDataProvider;
8
use yii\helpers\ArrayHelper;
9
use yii\helpers\Html;
10
use yii\widgets\ListView;
11
use dominus77\maintenance\models\SubscribeForm;
12
use dominus77\maintenance\widgets\followers\assets\FollowersAsset;
13
14
/**
15
 * Class FollowersWidget
16
 * @package dominus77\maintenance\widgets\followers
17
 *
18
 * @property ArrayDataProvider $dataProvider
19
 */
20
class FollowersWidget extends Widget
21
{
22
    /**
23
     * @var bool
24
     */
25
    public $status = true;
26
27
    /**
28
     * @var SubscribeForm
29
     */
30
    public $model;
31
32
    /**
33
     * @var int
34
     */
35
    public $pageSize = 18;
36
37
    /**
38
     * ListView options
39
     * @var array
40
     */
41
    public $options;
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function init()
47
    {
48
        parent::init();
49
        $this->model = $this->findModel();
50
        $options = [
51
            'tag' => 'div',
52
            'class' => 'list-wrapper',
53
            'id' => 'list-followers',
54
        ];
55
        $this->options = ArrayHelper::merge($this->options, $options);
56
    }
57
58
    /**
59
     * @return string|void
60
     * @throws Exception
61
     */
62
    public function run()
63
    {
64
        if ($this->status === true) {
65
            $this->registerResource();
66
            echo ListView::widget([
67
                'dataProvider' => $this->getDataProvider(),
68
                'layout' => "{summary}\n{items}\n{pager}",
69
                'options' => $this->options,
70
                'itemView' => static function ($model) {
71
                    return Html::a($model['email'], 'mailto:' . $model['email']);
72
                }
73
            ]);
74
        }
75
    }
76
77
    /**
78
     * Register resource
79
     */
80
    protected function registerResource()
81
    {
82
        $view = $this->getView();
83
        FollowersAsset::register($view);
84
    }
85
86
    /**
87
     * @return ArrayDataProvider
88
     */
89
    protected function getDataProvider()
90
    {
91
        return new ArrayDataProvider([
92
            'allModels' => $this->model->getFollowers(),
93
            'pagination' => [
94
                'pageSize' => $this->pageSize
95
            ],
96
        ]);
97
    }
98
99
    /**
100
     * @return SubscribeForm
101
     */
102
    protected function findModel()
103
    {
104
        if ($this->model === null) {
105
            return new SubscribeForm();
106
        }
107
        return $this->model;
108
    }
109
}
110