Passed
Push — master ( 3aa3d7...7e3d5d )
by Christoph
31:24 queued 14:57
created
apps/contactsinteraction/lib/Listeners/ContactInteractionListener.php 2 patches
Indentation   +127 added lines, -127 removed lines patch added patch discarded remove patch
@@ -44,131 +44,131 @@
 block discarded – undo
44 44
 
45 45
 class ContactInteractionListener implements IEventListener {
46 46
 
47
-	use TTransactional;
48
-
49
-	private RecentContactMapper $mapper;
50
-	private CardSearchDao $cardSearchDao;
51
-	private IUserManager $userManager;
52
-	private IDBConnection $dbConnection;
53
-	private ITimeFactory $timeFactory;
54
-	private IL10N $l10n;
55
-	private LoggerInterface $logger;
56
-
57
-	public function __construct(RecentContactMapper $mapper,
58
-								CardSearchDao $cardSearchDao,
59
-								IUserManager $userManager,
60
-								IDBConnection $connection,
61
-								ITimeFactory $timeFactory,
62
-								IL10N $l10nFactory,
63
-								LoggerInterface $logger) {
64
-		$this->mapper = $mapper;
65
-		$this->cardSearchDao = $cardSearchDao;
66
-		$this->userManager = $userManager;
67
-		$this->dbConnection = $connection;
68
-		$this->timeFactory = $timeFactory;
69
-		$this->l10n = $l10nFactory;
70
-		$this->logger = $logger;
71
-	}
72
-
73
-	public function handle(Event $event): void {
74
-		if (!($event instanceof ContactInteractedWithEvent)) {
75
-			return;
76
-		}
77
-
78
-		if ($event->getUid() === null && $event->getEmail() === null && $event->getFederatedCloudId() === null) {
79
-			$this->logger->warning("Contact interaction event has no user identifier set");
80
-			return;
81
-		}
82
-
83
-		if ($event->getUid() !== null && $event->getUid() === $event->getActor()->getUID()) {
84
-			$this->logger->info("Ignoring contact interaction with self");
85
-			return;
86
-		}
87
-
88
-		$this->atomic(function () use ($event) {
89
-			$uid = $event->getUid();
90
-			$email = $event->getEmail();
91
-			$federatedCloudId = $event->getFederatedCloudId();
92
-			$existing = $this->mapper->findMatch(
93
-				$event->getActor(),
94
-				$uid,
95
-				$email,
96
-				$federatedCloudId
97
-			);
98
-			if (!empty($existing)) {
99
-				$now = $this->timeFactory->getTime();
100
-				foreach ($existing as $c) {
101
-					$c->setLastContact($now);
102
-					$this->mapper->update($c);
103
-				}
104
-
105
-				return;
106
-			}
107
-
108
-			$contact = new RecentContact();
109
-			$contact->setActorUid($event->getActor()->getUID());
110
-			if ($uid !== null) {
111
-				$contact->setUid($uid);
112
-			}
113
-			if ($email !== null) {
114
-				$contact->setEmail($email);
115
-			}
116
-			if ($federatedCloudId !== null) {
117
-				$contact->setFederatedCloudId($federatedCloudId);
118
-			}
119
-			$contact->setLastContact($this->timeFactory->getTime());
120
-
121
-			$copy = $this->cardSearchDao->findExisting(
122
-				$event->getActor(),
123
-				$uid,
124
-				$email,
125
-				$federatedCloudId
126
-			);
127
-			if ($copy !== null) {
128
-				try {
129
-					$parsed = Reader::read($copy, Reader::OPTION_FORGIVING);
130
-					$parsed->CATEGORIES = $this->l10n->t('Recently contacted');
131
-					$contact->setCard($parsed->serialize());
132
-				} catch (Throwable $e) {
133
-					$this->logger->warning(
134
-						'Could not parse card to add recent category: ' . $e->getMessage(),
135
-						[
136
-							'exception' => $e,
137
-						]);
138
-					$contact->setCard($copy);
139
-				}
140
-			} else {
141
-				$contact->setCard($this->generateCard($contact));
142
-			}
143
-			$this->mapper->insert($contact);
144
-		}, $this->dbConnection);
145
-	}
146
-
147
-	private function getDisplayName(?string $uid): ?string {
148
-		if ($uid === null) {
149
-			return null;
150
-		}
151
-		if (($user = $this->userManager->get($uid)) === null) {
152
-			return null;
153
-		}
154
-
155
-		return $user->getDisplayName();
156
-	}
157
-
158
-	private function generateCard(RecentContact $contact): string {
159
-		$props = [
160
-			'URI' => UUIDUtil::getUUID(),
161
-			'FN' => $this->getDisplayName($contact->getUid()) ?? $contact->getEmail() ?? $contact->getFederatedCloudId(),
162
-			'CATEGORIES' => $this->l10n->t('Recently contacted'),
163
-		];
164
-
165
-		if ($contact->getEmail() !== null) {
166
-			$props['EMAIL'] = $contact->getEmail();
167
-		}
168
-		if ($contact->getFederatedCloudId() !== null) {
169
-			$props['CLOUD'] = $contact->getFederatedCloudId();
170
-		}
171
-
172
-		return (new VCard($props))->serialize();
173
-	}
47
+    use TTransactional;
48
+
49
+    private RecentContactMapper $mapper;
50
+    private CardSearchDao $cardSearchDao;
51
+    private IUserManager $userManager;
52
+    private IDBConnection $dbConnection;
53
+    private ITimeFactory $timeFactory;
54
+    private IL10N $l10n;
55
+    private LoggerInterface $logger;
56
+
57
+    public function __construct(RecentContactMapper $mapper,
58
+                                CardSearchDao $cardSearchDao,
59
+                                IUserManager $userManager,
60
+                                IDBConnection $connection,
61
+                                ITimeFactory $timeFactory,
62
+                                IL10N $l10nFactory,
63
+                                LoggerInterface $logger) {
64
+        $this->mapper = $mapper;
65
+        $this->cardSearchDao = $cardSearchDao;
66
+        $this->userManager = $userManager;
67
+        $this->dbConnection = $connection;
68
+        $this->timeFactory = $timeFactory;
69
+        $this->l10n = $l10nFactory;
70
+        $this->logger = $logger;
71
+    }
72
+
73
+    public function handle(Event $event): void {
74
+        if (!($event instanceof ContactInteractedWithEvent)) {
75
+            return;
76
+        }
77
+
78
+        if ($event->getUid() === null && $event->getEmail() === null && $event->getFederatedCloudId() === null) {
79
+            $this->logger->warning("Contact interaction event has no user identifier set");
80
+            return;
81
+        }
82
+
83
+        if ($event->getUid() !== null && $event->getUid() === $event->getActor()->getUID()) {
84
+            $this->logger->info("Ignoring contact interaction with self");
85
+            return;
86
+        }
87
+
88
+        $this->atomic(function () use ($event) {
89
+            $uid = $event->getUid();
90
+            $email = $event->getEmail();
91
+            $federatedCloudId = $event->getFederatedCloudId();
92
+            $existing = $this->mapper->findMatch(
93
+                $event->getActor(),
94
+                $uid,
95
+                $email,
96
+                $federatedCloudId
97
+            );
98
+            if (!empty($existing)) {
99
+                $now = $this->timeFactory->getTime();
100
+                foreach ($existing as $c) {
101
+                    $c->setLastContact($now);
102
+                    $this->mapper->update($c);
103
+                }
104
+
105
+                return;
106
+            }
107
+
108
+            $contact = new RecentContact();
109
+            $contact->setActorUid($event->getActor()->getUID());
110
+            if ($uid !== null) {
111
+                $contact->setUid($uid);
112
+            }
113
+            if ($email !== null) {
114
+                $contact->setEmail($email);
115
+            }
116
+            if ($federatedCloudId !== null) {
117
+                $contact->setFederatedCloudId($federatedCloudId);
118
+            }
119
+            $contact->setLastContact($this->timeFactory->getTime());
120
+
121
+            $copy = $this->cardSearchDao->findExisting(
122
+                $event->getActor(),
123
+                $uid,
124
+                $email,
125
+                $federatedCloudId
126
+            );
127
+            if ($copy !== null) {
128
+                try {
129
+                    $parsed = Reader::read($copy, Reader::OPTION_FORGIVING);
130
+                    $parsed->CATEGORIES = $this->l10n->t('Recently contacted');
131
+                    $contact->setCard($parsed->serialize());
132
+                } catch (Throwable $e) {
133
+                    $this->logger->warning(
134
+                        'Could not parse card to add recent category: ' . $e->getMessage(),
135
+                        [
136
+                            'exception' => $e,
137
+                        ]);
138
+                    $contact->setCard($copy);
139
+                }
140
+            } else {
141
+                $contact->setCard($this->generateCard($contact));
142
+            }
143
+            $this->mapper->insert($contact);
144
+        }, $this->dbConnection);
145
+    }
146
+
147
+    private function getDisplayName(?string $uid): ?string {
148
+        if ($uid === null) {
149
+            return null;
150
+        }
151
+        if (($user = $this->userManager->get($uid)) === null) {
152
+            return null;
153
+        }
154
+
155
+        return $user->getDisplayName();
156
+    }
157
+
158
+    private function generateCard(RecentContact $contact): string {
159
+        $props = [
160
+            'URI' => UUIDUtil::getUUID(),
161
+            'FN' => $this->getDisplayName($contact->getUid()) ?? $contact->getEmail() ?? $contact->getFederatedCloudId(),
162
+            'CATEGORIES' => $this->l10n->t('Recently contacted'),
163
+        ];
164
+
165
+        if ($contact->getEmail() !== null) {
166
+            $props['EMAIL'] = $contact->getEmail();
167
+        }
168
+        if ($contact->getFederatedCloudId() !== null) {
169
+            $props['CLOUD'] = $contact->getFederatedCloudId();
170
+        }
171
+
172
+        return (new VCard($props))->serialize();
173
+    }
174 174
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
 			return;
86 86
 		}
87 87
 
88
-		$this->atomic(function () use ($event) {
88
+		$this->atomic(function() use ($event) {
89 89
 			$uid = $event->getUid();
90 90
 			$email = $event->getEmail();
91 91
 			$federatedCloudId = $event->getFederatedCloudId();
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 					$contact->setCard($parsed->serialize());
132 132
 				} catch (Throwable $e) {
133 133
 					$this->logger->warning(
134
-						'Could not parse card to add recent category: ' . $e->getMessage(),
134
+						'Could not parse card to add recent category: '.$e->getMessage(),
135 135
 						[
136 136
 							'exception' => $e,
137 137
 						]);
Please login to merge, or discard this patch.