Completed
Push — master ( 0876bf...6c1183 )
by
unknown
01:40
created

PageController::get()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 20
cts 20
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 18
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
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
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\AnnouncementCenter\Controller;
25
26
use OCA\AnnouncementCenter\Manager;
27
use OCP\AppFramework\Http;
28
use OCP\AppFramework\Http\JSONResponse;
29
use OCP\AppFramework\Http\TemplateResponse;
30
use OCP\AppFramework\Http\Response;
31
use OCP\AppFramework\Controller;
32
use OCP\BackgroundJob\IJobList;
33
use OCP\IConfig;
34
use OCP\IDBConnection;
35
use OCP\IGroupManager;
36
use OCP\IL10N;
37
use OCP\IRequest;
38
use OCP\IUser;
39
use OCP\IUserManager;
40
use OCP\IUserSession;
41
42
class PageController extends Controller {
43
	/** @var int */
44
	const PAGE_LIMIT = 5;
45
46
	/** @var IJobList */
47
	protected $jobList;
48
49
	/** @var IDBConnection */
50
	protected $connection;
51
52
	/** @var IGroupManager */
53
	protected $groupManager;
54
55
	/** @var IUserManager */
56
	protected $userManager;
57
58
	/** @var IL10N */
59
	protected $l;
60
61
	/** @var Manager */
62
	protected $manager;
63
64
	/** @var IConfig */
65
	protected $config;
66
67
	/** @var IUserSession */
68
	protected $userSession;
69
70
	/**
71
	 * @param string $AppName
72
	 * @param IRequest $request
73
	 * @param IDBConnection $connection
74
	 * @param IGroupManager $groupManager
75
	 * @param IUserManager $userManager
76
	 * @param IJobList $jobList
77
	 * @param IL10N $l
78
	 * @param Manager $manager
79
	 * @param IConfig $config
80
	 * @param IUserSession $userSession
81
	 */
82 24
	public function __construct($AppName,
83
								IRequest $request,
84
								IDBConnection $connection,
85
								IGroupManager $groupManager,
86
								IUserManager $userManager,
87
								IJobList $jobList,
88
								IL10N $l,
89
								Manager $manager,
90
								IConfig $config,
91
								IUserSession $userSession) {
92 24
		parent::__construct($AppName, $request);
93
94 24
		$this->connection = $connection;
95 24
		$this->groupManager = $groupManager;
96 24
		$this->userManager = $userManager;
97 24
		$this->jobList = $jobList;
98 24
		$this->l = $l;
99 24
		$this->manager = $manager;
100 24
		$this->config = $config;
101 24
		$this->userSession = $userSession;
102 24
	}
103
104
	/**
105
	 * @NoAdminRequired
106
	 * @NoCSRFRequired
107
	 *
108
	 * @param int $offset
109
	 * @return JSONResponse
110
	 */
111 6
	public function get($offset = 0) {
112 6
		$rows = $this->manager->getAnnouncements(self::PAGE_LIMIT, $offset);
113
114 6
		$announcements = [];
115 6
		foreach ($rows as $row) {
116 3
			$displayName = $row['author'];
117 3
			$user = $this->userManager->get($displayName);
118 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...
119 1
				$displayName = $user->getDisplayName();
120 1
			}
121
122 3
			$announcements[] = [
123 3
				'id'		=> $row['id'],
124 3
				'author'	=> $displayName,
125 3
				'author_id'	=> $row['author'],
126 3
				'time'		=> $row['time'],
127 3
				'subject'	=> $row['subject'],
128 3
				'message'	=> $row['message'],
129 3
				'groups'	=> $row['groups'],
130 3
				'comments'	=> $row['comments'],
131
			];
132 6
		}
133
134 6
		return new JSONResponse($announcements);
135
	}
