Passed
Push — master ( 3d3642...978047 )
by Christoph
18:32 queued 12s
created
apps/dav/lib/CardDAV/Converter.php 1 patch
Indentation   +120 added lines, -120 removed lines patch added patch discarded remove patch
@@ -36,124 +36,124 @@
 block discarded – undo
36 36
 use Sabre\VObject\Property\Text;
37 37
 
38 38
 class Converter {
39
-	/** @var IAccountManager */
40
-	private $accountManager;
41
-	private IUserManager $userManager;
42
-
43
-	public function __construct(IAccountManager $accountManager,
44
-		IUserManager $userManager) {
45
-		$this->accountManager = $accountManager;
46
-		$this->userManager = $userManager;
47
-	}
48
-
49
-	public function createCardFromUser(IUser $user): ?VCard {
50
-		$userProperties = $this->accountManager->getAccount($user)->getAllProperties();
51
-
52
-		$uid = $user->getUID();
53
-		$cloudId = $user->getCloudId();
54
-		$image = $this->getAvatarImage($user);
55
-
56
-		$vCard = new VCard();
57
-		$vCard->VERSION = '3.0';
58
-		$vCard->UID = $uid;
59
-
60
-		$publish = false;
61
-
62
-		foreach ($userProperties as $property) {
63
-			if ($property->getName() !== IAccountManager::PROPERTY_AVATAR && empty($property->getValue())) {
64
-				continue;
65
-			}
66
-
67
-			$scope = $property->getScope();
68
-			// Do not write private data to the system address book at all
69
-			if ($scope === IAccountManager::SCOPE_PRIVATE || empty($scope)) {
70
-				continue;
71
-			}
72
-
73
-			$publish = true;
74
-			switch ($property->getName()) {
75
-				case IAccountManager::PROPERTY_DISPLAYNAME:
76
-					$vCard->add(new Text($vCard, 'FN', $property->getValue(), ['X-NC-SCOPE' => $scope]));
77
-					$vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()), ['X-NC-SCOPE' => $scope]));
78
-					break;
79
-				case IAccountManager::PROPERTY_AVATAR:
80
-					if ($image !== null) {
81
-						$vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType(), ['X-NC-SCOPE' => $scope]]);
82
-					}
83
-					break;
84
-				case IAccountManager::COLLECTION_EMAIL:
85
-				case IAccountManager::PROPERTY_EMAIL:
86
-					$vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER', 'X-NC-SCOPE' => $scope]));
87
-					break;
88
-				case IAccountManager::PROPERTY_WEBSITE:
89
-					$vCard->add(new Text($vCard, 'URL', $property->getValue(), ['X-NC-SCOPE' => $scope]));
90
-					break;
91
-				case IAccountManager::PROPERTY_PHONE:
92
-					$vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'VOICE', 'X-NC-SCOPE' => $scope]));
93
-					break;
94
-				case IAccountManager::PROPERTY_ADDRESS:
95
-					$vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER', 'X-NC-SCOPE' => $scope]));
96
-					break;
97
-				case IAccountManager::PROPERTY_TWITTER:
98
-					$vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER', 'X-NC-SCOPE' => $scope]));
99
-					break;
100
-				case IAccountManager::PROPERTY_ORGANISATION:
101
-					$vCard->add(new Text($vCard, 'ORG', $property->getValue(), ['X-NC-SCOPE' => $scope]));
102
-					break;
103
-				case IAccountManager::PROPERTY_ROLE:
104
-					$vCard->add(new Text($vCard, 'TITLE', $property->getValue(), ['X-NC-SCOPE' => $scope]));
105
-					break;
106
-			}
107
-		}
108
-
109
-		// Local properties
110
-		$managers = $user->getManagerUids();
111
-		// X-MANAGERSNAME only allows a single value, so we take the first manager
112
-		if (isset($managers[0])) {
113
-			$displayName = $this->userManager->getDisplayName($managers[0]);
114
-			// Only set the manager if a user object is found
115
-			if ($displayName !== null) {
116
-				$vCard->add(new Text($vCard, 'X-MANAGERSNAME', $displayName, [
117
-					'uid' => $managers[0],
118
-					'X-NC-SCOPE' => IAccountManager::SCOPE_LOCAL,
119
-				]));
120
-			}
121
-		}
122
-
123
-		if ($publish && !empty($cloudId)) {
124
-			$vCard->add(new Text($vCard, 'CLOUD', $cloudId));
125
-			$vCard->validate();
126
-			return $vCard;
127
-		}
128
-
129
-		return null;
130
-	}
131
-
132
-	public function splitFullName(string $fullName): array {
133
-		// Very basic western style parsing. I'm not gonna implement
134
-		// https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
135
-
136
-		$elements = explode(' ', $fullName);
137
-		$result = ['', '', '', '', ''];
138
-		if (count($elements) > 2) {
139
-			$result[0] = implode(' ', array_slice($elements, count($elements) - 1));
140
-			$result[1] = $elements[0];
141
-			$result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2));
142
-		} elseif (count($elements) === 2) {
143
-			$result[0] = $elements[1];
144
-			$result[1] = $elements[0];
145
-		} else {
146
-			$result[0] = $elements[0];
147
-		}
148
-
149
-		return $result;
150
-	}
151
-
152
-	private function getAvatarImage(IUser $user): ?IImage {
153
-		try {
154
-			return $user->getAvatarImage(-1);
155
-		} catch (Exception $ex) {
156
-			return null;
157
-		}
158
-	}
39
+    /** @var IAccountManager */
40
+    private $accountManager;
41
+    private IUserManager $userManager;
42
+
43
+    public function __construct(IAccountManager $accountManager,
44
+        IUserManager $userManager) {
45
+        $this->accountManager = $accountManager;
46
+        $this->userManager = $userManager;
47
+    }
48
+
49
+    public function createCardFromUser(IUser $user): ?VCard {
50
+        $userProperties = $this->accountManager->getAccount($user)->getAllProperties();
51
+
52
+        $uid = $user->getUID();
53
+        $cloudId = $user->getCloudId();
54
+        $image = $this->getAvatarImage($user);
55
+
56
+        $vCard = new VCard();
57
+        $vCard->VERSION = '3.0';
58
+        $vCard->UID = $uid;
59
+
60
+        $publish = false;
61
+
62
+        foreach ($userProperties as $property) {
63
+            if ($property->getName() !== IAccountManager::PROPERTY_AVATAR && empty($property->getValue())) {
64
+                continue;
65
+            }
66
+
67
+            $scope = $property->getScope();
68
+            // Do not write private data to the system address book at all
69
+            if ($scope === IAccountManager::SCOPE_PRIVATE || empty($scope)) {
70
+                continue;
71
+            }
72
+
73
+            $publish = true;
74
+            switch ($property->getName()) {
75
+                case IAccountManager::PROPERTY_DISPLAYNAME:
76
+                    $vCard->add(new Text($vCard, 'FN', $property->getValue(), ['X-NC-SCOPE' => $scope]));
77
+                    $vCard->add(new Text($vCard, 'N', $this->splitFullName($property->getValue()), ['X-NC-SCOPE' => $scope]));
78
+                    break;
79
+                case IAccountManager::PROPERTY_AVATAR:
80
+                    if ($image !== null) {
81
+                        $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType(), ['X-NC-SCOPE' => $scope]]);
82
+                    }
83
+                    break;
84
+                case IAccountManager::COLLECTION_EMAIL:
85
+                case IAccountManager::PROPERTY_EMAIL:
86
+                    $vCard->add(new Text($vCard, 'EMAIL', $property->getValue(), ['TYPE' => 'OTHER', 'X-NC-SCOPE' => $scope]));
87
+                    break;
88
+                case IAccountManager::PROPERTY_WEBSITE:
89
+                    $vCard->add(new Text($vCard, 'URL', $property->getValue(), ['X-NC-SCOPE' => $scope]));
90
+                    break;
91
+                case IAccountManager::PROPERTY_PHONE:
92
+                    $vCard->add(new Text($vCard, 'TEL', $property->getValue(), ['TYPE' => 'VOICE', 'X-NC-SCOPE' => $scope]));
93
+                    break;
94
+                case IAccountManager::PROPERTY_ADDRESS:
95
+                    $vCard->add(new Text($vCard, 'ADR', $property->getValue(), ['TYPE' => 'OTHER', 'X-NC-SCOPE' => $scope]));
96
+                    break;
97
+                case IAccountManager::PROPERTY_TWITTER:
98
+                    $vCard->add(new Text($vCard, 'X-SOCIALPROFILE', $property->getValue(), ['TYPE' => 'TWITTER', 'X-NC-SCOPE' => $scope]));
99
+                    break;
100
+                case IAccountManager::PROPERTY_ORGANISATION:
101
+                    $vCard->add(new Text($vCard, 'ORG', $property->getValue(), ['X-NC-SCOPE' => $scope]));
102
+                    break;
103
+                case IAccountManager::PROPERTY_ROLE:
104
+                    $vCard->add(new Text($vCard, 'TITLE', $property->getValue(), ['X-NC-SCOPE' => $scope]));
105
+                    break;
106
+            }
107
+        }
108
+
109
+        // Local properties
110
+        $managers = $user->getManagerUids();
111
+        // X-MANAGERSNAME only allows a single value, so we take the first manager
112
+        if (isset($managers[0])) {
113
+            $displayName = $this->userManager->getDisplayName($managers[0]);
114
+            // Only set the manager if a user object is found
115
+            if ($displayName !== null) {
116
+                $vCard->add(new Text($vCard, 'X-MANAGERSNAME', $displayName, [
117
+                    'uid' => $managers[0],
118
+                    'X-NC-SCOPE' => IAccountManager::SCOPE_LOCAL,
119
+                ]));
120
+            }
121
+        }
122
+
123
+        if ($publish && !empty($cloudId)) {
124
+            $vCard->add(new Text($vCard, 'CLOUD', $cloudId));
125
+            $vCard->validate();
126
+            return $vCard;
127
+        }
128
+
129
+        return null;
130
+    }
131
+
132
+    public function splitFullName(string $fullName): array {
133
+        // Very basic western style parsing. I'm not gonna implement
134
+        // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
135
+
136
+        $elements = explode(' ', $fullName);
137
+        $result = ['', '', '', '', ''];
138
+        if (count($elements) > 2) {
139
+            $result[0] = implode(' ', array_slice($elements, count($elements) - 1));
140
+            $result[1] = $elements[0];
141
+            $result[2] = implode(' ', array_slice($elements, 1, count($elements) - 2));
142
+        } elseif (count($elements) === 2) {
143
+            $result[0] = $elements[1];
144
+            $result[1] = $elements[0];
145
+        } else {
146
+            $result[0] = $elements[0];
147
+        }
148
+
149
+        return $result;
150
+    }
151
+
152
+    private function getAvatarImage(IUser $user): ?IImage {
153
+        try {
154
+            return $user->getAvatarImage(-1);
155
+        } catch (Exception $ex) {
156
+            return null;
157
+        }
158
+    }
159 159
 }
Please login to merge, or discard this patch.