FoldersController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 4
crap 1
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Thomas Imbreckx <[email protected]>
6
 * @author Thomas Müller <[email protected]>
7
 *
8
 * Mail
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Mail\Controller;
25
26
use OCP\IRequest;
27
use OCA\Mail\Service\AccountService;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Db\DoesNotExistException;
30
use OCP\AppFramework\Http\JSONResponse;
31
use OCP\AppFramework\Http;
32
33
class FoldersController extends Controller {
34
35
	/** @var AccountService */
36
	private $accountService;
37
38
	/**
39
	 * @var string
40
	 */
41
	private $currentUserId;
42
43
	/**
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param AccountService $accountService
47
	 * @param $UserId
48
	 */
49 11
	public function __construct($appName, IRequest $request, AccountService $accountService, $UserId) {
50 11
		parent::__construct($appName, $request);
51 11
		$this->accountService = $accountService;
52 11
		$this->currentUserId = $UserId;
53 11
	}
54
55
	/**
56
	 * @NoAdminRequired
57
	 * @NoCSRFRequired
58
	 * @param int $accountId
59
	 */
60 2
	public function index($accountId) {
61 2
		$account = $this->accountService->find($this->currentUserId, $accountId);
62 2
		$json = $account->getListArray();
63
64 2
		$folders = array_filter($json['folders'], function($folder){
65 1
			return is_null($folder['parent']);
66 2
		});
67 2
		foreach($json['folders'] as $folder) {
68 1
			if (is_null($folder['parent'])) {
69 1
				continue;
70
			}
71
			$parentId = $folder['parent'];
72
			foreach($folders as &$parent) {
73
				if($parent['id'] === $parentId) {
74
					if (!isset($parent['folders'])) {
75
						$parent['folders'] = [];
76
					}
77
					$parent['folders'][] = $folder;
78
					break;
79
				}
80
			}
81 2
		}
82
83 2
		$json['folders'] = array_values($folders);
84 2
		return new JSONResponse($json);
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 * @NoCSRFRequired
90
	 */
91 1
	public function show() {
92 1
		$response = new JSONResponse();
93 1
		$response->setStatus(Http::STATUS_NOT_IMPLEMENTED);
94 1
		return $response;
95
	}
96
97
	/**
98
	 * @NoAdminRequired
99
	 */
100 1
	public function update() {
101 1
		$response = new JSONResponse();
102 1
		$response->setStatus(Http::STATUS_NOT_IMPLEMENTED);
103 1
		return $response;
104
	}
105
106
	/**
107
	 * @NoAdminRequired
108
	 * @param int $accountId
109
	 * @param string $folderId
110
	 * @return JSONResponse
111
	 */
112 2
	public function destroy($accountId, $folderId) {
113
		try {
114 2
			$account = $this->accountService->find($this->currentUserId, $accountId);
115 1
			$imap = $account->getImapConnection();
116 1
			$imap->deleteMailbox($folderId);
117
118 1
			return new JSONResponse();
119 1
		} catch (DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
120 1
			return new JSONResponse(null, 404);
121
		}
122
	}
123
124
	/**
125
	 * @NoAdminRequired
126
	 * @param int $accountId
127
	 * @param string $mailbox
128
	 */
129 2
	public function create($accountId, $mailbox) {
130
		try {
131 2
			$account = $this->accountService->find($this->currentUserId, $accountId);
132 2
			$imap = $account->getImapConnection();
133
134
			// TODO: read http://tools.ietf.org/html/rfc6154
135 2
			$imap->createMailbox($mailbox);
136
137 1
			return new JSONResponse([
138
				'data' => [
139
					'id' => $mailbox
140 1
				]
141 1
				], Http::STATUS_CREATED);
142 1
		} catch (\Horde_Imap_Client_Exception $e) {
0 ignored issues
show
Bug introduced by
The class Horde_Imap_Client_Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
143 1
			$response = new JSONResponse();
144 1
			$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
145 1
			return $response;
146
		} catch (DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
147
			return new JSONResponse();
148
		}
149
	}
150
151
	/**
152
	 * @NoAdminRequired
153
	 * @param $accountId
154
	 * @param $folders
155
	 * @return JSONResponse
156
	 */
157
	public function detectChanges($accountId, $folders) {
158
		try {
159
			$query = [];
160
			foreach($folders as $folder) {
161
				$folderId = base64_decode($folder['id']);
162
				$parts = explode('/', $folderId);
163
				if (count($parts) > 1 && $parts[1] === 'FLAGGED') {
164
					continue;
165
				}
166
				if (isset($folder['error'])) {
167
					continue;
168
				}
169
				$query[$folderId] = $folder;
170
			}
171
			$account = $this->accountService->find($this->currentUserId, $accountId);
172
			$mailBoxes = $account->getChangedMailboxes($query);
173
174
			return new JSONResponse($mailBoxes);
175
		} catch (\Horde_Imap_Client_Exception $e) {
0 ignored issues
show
Bug introduced by
The class Horde_Imap_Client_Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
176
			$response = new JSONResponse();
177
			$response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
178
			return $response;
179
		} catch (DoesNotExistException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\Db\DoesNotExistException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
180
			return new JSONResponse();
181
		}
182
	}
183
}
184