136
137
	/**
138
	 * @NoAdminRequired
139
	 *
140
	 * @param string $subject
141
	 * @param string $message
142
	 * @param string[] $groups,
0 ignored issues
show
Documentation introduced by
There is no parameter named $groups,. Did you maybe mean $groups?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
143
	 * @param bool $activities
144
	 * @param bool $notifications
145
	 * @param bool $comments
146
	 * @return JSONResponse
147
	 */
148 8
	public function add($subject, $message, array $groups, $activities, $notifications, $comments) {
149 8
		if (!$this->manager->checkIsAdmin()) {
150 1
			return new JSONResponse(
151 1
				['message' => 'Logged in user must be an admin'],
152
				Http::STATUS_FORBIDDEN
153 1
			);
154
		}
155
156 7
		$timeStamp = time();
157
		try {
158 7
			$announcement = $this->manager->announce($subject, $message, $this->userSession->getUser()->getUID(), $timeStamp, $groups, $comments);
159 7
		} catch (\InvalidArgumentException $e) {
160 2
			return new JSONResponse(
161 2
				['error' => (string)$this->l->t('The subject is too long or empty')],
162
				Http::STATUS_BAD_REQUEST
163 2
			);
164
		}
165
166 5
		if ($activities || $notifications) {
167 3
			$this->jobList->add('OCA\AnnouncementCenter\BackgroundJob', [
168 3
				'id' => $announcement['id'],
169 3
				'activities' => $activities,
170 3
				'notifications' => $notifications,
171 3
			]);
172 3
		}
173
174 5
		$announcement['author_id'] = $announcement['author'];
175 5
		$announcement['author'] = $this->userManager->get($announcement['author_id'])->getDisplayName();
176
177 5
		return new JSONResponse($announcement);
178
	}
179
180
	/**
181
	 * @NoAdminRequired
182
	 *
183
	 * @param int $id
184
	 * @return Response
185
	 */
186 2
	public function delete($id) {
187 2
		if (!$this->manager->checkIsAdmin()) {
188 1
			return new JSONResponse(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OCP\AppFrame...ttp::STATUS_FORBIDDEN); (OCP\AppFramework\Http\JSONResponse) is incompatible with the return type documented by OCA\AnnouncementCenter\C...\PageController::delete of type OCP\AppFramework\Http\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
189 1
				['message' => 'Logged in user must be an admin'],
190
				Http::STATUS_FORBIDDEN
191 1
			);
192
		}
193
194 1
		$this->manager->delete($id);
195
196 1
		return new Response();
197
	}
198
199
	/**
200
	 * @NoAdminRequired
201
	 * @NoCSRFRequired
202
	 *
203
	 * @param int $announcement
204
	 * @return TemplateResponse
205
	 */
206 3
	public function index($announcement = 0) {
207 3
		if ($announcement) {
208
			$this->manager->markNotificationRead($announcement);
209
		}
210
211 3
		return new TemplateResponse('announcementcenter', 'main', [
212 3
			'isAdmin'	=> $this->manager->checkIsAdmin(),
213 3
			'createActivities' => $this->config->getAppValue('announcementcenter', 'create_activities', 'yes') === 'yes',
214 3
			'createNotifications' => $this->config->getAppValue('announcementcenter', 'create_notifications', 'yes') === 'yes',
215 3
			'allowComments' => $this->config->getAppValue('announcementcenter', 'allow_comments', 'yes') === 'yes',
216 3
		]);
217
	}
218
219
	/**
220
	 * @NoAdminRequired
221
	 *
222
	 * @param string $pattern
223
	 * @return JSONResponse
224
	 */
225 3
	public function searchGroups($pattern) {
226 3
		if (!$this->manager->checkIsAdmin()) {
227 1
			return new JSONResponse(
228 1
				['message' => 'Logged in user must be an admin'],
229
				Http::STATUS_FORBIDDEN
230 1
			);
231
		}
232
233 2
		$groups = $this->groupManager->search($pattern, 10);
234 2
		$gids = [];
235 2
		foreach ($groups as $group) {
236 1
			$gids[] = $group->getGID();
237 2
		}
238
239 2
		return new JSONResponse($gids);
240
	}
241
}
242