Completed
Push — master ( 6e55d9...6317d4 )
by Silvan
02:41
created

SlickWidget   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 68
ccs 26
cts 31
cp 0.8387
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 32 2
A slickConfigFile() 0 19 3
A run() 0 3 1
1
<?php
2
3
namespace dev7ch\slick;
4
5
use dev7ch\slick\assets\PicturefillAsset;
6
use dev7ch\slick\assets\ResourcesAsset;
7
use dev7ch\slick\assets\SlickAsset;
8
9 1
\Yii::setAlias('@slick', '@bower/slick-carousel');
10 1
\Yii::setAlias('@resources', dirname(__DIR__).'/src/resources');
11
12
class SlickWidget extends \luya\base\Widget
13
{
14
    public $baseUrl;
15
    public $images;
16
    public $slickConfig = [];
17
    public $slickConfigWidget;
18
    public $slickConfigFile;
19
20
21 2
    private function slickConfigFile() {
22
23 2
        $file = dirname(__DIR__, 3) . '/' . $this->slickConfigFile;
24 2
        $type = pathinfo($file);
25
26 2
        if ($type['extension'] == 'php') {
27 2
            $config = include $file;
28
29
        } elseif ($type['extension'] == 'json') {
30
            $json = file_get_contents($file);
31
            $config = json_decode($json, true);
32
        }
33
34
        else {
35
36
            throw new \luya\Exception('The file type .'. $type['extension'] . ' is currently not supported. Please use .php or .json files array for custom configuration of Slick.js.');
37
        }
38
39
        return $this->slickConfigFile = $config;
40
    }
41
42
43 3
    public function init()
44
    {
45 3
        PicturefillAsset::register($this->getView());
46 3
        SlickAsset::register($this->getView());
47 3
        ResourcesAsset::register($this->getView());
48
49 3
        if ($this->slickConfigFile != null) {
50
51 2
            $this->slickConfig = $this->slickConfigFile();
52
53
        } else {
54
55 2
            $this->slickConfig = $this->slickConfigWidget;
56
            
57
        }
58
59 2
        $this->view->registerJs(
60
            "var slickSlider = $('.slick-slider').slick({"
61 2
                .implode(', ',
62 2
                    array_map(
63 2
                        function ($config, $option) {
64 2
                            return sprintf('%s:%s', $option, $config);
65 2
                        },
66 2
                        $this->slickConfig, array_keys($this->slickConfig)
67
                    )
68
                ).
69 2
            '});',
70 2
            \luya\web\View::POS_READY,
71 2
            'slickSlider'
72
        );
73
74 2
        parent::init();
75 2
    }
76
77 2
    public function run()
78
    {
79 2
        return $this->render('SlickSlider', ['widget' => $this]);
80
    }
81
}
82