Test Failed
Push — master ( 66c7a4...c46ff8 )
by
unknown
02:24 queued 12s
created

GrommunioSyncState   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 125
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setState() 0 7 1
A getAppttsref() 0 12 2
A getState() 0 12 2
A getSourcekey() 0 12 2
A rememberAppttsref() 0 7 1
A __construct() 0 14 1
1
<?php
2
/*
3
 * SPDX-License-Identifier: AGPL-3.0-only
4
 * SPDX-FileCopyrightText: Copyright 2016 - 2018 Kopano b.v.
5
 * SPDX-FileCopyrightText: Copyright 2020 grommunio GmbH
6
 *
7
 * Class for handling sync state.
8
 */
9
10
namespace grommunio\DAV;
11
12
class GrommunioSyncState {
13
	private $db;
14
15
	/**
16
	 * Constructor.
17
	 *
18
	 * @param GLogger $logger
19
	 * @param string  $dbstring
20
	 */
21
	public function __construct($logger, $dbstring) {
22
		$this->logger = $logger;
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
		$this->logger->trace("Using db %s", $dbstring);
24
		$this->db = new \PDO($dbstring);
25
26
		$query = "CREATE TABLE IF NOT EXISTS gdav_sync_state (
27
		          id VARCHAR(255), folderid VARCHAR(255), value TEXT,
28
		          PRIMARY KEY (id, folderid));
29
		          CREATE TABLE IF NOT EXISTS gdav_sync_appttsref (
30
		          sourcekey VARCHAR(255), folderid VARCHAR(255), appttsref VARCHAR(255),
31
		          PRIMARY KEY (sourcekey, folderid));
32
		          CREATE INDEX IF NOT EXISTS idx_appttsref ON gdav_sync_appttsref(appttsref);";
33
34
		$this->db->exec($query);
35
	}
36
37
	/**
38
	 * Fetch state information for a folderId (e.g. calenderId) and an id (uuid).
39
	 *
40
	 * @param string $folderid
41
	 * @param string $id
42
	 *
43
	 * @return string
44
	 */
45
	public function getState($folderid, $id) {
46
		$query = "SELECT value FROM gdav_sync_state WHERE folderid = :folderid AND id = :id";
47
		$statement = $this->db->prepare($query);
48
		$statement->bindParam(":folderid", $folderid);
49
		$statement->bindParam(":id", $id);
50
		$statement->execute();
51
		$result = $statement->fetch();
52
		if (!$result) {
53
			return null;
54
		}
55
56
		return $result['value'];
57
	}
58
59
	/**
60
	 * Set state information for a folderId (e.g. calenderId) and an id (uuid).
61
	 * The state information is the sync token for ICS.
62
	 *
63
	 * @param string $folderid
64
	 * @param string $id
65
	 * @param string $value
66
	 */
67
	public function setState($folderid, $id, $value) {
68
		$query = "REPLACE INTO gdav_sync_state (id, folderid, value) VALUES(:id, :folderid, :value)";
69
		$statement = $this->db->prepare($query);
70
		$statement->bindParam(":folderid", $folderid);
71
		$statement->bindParam(":id", $id);
72
		$statement->bindParam(":value", $value);
73
		$statement->execute();
74
	}
75
76
	/**
77
	 * Set the APPTTSREF (custom URL) for a folderId and source key.
78
	 * This is needed for detecting the URL of deleted items reported by ICS.
79
	 *
80
	 * @param string $folderid
81
	 * @param string $sourcekey
82
	 * @param string $appttsref
83
	 */
84
	public function rememberAppttsref($folderid, $sourcekey, $appttsref) {
85
		$query = "REPLACE INTO gdav_sync_appttsref (folderid, sourcekey, appttsref) VALUES(:folderid, :sourcekey, :appttsref)";
86
		$statement = $this->db->prepare($query);
87
		$statement->bindParam(":folderid", $folderid);
88
		$statement->bindParam(":sourcekey", $sourcekey);
89
		$statement->bindParam(":appttsref", $appttsref);
90
		$statement->execute();
91
	}
92
93
	/**
94
	 * Get the APPTTSREF (custom URL) for a folderId and source key.
95
	 * This is needed for detecting the URL of deleted items reported by ICS.
96
	 *
97
	 * @param string $folderid
98
	 * @param string $sourcekey
99
	 *
100
	 * @return string
101
	 */
102
	public function getAppttsref($folderid, $sourcekey) {
103
		$query = "SELECT appttsref FROM gdav_sync_appttsref WHERE folderid = :folderid AND sourcekey = :sourcekey";
104
		$statement = $this->db->prepare($query);
105
		$statement->bindParam(":folderid", $folderid);
106
		$statement->bindParam(":sourcekey", $sourcekey);
107
		$statement->execute();
108
		$result = $statement->fetch();
109
		if (!$result) {
110
			return null;
111
		}
112
113
		return $result['appttsref'];
114
	}
115
116
	/**
117
	 * Get the sourcekey from the saved APPTTSREF (custom URL) and a folderId.
118
	 * This is the last resort when searching for an item in the store fails.
119
	 *
120
	 * @param string $folderid
121
	 * @param string $appttsref
122
	 *
123
	 * @return string
124
	 */
125
	public function getSourcekey($folderid, $appttsref) {
126
		$query = "SELECT sourcekey FROM gdav_sync_appttsref WHERE folderid = :folderid AND appttsref = :appttsref";
127
		$statement = $this->db->prepare($query);
128
		$statement->bindParam(":folderid", $folderid);
129
		$statement->bindParam(":appttsref", $appttsref);
130
		$statement->execute();
131
		$result = $statement->fetch();
132
		if (!$result) {
133
			return null;
134
		}
135
136
		return $result['sourcekey'];
137
	}
138
}
139