ConversationMapper::existsByUsers()   C
last analyzed

Complexity

Conditions 7
Paths 72

Size

Total Lines 76
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 76
rs 6.5808
cc 7
eloc 31
nc 72
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright (c) 2014, Tobia De Koninck hey--at--ledfan.be
4
 * This file is licensed under the AGPL version 3 or later.
5
 * See the COPYING file.
6
 */
7
8
namespace OCA\Chat\OCH\Db;
9
10
use \OCP\AppFramework\Db\Mapper;
11
use \OCP\IDb;
12
use \OCA\Chat\Db\DoesNotExistException;
13
14
class ConversationMapper extends Mapper {
15
16
	public function __construct(IDb $api) {
17
		parent::__construct($api, 'chat_och_conversations'); // tablename is news_feeds
18
	}
19
20
	public function deleteConversation($conversationID){
21
		$sql = 'DELETE FROM `' . $this->getTableName() . '` WHERE `conversation_id` = ? ';
22
		$this->execute($sql, array($conversationID));
23
	}
24
25
	public function findByConversationId($conversationID){
26
		$sql = 'SELECT * FROM `' . $this->getTableName() . '` ' . 'WHERE `conversation_id` = ?';
27
		return $this->findEntity($sql, array($conversationID));
28
	}
29
30
	public function existsByConvId($id){
31
		$sql = 'SELECT `conversation_id` FROM `' . $this->getTableName() . '` ' . 'WHERE `conversation_id` = ?';
32
		$result = $this->execute($sql, array($id));
33
		if(count($result->fetchAll()) === 1){
34
			return true;
35
		} else {
36
			return false;
37
		}
38
	}
39
40
	public function existsByUsers($users){
41
		$usersCount = count($users);
42
		$sql = <<<SQL
43
			SELECT
44
				DISTINCT c1.conversation_id AS conv_id
45
			FROM
46
				*PREFIX*chat_och_users_in_conversation c1
47
			WHERE EXISTS (
48
				SELECT
49
					1
50
				FROM
51
					*PREFIX*chat_och_users_in_conversation c2
52
				WHERE
53
					c1.conversation_id = c2.conversation_id
54
				AND
55
				 	c2.user = ?
56
			)
57
SQL;
58
59
		for($i = 0; $i < ($usersCount -1); $i++){
60
			$sql .= <<<SQL
61
			AND EXISTS (
62
				SELECT
63
					1
64
				FROM
65
				 	*PREFIX*chat_och_users_in_conversation c2
66
				WHERE
67
					c1.conversation_id = c2.conversation_id
68
				AND
69
					c2.user = ?
70
			)
71
SQL;
72
		}
73
74
		$sql .= <<<SQL
75
			AND NOT EXISTS (
76
				SELECT
77
					1
78
				FROM
79
					*PREFIX*chat_och_users_in_conversation  c2
80
			 	WHERE
81
			 		c1.conversation_id = c2.conversation_id
82
			 	AND
83
			 		c2.user NOT IN (
84
SQL;
85
86
		foreach($users as $key=>$user){
87
			if($key === $usersCount-1){
88
				$sql .= " ?";
89
			} else {
90
				$sql .= " ?,";
91
			}
92
		}
93
		$sql .= <<<SQL
94
				)
95
			)
96
SQL;
97
98
		$params = array();
99
100
		foreach($users as $user){
101
			$params[] = $user;
102
		}
103
104
		foreach($users as $user){
105
			$params[] = $user;
106
		}
107
108
		try{
109
			$result = $this->execute($sql, $params);
110
			$row = $result->fetch();
111
			return $row;
112
		} catch (DoesNotExistException $exception) {
113
			return false;
114
		}
115
	}
116
}