AppController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A contacts() 0 4 1
A __construct() 0 14 1
B index() 0 27 2
A addContact() 0 21 3
A removeContact() 0 8 2
B initVar() 0 25 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\Controller;
9
10
use \OCP\AppFramework\Controller;
11
use \OCP\IRequest;
12
use \OCP\AppFramework\Http\JSONResponse;
13
use \OCP\AppFramework\Http\TemplateResponse;
14
use \OCA\Chat\App\Chat;
15
use \OCP\Contacts\IManager;
16
use \OCP\IConfig;
17
use \OCA\Chat\OCH\Commands\Greet;
18
19
class AppController extends Controller {
20
21
	/**
22
	 * @var Chat OCA\Chat\App\Chat;
23
	 */
24
	private $app;
25
26
	/**
27
	 * @var \OCP\IConfig
28
	 */
29
	private $config;
30
31
	/**
32
	 * @var \OCP\Contacts\IManager
33
	 */
34
	private $cm;
35
36
	/**
37
	 * @var \OCA\Chat\OCH\Commands\Greet
38
	 */
39
	private $greet;
40
41
	public function __construct(
42
		$appName,
43
		IRequest $request,
44
		Chat $app,
45
		IManager $cm,
46
		IConfig $config,
47
		Greet $greet
48
	){
49
		parent::__construct($appName, $request);
50
		$this->app = $app;
51
		$this->cm = $cm;
52
		$this->config = $config;
53
		$this->greet = $greet;
54
	}
55
56
	/**
57
	 * @NoCSRFRequired
58
	 * @NoAdminRequired
59
	 * @return TemplateResponse
60
	 */
61
	public function index() {
62
		session_write_close();
63
		$this->greet->setRequestData(array(
64
			"timestamp" => time(),
65
			"user" => $this->app->getCurrentUser(),
66
		));
67
		$sessionId = $this->greet->execute();
68
		$contacts = $this->app->getContacts();
69
		$backends = $this->app->getBackends();
70
		$backendsToArray = array();
71
		foreach($backends as $backend){
72
			$backendsToArray[$backend->getId()] = $backend->toArray();
73
		}
74
		$initConvs = $this->app->getInitConvs();
75
		$params = array(
76
			"initvar" => json_encode(array(
77
				"contacts" => $contacts['contacts'],
78
				"contactsList" => $contacts['contactsList'],
79
				"contactsObj" => $contacts['contactsObj'],
80
				"backends" => $backendsToArray,
81
				"initConvs" => $initConvs,
82
				"sessionId" => $sessionId['session_id'],
83
				"avatars_enabled" => $this->config->getSystemValue('enable_avatars', true)
84
			)),
85
 		);
86
		return new TemplateResponse($this->appName, 'main', $params);
87
	}
88
89
	/**
90
	 * @NoAdminRequired
91
	 * @return JSONResponse
92
	 */
93
	public function contacts(){
94
		session_write_close();
95
		return new JSONResponse($this->app->getContacts());
96
	}
97
98
	/**
99
	 * @NoAdminRequired
100
	 * @return JSONResponse
101
	 */
102
	public function addContact($contacts){
103
104
		$addressbooks = $this->cm->getAddressBooks();
105
		$key = array_search('Contacts', $addressbooks);
106
107
		// Create contacts
108
		$ids = array();
109
		foreach ($contacts as $contact){
110
			$r = $this->cm->createOrUpdate($contact, $key);
111
			$ids[] = $r->getId();
112
		}
113
114
		// Return just created contacts as contacts which can be used by the Chat app
115
		$contacts =  $this->app->getContacts();
116
		$newContacts = array();
117
		foreach ($ids as $id){
118
			$newContacts[$id] = $contacts['contactsObj'][$id];
119
		}
120
121
		return $newContacts;
122
	}
123
124
	/**
125
	 * @NoAdminRequired
126
	 * @return JSONResponse
127
	 */
128
	public function removeContact($contacts){
129
		// Create contacts
130
		$ids = array();
0 ignored issues
show
Unused Code introduced by
$ids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
		foreach ($contacts as $contact){
132
			$this->cm->delete($contact, 'local:1');
133
		}
134
135
	}
136
137
138
	/**
139
	 * @NoAdminRequired
140
	 * @return JSONResponse
141
	 */
142
	public function initVar(){
143
		session_write_close();
144
		$this->greet->setRequestData(array(
145
			"timestamp" => time(),
146
			"user" => $this->app->getCurrentUser(),
147
		));
148
		$sessionId = $this->greet->execute();
149
150
		$contacts = $this->app->getContacts();
151
		$backends = $this->app->getBackends();
152
		$backendsToArray = array();
153
		foreach($backends as $backend){
154
			$backendsToArray[$backend->getId()] = $backend->toArray();
155
		}
156
		$initConvs = $this->app->getInitConvs();
157
		return array(
158
			"contacts" => $contacts['contacts'],
159
			"contactsList" => $contacts['contactsList'],
160
			"contactsObj" => $contacts['contactsObj'],
161
			"backends" => $backendsToArray,
162
			"initConvs" => $initConvs,
163
			"sessionId" => $sessionId['session_id'],
164
			"avatars_enabled" => $this->config->getSystemValue('enable_avatars', true)
165
		);
166
	}
167
168
}
169