PushMessageMapper   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 69
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findAll() 0 4 1
A __construct() 0 5 1
A findBysSessionId() 0 9 2
A createForAllSessionsOfAUser() 0 11 2
A createForAllUsersInConv() 0 14 3
A createForAllSessions() 0 11 2
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
use \OCA\Chat\OCH\Db\UserOnlineMapper;
14
15
class PushMessageMapper extends Mapper {
16
17
	private $USER_ONLINE = '*PREFIX*chat_och_users_online';
0 ignored issues
show
Unused Code introduced by
The property $USER_ONLINE is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
	private $USERS_IN_CONV = '*PREFIX*chat_och_users_in_conversation';
0 ignored issues
show
Unused Code introduced by
The property $USERS_IN_CONV is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
20
	private $userOnlineMapper;
21
	private $userMapper;
22
23
	public function __construct(IDb $api, UserOnlineMapper $userOnlineMapper, UserMapper $userMapper) {
24
		parent::__construct($api, 'chat_och_push_messages'); // tablename is news_feeds
25
		$this->userOnlineMapper = $userOnlineMapper;
26
		$this->userMapper = $userMapper;
27
	}
28
29
	public function findBysSessionId($sessionId){
30
		$sql = 'SELECT * FROM `' . $this->getTableName() . '` ' . 'WHERE `receiver_session_id` = ?';
31
		$feeds =  $this->findEntities($sql, array($sessionId));
32
		if (count($feeds) === 0 ){
33
			throw new DoesNotExistException('');
34
		} else {
35
			return $feeds;
36
		}
37
	}
38
39
	public function createForAllSessionsOfAUser($receiverId, $sender, $command){
40
		$receivers = $this->userOnlineMapper->findByUser($receiverId);
41
		foreach($receivers as $receiver){
42
			$pushMessage = new PushMessage();
43
			$pushMessage->setSender($sender);
44
			$pushMessage->setCommand($command);
45
			$pushMessage->setReceiver($receiver->getUser());
46
			$pushMessage->setReceiverSessionId($receiver->getSessionId());
47
			$this->insert($pushMessage);
48
		}
49
	}
50
51
	public function createForAllUsersInConv($sender, $convId, $command, $exception=null){
52
		$sessions = $this->userMapper->findSessionsByConversation($convId);
53
		foreach($sessions as $session){
54
			if($exception === $session->getUser()){
55
				continue;
56
			}
57
			$pushMessage = new PushMessage();
58
			$pushMessage->setSender($sender);
59
			$pushMessage->setCommand($command);
60
			$pushMessage->setReceiver($session->getUser());
61
			$pushMessage->setReceiverSessionId($session->getSessionId());
62
			$this->insert($pushMessage);
63
		}
64
	}
65
66
	public function createForAllSessions($sender, $command){
67
		$sessions = $this->userOnlineMapper->getAll();
68
		foreach($sessions as $session){
69
			$pushMessage = new PushMessage();
70
			$pushMessage->setSender($sender);
71
			$pushMessage->setCommand($command);
72
			$pushMessage->setReceiver($session->getUser());
73
			$pushMessage->setReceiverSessionId($session->getSessionId());
74
			$this->insert($pushMessage);
75
		}
76
	}
77
78
	public function findAll(){
79
		$sql = 'SELECT * FROM `' . $this->getTableName() . '`';
80
		return $this->findEntities($sql, array());
81
	}
82
83
}