OCH   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A getDisplayName() 0 3 1
A getProtocols() 0 3 1
A getDefaultConfig() 0 4 1
A getHelp() 0 3 1
A __construct() 0 18 1
A getInitConvs() 0 6 2
B createInitConvs() 0 64 7
1
<?php
2
3
namespace OCA\Chat\OCH;
4
5
use \OCA\Chat\IBackend;
6
use \OCA\Chat\App\Chat;
7
use \OCA\Chat\AbstractBackend;
8
use \OCA\Chat\OCH\Db\UserMapper;
9
use \OCA\Chat\Db\ConfigMapper;
10
use \OCA\Chat\OCH\Db\AttachmentMapper;
11
use \OCP\IConfig;
12
use \OCA\Chat\OCH\Commands\StartConv;
13
use \OCA\Chat\OCH\Commands\Join;
14
use \OCA\Chat\OCH\Data\Messages;
15
16
class OCH extends AbstractBackend implements IBackend {
17
18
	/**
19
	 * @var $userMapper \OCA\Chat\OCH\Db\UserMapper
20
	 */
21
	private $userMapper;
22
23
	/**
24
	 * @var $attachmentMapper \OCA\Chat\OCH\Db\AttachmentMapper
25
	 */
26
	private $attachmentMapper;
27
28
	/**
29
	 * @var $messages \OCA\Chat\OCH\Data\Messages
30
	 */
31
	private $messages;
32
33
	/**
34
	 * @var $startconv \OCA\Chat\OCH\Commands\StartConv
35
	 */
36
	private $startconv;
37
38
	/**
39
	 * @var $join \OCA\Chat\OCH\Commands\Join
40
	 */
41
	private $join;
42
43
	/**
44
	 * @var $app \OCA\Chat\App\Chat
45
	 */
46
	private $app;
47
48
	public function __construct(
49
		ConfigMapper $configMapper,
50
		IConfig $config,
51
		UserMapper $userMapper,
52
		AttachmentMapper $attachmentMapper,
53
		StartConv $startconv,
54
		Messages $messages,
55
		Join $join,
56
		Chat $app
57
	){
58
		parent::__construct($configMapper, $config);
59
		$this->userMapper = $userMapper;
60
		$this->attachmentMapper = $attachmentMapper;
61
		$this->startconv = $startconv;
62
		$this->messages = $messages;
63
		$this->join = $join;
64
		$this->app = $app;
65
	}
66
67
	public function getId(){
68
		return 'och';
69
	}
70
71
	public function getInitConvs(){
72
		if(count(self::$initConvs) === 0){
73
			$this->createInitConvs();
74
		}
75
		return self::$initConvs;
76
	}
77
78
	private function createInitConvs(){
79
		$initConvs = array();
80
		$convs = $this->userMapper->findByUser($this->app->getUserId());
81
		$usersAllreadyInConv = array();
82
		foreach($convs as $conv){
83
			$users = $this->userMapper->findUsersInConv($conv->getConversationId());
84
			// Find the correct contact for the correct user
85
			$this->messages->setRequestData(array(
86
				"conv_id" => $conv->getConversationId(),
87
				'user' => $this->app->getCurrentUser(),
88
				"limit" => array(0,30)
89
			));
90
			$messages = $this->messages->execute();
91
			$messages = $messages['messages'];
92
93
			$files = $this->attachmentMapper->findRawByConv($conv->getConversationId());
94
			$initConvs[$conv->getConversationId()] = array(
95
				"id" => $conv->getConversationId(),
96
				"users"=> $users,
97
				"backend" => "och",
98
				"messages" => $messages,
99
				"files" => $files
100
			);
101
			if(count($users) === 2){
102
				foreach($users as $user){
103
					if($user !== \OCP\User::getUser()){
104
						$usersAllreadyInConv[] = $user;
105
					}
106
				}
107
			}
108
109
			$this->join->setRequestData(array(
110
				"conv_id" => $conv->getConversationId(),
111
				"user" => $this->app->getCurrentUser(),
112
			));
113
			$this->join->execute();
114
		}
115
116
		$allUsers = \OCP\User::getUsers();
117
		$users = array_diff($allUsers, $usersAllreadyInConv);
118
119
		foreach($users as $user){
120
			if($user !== \OCP\User::getUser()){
121
				$this->startconv->setRequestData(array(
122
					"user" => $this->app->getCurrentUser(),
123
					"user_to_invite" => array(
124
						$this->app->getUserasContact($user),
125
					)
126
				));
127
				$info =  $this->startconv->execute();
128
				$initConvs[$info['conv_id']] = array(
129
					"id" => $info['conv_id'],
130
					"users"=> array(
131
						\OCP\User::getUser(),
132
						$user
133
					),
134
					"backend" => "och",
135
					"messages" => array()
136
				);
137
138
			}
139
		}
140
		self::$initConvs = $initConvs;
141
	}
142
143
	public function getDisplayName(){
144
		return 'ownCloud Chat';
145
	}
146
147
	public function getProtocols(){
148
		return array('x-owncloud-handle');
149
	}
150
151
152
	public function getDefaultConfig(){
153
		return array(
154
		);
155
	}
156
157
	public function getHelp(){
158
		return 'This Chat backend works without configuration. It can be used to chat with other ownCloud users';
159
	}
160
}
161