HtmlCarousel   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 27
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 141
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A getBase() 0 3 1
A setBase() 0 8 2
A run() 0 5 1
A createControls() 0 4 1
A createControl() 0 10 1
A getGlyph() 0 5 2
A setRightGlyph() 0 5 2
A setLeftGlyph() 0 5 2
A addImage() 0 19 4
B fromArray() 0 12 5
A createIndicator() 0 7 1
A compile() 0 6 1
A fromDatabaseObject() 0 3 1
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
use Ajax\JsUtils;
6
use Ajax\bootstrap\html\content\HtmlCarouselControl;
7
use Ajax\bootstrap\html\base\CssRef;
8
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
9
use Ajax\bootstrap\html\content\HtmlCarouselItem;
10
11
use Ajax\common\html\BaseHtml;
12
/**
13
 * Composant Twitter Bootstrap Carousel
14
 * @see http://getbootstrap.com/components/#carousel
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlCarousel extends BaseHtml {
19
	protected $indicators=array ();
20
	protected $slides=array ();
21
	protected $leftControl="";
22
	protected $rightControl="";
23
	protected $_base="";
24
	protected $_glyphs=array ();
25
26
	public function __construct($identifier, $images=NULL) {
27
		parent::__construct($identifier);
28
		$this->_template=include 'templates/tplCarousel.php';
29
		if ($images!=NULL) {
30
			if (is_array($images)) {
31
				$this->fromArray($images);
32
			}
33
		}
34
	}
35
36
	public function getBase() {
37
		return $this->_base;
38
	}
39
40
	public function setBase($_base) {
41
		foreach ($this->slides as $slide){
42
			$imgSrc=$slide->getImageSrc();
43
			$slide->setImageSrc(str_replace($this->_base.$imgSrc, $_base.$imgSrc, $imgSrc));
44
		}
45
		$this->_base=$_base;
46
		return $this;
47
	}
48
49
	public function run(JsUtils $js) {
50
		$this->_bsComponent=$js->bootstrap()->carousel("#".$this->identifier);
51
		$this->addEventsOnRun($js);
52
		return $this->_bsComponent;
53
	}
54
55
	private function createControls() {
56
		$this->rightControl=$this->createControl("next", "right");
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createControl('next', 'right') can also be of type object<Ajax\bootstrap\ht...nt\HtmlCarouselControl>. However, the property $rightControl is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
57
		$this->leftControl=$this->createControl("previous", "left");
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createControl('previous', 'left') can also be of type object<Ajax\bootstrap\ht...nt\HtmlCarouselControl>. However, the property $leftControl is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
58
	}
59
60
	/**
61
	 *
62
	 * @param string $caption
63
	 * @param string $sens
64
	 * @return HtmlCarouselControl|string
65
	 */
66
	private function createControl($caption="next", $sens="left") {
67
		$control=new HtmlCarouselControl($sens."-ctrl-".$this->identifier);
68
		$control->setClass($sens." carousel-control");
69
		$control->setProperty("data-slide", substr($caption, 0, 4));
70
		$control->setHref("#".$this->identifier);
71
		$control->setRole("button");
72
		$control->setCaption(ucfirst($caption));
73
		$control->setGlyphIcon($this->getGlyph($sens));
74
		return $control;
75
	}
76
77
	private function getGlyph($sens="left") {
78
		if (array_key_exists($sens, $this->_glyphs))
79
			return $this->_glyphs [$sens];
80
		return "glyphicon-chevron-".$sens;
81
	}
82
83
	public function setRightGlyph($glyphicon) {
84
		$glyphs=CssRef::glyphIcons();
85
		if (array_search($glyphicon, $glyphs)!==false)
86
			$this->_glyphs ["right"]=$glyphicon;
87
	}
88
89
	public function setLeftGlyph($glyphicon) {
90
		$glyphs=CssRef::glyphIcons();
91
		if (array_search($glyphicon, $glyphs)!==false)
92
			$this->_glyphs ["left"]=$glyphicon;
93
	}
94
95
	public function addImage($imageSrc, $imageAlt="", $caption=NULL, $description=NULL) {
96
		if(is_array($imageSrc)){
97
			$this->addImage($imageSrc[0],@$imageSrc[1],@$imageSrc[2],@$imageSrc[3]);
98
		}else{
99
			$image=new HtmlCarouselItem("item-".$this->identifier);
100
			$image->setImageSrc($this->_base.$imageSrc);
101
			$image->setImageAlt($imageAlt);
102
			$image->setClass("item");
103
			if (isset($caption)) {
104
				$optCaption="<h3>".$caption."</h3>";
105
				if (isset($description)) {
106
					$optCaption.="<p>".$description."</p>";
107
				}
108
				$image->setCaption($optCaption);
109
			}
110
			$this->slides []=$image;
111
			$this->createIndicator();
112
		}
113
	}
114
115
	/*
116
	 * (non-PHPdoc)
117
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromArray()
118
	 */
119
	public function fromArray($array) {
120
		if (is_array($array) && sizeof($array)>0) {
121
			foreach ( $array as $value ) {
122
				if (is_array($value)) {
123
					$this->addImage($value ["src"], @$value ["alt"], @$value ["caption"], @$value ["description"]);
124
				} else {
125
					$this->addImage($value);
126
				}
127
			}
128
		}
129
		return $this;
130
	}
131
132
	private function createIndicator() {
133
		$indicator=new HtmlBsDoubleElement("indicator-".$this->identifier);
134
		$indicator->setProperty("data-target", "#".$this->identifier);
135
		$indicator->setProperty("data-slide-to", sizeof($this->indicators));
136
		$indicator->setTagName("li");
137
		$this->indicators []=$indicator;
138
	}
139
140
	/*
141
	 * (non-PHPdoc)
142
	 * @see \Ajax\bootstrap\html\base\BaseHtml::compile()
143
	 */
144
	public function compile(JsUtils $js=NULL, &$view=NULL) {
145
		$this->slides [0]->setClass("item active");
146
		$this->indicators [0]->setClass("active");
147
		$this->createControls();
148
		return parent::compile($js, $view);
149
	}
150
151
152
	/* (non-PHPdoc)
153
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
154
	 */
155
	public function fromDatabaseObject($object, $function) {
156
		$this->addImage($function($object));
157
	}
158
}