OwlCarousel   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 61
ccs 0
cts 20
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A run() 0 11 2
A getPluginInit() 0 4 1
A initCarouselJs() 0 6 1
1
<?php
2
3
namespace hiqdev\themes\obaju\widgets;
4
5
use hiqdev\themes\obaju\OwlCarouselAsset;
6
use yii\base\Widget;
7
use yii\helpers\Html;
8
use yii\web\JsExpression;
9
use yii\web\View;
10
11
class OwlCarousel extends Widget
12
{
13
    /**
14
     * Items (div, a, img, span, li etc.)
15
     * @var array
16
     */
17
    public $items = [];
18
19
    public $formatter;
20
21
    /**
22
     * Container element (div, ul etc.)
23
     * @var string
24
     */
25
    public $containerTag = 'div';
26
27
    public $wrapperTag = 'div';
28
29
    /**
30
     * Only the class owl-carousel is mandatory to apply proper styles
31
     * NOTE: The owl-theme class is optional, but without it, you will need to style navigation features on your own.
32
     * @var array
33
     */
34
    protected $defaultContainerOptions = ['class' => 'owl-carousel owl-theme'];
35
36
    public $containerOptions = [];
37
38
    public function init()
39
    {
40
        if (is_callable($this->formatter)) {
41
            $this->items = array_map($this->formatter, $this->items, array_keys($this->items));
42
        }
43
    }
44
45
    public function run()
46
    {
47
        $this->initCarouselJs();
48
        $out = Html::beginTag($this->containerTag, array_merge($this->defaultContainerOptions, $this->containerOptions));
49
        foreach ($this->items as $item) {
50
            $out .= $item;
51
        }
52
        $out .= Html::endTag($this->containerTag);
53
54
        return $out;
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getPluginInit()
61
    {
62
        return '$(".owl-carousel").owlCarousel();';
63
    }
64
65
    protected function initCarouselJs()
66
    {
67
        OwlCarouselAsset::register($this->view);
68
        $pi = new JsExpression($this->getPluginInit());
69
        $this->view->registerJs($pi, View::POS_READY);
70
    }
71
}
72