GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TbSortableAction   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 79
ccs 0
cts 50
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 21 3
A isValidRequest() 0 6 3
C update() 0 31 7
1
<?php
2
/**
3
 *## TbSortableAction class file
4
 *
5
 * @author ruslan fadeev <[email protected]>
6
 */
7
8
/**
9
 *## TbSortableAction CAction Component
10
 *
11
 * It is a component that works in conjunction of TbExtendedGridView widget with sortableRows true. Just attach to the controller you wish to
12
 * make the calls to.
13
 *
14
 * @package booster.actions
15
 */
16
class TbSortableAction extends CAction
17
{
18
	/**
19
	 * @var string the name of the model we are going to toggle values to
20
	 */
21
	public $modelName;
22
23
	/**
24
	 * Widgets run function
25
	 * @throws CHttpException
26
	 */
27
	public function run()
28
	{
29
		if (!$this->isValidRequest())
30
			throw new CHttpException(400, Yii::t('yii', 'Your request is invalid.'));
31
32
		$sortableAttribute = Yii::app()->request->getQuery('sortableAttribute');
33
34
		/** @var $model CActiveRecord */
35
		$model = new $this->modelName;
36
		if (!$model->hasAttribute($sortableAttribute)) {
37
			throw new CHttpException(400, Yii::t(
38
				'yii',
39
				'{attribute} "{value}" is invalid.',
40
				array('{attribute}' => 'sortableAttribute', '{value}' => $sortableAttribute)
41
			));
42
		}
43
44
		$sortOrderData = $_POST['sortOrder'];
45
46
		$this->update($model, $sortableAttribute, $sortOrderData);
47
	}
48
49
	private function isValidRequest()
50
	{
51
		return Yii::app()->request->isPostRequest
52
			&& Yii::app()->request->isAjaxRequest
53
			&& isset($_POST['sortOrder']);
54
	}
55
56
	/**
57
	 * @param CActiveRecord $model
58
	 * @param $sortableAttribute
59
	 * @param $sortOrderData
60
	 *
61
	 * @return string
62
	 */
63
	private function update($model, $sortableAttribute, $sortOrderData)
64
	{
65
		$pk = $model->tableSchema->primaryKey;
66
		$pk_array = array();
67
		if(is_array($pk)) { // composite key
68
			$string_ids = array_keys($sortOrderData);
69
			
70
			$array_ids = array();
71
			foreach ($string_ids as $string_id)
72
				$array_ids[] = explode(',', $string_id);
73
			
74
			foreach ($array_ids as $array_id)
75
				$pk_array[] = array_combine($pk, $array_id);
76
		} else { // normal key
77
			$pk_array = array_keys($sortOrderData);
78
		}
79
		
80
		$models = $model->model()->findAllByPk($pk_array);
81
		$transaction = Yii::app()->db->beginTransaction();
82
		try {
83
			foreach ($models as $model) {
84
				$_key = is_array($pk) ? implode(',', array_values($model->primaryKey)) : $model->primaryKey;
85
				$model->{$sortableAttribute} = $sortOrderData[$_key];
86
				$model->save();
87
			}
88
			$transaction->commit();
89
		}
90
		catch(Exception $e) { // an exception is raised if a query fails
91
			$transaction->rollback();
92
		}
93
	}
94
}
95