Completed
Pull Request — master (#362)
by Maxence
02:13
created

GSEventsRequest::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
28
namespace OCA\Circles\Db;
29
30
31
use OCA\Circles\Exceptions\JsonException;
32
use OCA\Circles\Exceptions\ModelException;
33
use OCA\Circles\Model\GlobalScale\GSWrapper;
34
35
36
/**
37
 * Class GSEventsRequest
38
 *
39
 * @package OCA\Circles\Db
40
 */
41
class GSEventsRequest extends GSEventsRequestBuilder {
42
43
44
	/**
45
	 * @param GSWrapper $wrapper
46
	 *
47
	 * @return GSWrapper
48
	 */
49
	public function create(GSWrapper $wrapper): GSWrapper {
50
		$qb = $this->getGSEventsInsertSql();
51
		$qb->setValue('token', $qb->createNamedParameter($wrapper->getToken()))
52
		   ->setValue('event', $qb->createNamedParameter(json_encode($wrapper->getEvent())))
53
		   ->setValue('instance', $qb->createNamedParameter($wrapper->getInstance()))
54
		   ->setValue('severity', $qb->createNamedParameter($wrapper->getSeverity()))
55
		   ->setValue('status', $qb->createNamedParameter($wrapper->getStatus()))
56
		   ->setValue('creation', $qb->createNamedParameter($wrapper->getCreation()));
57
58
		$qb->execute();
59
60
		return $wrapper;
61
	}
62
63
	/**
64
	 * @param GSWrapper $wrapper
65
	 */
66
	public function update(GSWrapper $wrapper): void {
67
		$qb = $this->getGSEventsUpdateSql();
68
		$qb->set('event', $qb->createNamedParameter(json_encode($wrapper->getEvent())))
69
		   ->set('status', $qb->createNamedParameter($wrapper->getStatus()));
70
71
		$this->limitToInstance($qb, $wrapper->getInstance());
72
		$this->limitToToken($qb, $wrapper->getToken());
73
74
		$qb->execute();
75
	}
76
77
78
	/**
79
	 * @param string $token
80
	 *
81
	 * @return GSWrapper[]
82
	 * @throws JsonException
83
	 * @throws ModelException
84
	 */
85
	public function getByToken(string $token): array {
86
		$qb = $this->getGSEventsSelectSql();
87
		$this->limitToToken($qb, $token);
88
89
		$wrappers = [];
90
		$cursor = $qb->execute();
91
		while ($data = $cursor->fetch()) {
92
			$wrappers[] = $this->parseGSeventsSelectSql($data);
93
		}
94
		$cursor->closeCursor();
95
96
		return $wrappers;
97
	}
98
99
}
100
101