Invite   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 91
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B setRequestData() 0 20 5
B execute() 0 34 1
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\Commands;
9
10
use OCA\Chat\Controller\OCH\ApiController;
11
use \OCA\Chat\OCH\ChatAPI;
12
use OCA\Chat\OCH\Data\GetUsers;
13
use \OCA\Chat\OCH\Db\UserOnlineMapper;
14
use \OCA\Chat\OCH\Db\PushMessage;
15
use \OCA\Chat\OCH\Db\PushMessageMapper;
16
use \OCA\Chat\OCH\Exceptions\RequestDataInvalid;
17
use \OCA\Chat\OCH\Db\User;
18
19
class Invite extends ChatAPI {
20
21
	/**
22
	 * @var $pushMessageMapper \OCA\Chat\OCH\Db\PushMessageMapper
23
	 */
24
	private $pushMessageMapper;
25
26
	/**
27
	 * @var $join \OCA\Chat\OCH\Db\UserOnlineMapper
28
	 */
29
	private $join;
30
31
	/**
32
	 * @var $getUsers \OCA\Chat\OCH\Data\GetUsers
33
	 */
34
	private $getUsers;
35
36
	public function __construct(
37
		PushMessageMapper $pushMessageMapper,
38
		Join $join,
39
		GetUsers $getUsers
40
	){
41
		$this->pushMessageMapper = $pushMessageMapper;
42
		$this->join = $join;
43
		$this->getUsers = $getUsers;
44
	}
45
46
47
	/*
48
	 * @param $requestData['user'] String user id of the client
49
	 * @param $requestData['session_id'] String session_id of the client
50
	 * @param $requestData['timestamp'] Int timestamp when the command was send
51
	 * @param $requestData['conv_id'] String id of the conversation
52
	 * @param $requestData['user_to_invite'] String id of the user which need to be invited
53
	*/
54
	public function setRequestData(array $requestData){
55
56
		if(empty($requestData['conv_id'])){
57
			throw new RequestDataInvalid(ApiController::NO_SESSION_ID);
58
		}
59
60
		if(empty($requestData['user_to_invite'])){
61
			throw new RequestDataInvalid(ApiController::NO_USER_TO_INVITE);
62
		}
63
64
		if($requestData['user']['id'] === $requestData['user_to_invite']){
65
			throw new RequestDataInvalid(ApiController::USER_EQUAL_TO_USER_TO_INVITE);
66
		}
67
68
		if(!in_array($requestData['user_to_invite']['id'], \OCP\User::getUsers())){
69
			throw new RequestDataInvalid(ApiController::USER_TO_INVITE_NOT_OC_USER);
70
		}
71
72
		$this->requestData = $requestData;
73
	}
74
75
	public function execute(){
76
77
		// add the user to thx	e conv
78
		// this is done by executing the join command
79
		$requestData = array(
80
			"user" => $this->requestData['user_to_invite'],
81
			"conv_id" => $this->requestData['conv_id']
82
		);
83
		$this->join->setRequestData($requestData);
84
		$this->join->execute();
85
86
		$command = json_encode(array(
87
			"type" => "invite",
88
			"data" => array(
89
				"user" => $this->requestData['user'],
90
				"conv_id" => $this->requestData['conv_id'],
91
				"user_to_invite" => $this->requestData['user_to_invite']
92
			)
93
		));
94
95
		$this->pushMessageMapper->createForAllSessionsOfAUser(
96
			$this->requestData['user_to_invite']['id'],
97
			$this->requestData['user']['id'],
98
			$command
99
		);
100
101
		$this->getUsers->setRequestData(array("conv_id" => $this->requestData['conv_id']));
102
		$users = $this->getUsers->execute();
103
		$users = $users['users'];
104
105
		return array(
106
			"users" => $users
107
		);
108
	}
109
}
110