APIv1::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 7
crap 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
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
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\Activity\Controller;
23
24
use OCA\Activity\CurrentUser;
25
use OCA\Activity\Data;
26
use OCA\Activity\GroupHelper;
27
use OCA\Activity\PlainTextParser;
28
use OCA\Activity\UserSettings;
29
use OCP\AppFramework\Http\DataResponse;
30
use OCP\AppFramework\OCSController;
31
use OCP\IRequest;
32
33
class APIv1 extends OCSController {
34
35
	/** @var Data */
36
	protected $data;
37
38
	/** @var GroupHelper */
39
	protected $groupHelper;
40
41
	/** @var UserSettings */
42
	protected $userSettings;
43
44
	/** @var PlainTextParser */
45
	protected $parser;
46
47
	/** @var CurrentUser */
48
	protected $currentUser;
49
50
	/**
51
	 * @param string $appName
52
	 * @param IRequest $request
53
	 * @param Data $data
54
	 * @param GroupHelper $groupHelper
55
	 * @param UserSettings $userSettings
56
	 * @param PlainTextParser $parser
57
	 * @param CurrentUser $currentUser
58
	 */
59 8
	public function __construct($appName,
60
								IRequest $request,
61
								Data $data,
62
								GroupHelper $groupHelper,
63
								UserSettings $userSettings,
64
								PlainTextParser $parser,
65
								CurrentUser $currentUser) {
66 8
		parent::__construct($appName, $request);
67
68 8
		$this->data = $data;
69 8
		$this->userSettings = $userSettings;
70 8
		$this->groupHelper = $groupHelper;
71 8
		$this->parser = $parser;
72 8
		$this->currentUser = $currentUser;
73 8
	}
74
75
	/**
76
	 * @NoAdminRequired
77
	 *
78
	 * @param int $start
79
	 * @param int $count
80
	 * @return DataResponse
81
	 */
82 5
	public function get($start = 0, $count = 30) {
83 5
		if ($start !== 0) {
84 2
			$start = $this->getSinceFromOffset($start);
85
		}
86
87 5
		$activities = $this->data->get(
88 5
			$this->groupHelper,
89 5
			$this->userSettings,
90 5
			$this->currentUser->getUID(), $start, $count, 'desc', 'all'
91
		);
92
93 5
		$entries = array();
94 5
		foreach($activities['data'] as $entry) {
95 4
			$entries[] = array(
96 4
				'id' => $entry['activity_id'],
97 4
				'subject' => $entry['subject'],
98 4
				'message' => $entry['message'],
99 4
				'file' => $entry['object_name'],
100 4
				'link' => $entry['link'],
101 4
				'date' => date('c', $entry['timestamp']),
102
			);
103
		}
104
105 5
		return new DataResponse($entries);
106
	}
107
108
	/**
109
	 * @param int $offset
110
	 * @return int
111
	 */
112 2
	protected function getSinceFromOffset($offset) {
113 2
		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
114 2
		$query->select('activity_id')
115 2
			->from('activity')
116 2
			->where($query->expr()->eq('affecteduser', $query->createNamedParameter($this->currentUser->getUID())))
117 2
			->orderBy('activity_id', 'desc')
118 2
			->setFirstResult($offset - 1)
119 2
			->setMaxResults(1);
120
121 2
		$result = $query->execute();
122 2
		$row = $result->fetch();
123 2
		$result->closeCursor();
124
125 2
		if ($row) {
126 1
			return (int) $row['activity_id'];
127
		}
128
129 1
		return 0;
130
	}
131
}
132