Completed
Pull Request — master (#8)
by Joas
04:31
created

PageController::get()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3

Importance

Changes 8
Bugs 1 Features 3
Metric Value
c 8
b 1
f 3
dl 0
loc 24
ccs 19
cts 19
cp 1
rs 8.9713
cc 3
eloc 17
nc 3
nop 1
crap 3
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 3
				'groups'	=> $row['groups'],
118
			];
119 6
		}
120
121 6
		return new JSONResponse($announcements);
122
	}
123
124
	/**
125
	 * @param string $subject
126
	 * @param string $message
127
	 * @param string[] $groups
128
	 * @return JSONResponse
129
	 */
130 3
	public function add($subject, $message, array $groups) {
131 3
		$timeStamp = time();
132
		try {
133 3
			$announcement = $this->manager->announce($subject, $message, $this->userId, $timeStamp, $groups);
134 3
		} catch (\InvalidArgumentException $e) {
135 2
			return new JSONResponse(
136 2
				['error' => (string)$this->l->t('The subject is too long or empty')],
137
				Http::STATUS_BAD_REQUEST
138 2
			);
139
		}
140
141 1
		$this->jobList->add('OCA\AnnouncementCenter\BackgroundJob', ['id' => $announcement['id']]);
142
143 1
		$announcement['author_id'] = $announcement['author'];
144 1
		$announcement['author'] = $this->userManager->get($announcement['author_id'])->getDisplayName();
145
146 1
		return new JSONResponse($announcement);
147
	}
148
149
	/**
150
	 * @param int $id
151
	 * @return Response
152
	 */
153 2
	public function delete($id) {
154 2
		$this->manager->delete($id);
155
156 2
		$notification = $this->notificationManager->createNotification();
157 2
		$notification->setApp('announcementcenter')
158 2
			->setObject('announcement', $id);
159 2
		$this->notificationManager->markProcessed($notification);
160
161 2
		return new Response();
162
	}
163
164
	/**
165
	 * @NoAdminRequired
166
	 * @NoCSRFRequired
167
	 *
168
	 * @return TemplateResponse
169
	 */
170 2
	public function index() {
171 2
		return new TemplateResponse('announcementcenter', 'main', [
172 2
			'is_admin'	=> $this->groupManager->isAdmin($this->userId),
173 2
		]);
174
	}
175
}
176