Passed
Push — master ( 6522b6...8c95d3 )
by
unknown
04:16 queued 01:06
created

SharedFolders   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 45
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A GetSharedFolders() 0 6 2
A __construct() 0 21 3
A GetSharedFoldersRaw() 0 7 2
1
<?php
2
/*
3
 * SPDX-License-Identifier: AGPL-3.0-only
4
 * SPDX-FileCopyrightText: Copyright 2022 grommunio GmbH
5
 *
6
 * Cache shared public folder hierarchy information.
7
 */
8
9
class SharedFolders extends InterProcessData {
10
	public static $instance = false;
11
12
	public static function GetSharedFolders() {
13
		if (!self::$instance) {
14
			self::$instance = new SharedFolders();
15
		}
16
17
		return self::$instance->GetSharedFoldersRaw();
18
	}
19
20
	/**
21
	 * Constructor.
22
	 */
23
	public function __construct() {
24
		// initialize super parameters
25
		$this->allocate = 0;
26
		$this->localpart = "undefined";
0 ignored issues
show
Bug Best Practice introduced by
The property localpart does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
		$this->mainDomain = "undefined";
0 ignored issues
show
Bug Best Practice introduced by
The property mainDomain does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
		if (preg_match('/^([*+!.&#$|\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$/i', nsp_getuserinfo(Request::GetUser())['primary_email'], $matches)) {
29
			$this->localpart = $matches[1];
30
			$this->mainDomain = $matches[2];
31
		}
32
		$this->type = "grommunio-sync:sharedfolders-" . $this->mainDomain;
33
		parent::__construct();
34
		// initialize params
35
		$this->initializeParams();
36
37
		// get cached data from redis
38
		$shared = $this->getDeviceUserData($this->type, $this->localpart, -1);
39
40
		// no shared folder in redis for this user, get them from the public folder
41
		if (empty($shared)) {
42
			$shared = GSync::GetBackend()->GetPublicSyncEnabledFolders();
43
			$this->setDeviceUserData($this->type, $shared, $this->localpart, -1);
44
		}
45
	}
46
47
	public function GetSharedFoldersRaw() {
48
		// get cached data from redis
49
		$shared = $this->getDeviceUserData($this->type, $this->localpart, -1);
50
		if (!$shared) {
51
			return [];
52
		}
53
		return $shared;
54
	}
55
}
56