Completed
Pull Request — master (#8)
by Joas
78:41 queued 76:37
created

PageController::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 8
Bugs 1 Features 3
Metric Value
c 8
b 1
f 3
dl 0
loc 18
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 12
nc 2
nop 3
crap 6
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\AnnouncementCenter\Controller;
23
24
use OCA\AnnouncementCenter\Manager;
25
use OCP\AppFramework\Http;
26
use OCP\AppFramework\Http\JSONResponse;
27
use OCP\AppFramework\Http\TemplateResponse;
28
use OCP\AppFramework\Http\Response;
29
use OCP\AppFramework\Controller;
30
use OCP\BackgroundJob\IJobList;
31
use OCP\IDBConnection;
32
use OCP\IGroupManager;
33
use OCP\IL10N;
34
use OCP\IRequest;
35
use OCP\IUser;
36
use OCP\IUserManager;
37
use OCP\Notification\IManager as INotificationManager;
38
39
class PageController extends Controller {
40
	/** @var int */
41
	const PAGE_LIMIT = 5;
42
43
	/** @var INotificationManager */
44
	protected $notificationManager;
45
46
	/** @var IJobList */
47
	protected $jobList;
48
49
	/** @var IDBConnection */
50
	private $connection;
51
52
	/** @var IGroupManager */
53
	private $groupManager;
54
55
	/** @var IUserManager */
56
	private $userManager;
57
58
	/** @var IL10N */
59
	private $l;
60
61
	/** @var Manager */
62
	private $manager;
63
64
	/** @var string */
65
	private $userId;
66
67
	/**
68
	 * @param string $AppName
69
	 * @param IRequest $request
70
	 * @param IDBConnection $connection
71
	 * @param IGroupManager $groupManager
72
	 * @param IUserManager $userManager
73
	 * @param IJobList $jobList
74
	 * @param INotificationManager $notificationManager
75
	 * @param IL10N $l
76
	 * @param Manager $manager
77
	 * @param string $UserId
78
	 */
79 14
	public function __construct($AppName, IRequest $request, IDBConnection $connection, IGroupManager $groupManager, IUserManager $userManager, IJobList $jobList, INotificationManager $notificationManager, IL10N $l, Manager $manager, $UserId) {
80 14
		parent::__construct($AppName, $request);
81
82 14
		$this->connection = $connection;
83 14
		$this->groupManager = $groupManager;
84 14
		$this->userManager = $userManager;
85 14
		$this->jobList = $jobList;
86 14
		$this->notificationManager = $notificationManager;
87 14
		$this->l = $l;
88 14
		$this->manager = $manager;
89 14
		$this->userId = $UserId;
90 14
	}
91
92
	/**
93
	 * @NoAdminRequired
94
	 * @NoCSRFRequired
95
	 *
96
	 * @param int $offset
97
	 * @return JSONResponse
98
	 */
99 6
	public function get($offset = 0) {
100 6
		$rows = $this->manager->getAnnouncements(self::PAGE_LIMIT, $offset);
101
102 6
		$announcements = [];
103 6
		foreach ($rows as $row) {
104 3
			$displayName = $row['author'];
105 3
			$user = $this->userManager->get($displayName);
106 3
			if ($user instanceof IUser) {
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
107 1
				$displayName = $user->getDisplayName();
108 1
			}
109
110 3
			$announcements[] = [
111 3
				'id'		=> $row['id'],
112 3
				'author'	=> $displayName,
113 3
				'author_id'	=> $row['author'],
114 3
				'time'		=> $row['time'],
115 3
				'subject'	=> $row['subject'],
116 3
				'message'	=> $row['message'],
117
			];
118 6
		}
119
120 6
		return new JSONResponse($announcements);
121
	}
122
123
	/**
124
	 * @param string $subject
125
	 * @param string $message
126
	 * @param string[] $groups
127
	 * @return JSONResponse
128
	 */
129
	public function add($subject, $message, array $groups) {
130
		$timeStamp = time();
131
		try {
132
			$announcement = $this->manager->announce($subject, $message, $this->userId, $timeStamp, $groups);
133
		} catch (\InvalidArgumentException $e) {
134
			return new JSONResponse(
135
				['error' => (string)$this->l->t('The subject is too long or empty')],
136
				Http::STATUS_BAD_REQUEST
137
			);
138
		}
139
140
		$this->jobList->add('OCA\AnnouncementCenter\BackgroundJob', ['id' => $announcement['id']]);
141
142
		$announcement['author_id'] = $announcement['author'];
143
		$announcement['author'] = $this->userManager->get($announcement['author_id'])->getDisplayName();
144
145
		return new JSONResponse($announcement);
146
	}
147
148
	/**
149
	 * @param int $id
150
	 * @return Response
151
	 */
152 2
	public function delete($id) {
153 2
		$this->manager->delete($id);
154
155 2
		$notification = $this->notificationManager->createNotification();
156 2
		$notification->setApp('announcementcenter')
157 2
			->setObject('announcement', $id);
158 2
		$this->notificationManager->markProcessed($notification);
159
160 2
		return new Response();
161
	}
162
163
	/**
164
	 * @NoAdminRequired
165
	 * @NoCSRFRequired
166
	 *
167
	 * @return TemplateResponse
168
	 */
169 2
	public function index() {
170 2
		return new TemplateResponse('announcementcenter', 'main', [
171 2
			'is_admin'	=> $this->groupManager->isAdmin($this->userId),
172 2
		]);
173
	}
174
}
175