LabelMapper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 26.87 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 1
dl 18
loc 67
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteLabelAssignments() 6 6 1
A deleteLabelAssignmentsForCard() 6 6 1
A isOwner() 6 6 1
A findBoardId() 0 4 1
A __construct() 0 3 1
A findAll() 0 4 1
A delete() 0 6 1
A findAssignedLabelsForCard() 0 4 1
A findAssignedLabelsForBoard() 0 6 1
A getAssignedLabelsForBoard() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *  
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *  
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *  
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *  
22
 */
23
24
namespace OCA\Deck\Db;
25
26
use OCP\IDBConnection;
27
28
29
class LabelMapper extends DeckMapper implements IPermissionMapper {
30
31
	public function __construct(IDBConnection $db) {
32
		parent::__construct($db, 'deck_labels', '\OCA\Deck\Db\Label');
33
	}
34
35
	public function findAll($boardId, $limit = null, $offset = null) {
36
		$sql = 'SELECT * FROM `*PREFIX*deck_labels` WHERE `board_id` = ? ORDER BY `id`';
37
		return $this->findEntities($sql, [$boardId], $limit, $offset);
38
	}
39
40
	public function delete(\OCP\AppFramework\Db\Entity $entity) {
41
		// delete assigned labels
42
		$this->deleteLabelAssignments($entity->getId());
43
		// delete label
44
		return parent::delete($entity);
45
	}
46
47
	public function findAssignedLabelsForCard($cardId, $limit = null, $offset = null) {
48
		$sql = 'SELECT l.* FROM `*PREFIX*deck_assigned_labels` as al INNER JOIN *PREFIX*deck_labels as l ON l.id = al.label_id WHERE `card_id` = ? ORDER BY l.id';
49
		return $this->findEntities($sql, [$cardId], $limit, $offset);
50
	}
51
	public function findAssignedLabelsForBoard($boardId, $limit = null, $offset = null) {
52
		$sql = "SELECT c.id as card_id, l.id as id, l.title as title, l.color as color FROM oc_deck_cards as c " .
53
			" INNER JOIN oc_deck_assigned_labels as al ON al.card_id = c.id INNER JOIN oc_deck_labels as l ON  al.label_id = l.id WHERE board_id=? ORDER BY l.id";
54
		$entities = $this->findEntities($sql, [$boardId], $limit, $offset);
55
		return $entities;
56
	}
57
58
	public function getAssignedLabelsForBoard($boardId) {
59
		$labels = $this->findAssignedLabelsForBoard($boardId);
60
		$result = array();
61
		foreach ($labels as $label) {
62
			if (!array_key_exists($label->getCardId(), $result)) {
63
				$result[$label->getCardId()] = array();
64
			}
65
			$result[$label->getCardId()][] = $label;
66
		}
67
		return $result;
68
	}
69
70 View Code Duplication
	public function deleteLabelAssignments($labelId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
		$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE label_id = ?';
72
		$stmt = $this->db->prepare($sql);
73
		$stmt->bindParam(1, $labelId, \PDO::PARAM_INT);
74
		$stmt->execute();
75
	}
76
77 View Code Duplication
	public function deleteLabelAssignmentsForCard($cardId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
		$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE card_id = ?';
79
		$stmt = $this->db->prepare($sql);
80
		$stmt->bindParam(1, $cardId, \PDO::PARAM_INT);
81
		$stmt->execute();
82
	}
83
84 View Code Duplication
	public function isOwner($userId, $labelId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
		$sql = 'SELECT owner FROM `*PREFIX*deck_boards` WHERE `id` IN (SELECT board_id FROM `*PREFIX*deck_labels` WHERE id = ?)';
86
		$stmt = $this->execute($sql, [$labelId]);
87
		$row = $stmt->fetch();
88
		return ($row['owner'] === $userId);
89
	}
90
91
	public function findBoardId($labelId) {
92
		$entity = $this->find($labelId);
93
		return $entity->getBoardId();
94
	}
95
}
96