-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAwardService.php
More file actions
92 lines (84 loc) · 2.79 KB
/
AwardService.php
File metadata and controls
92 lines (84 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace App\Service;
use App\Event\MessageEvent;
use App\Event\ReactionAddedEvent;
use App\Model\Entity\User;
use Cake\ORM\Locator\LocatorAwareTrait;
use Sentry\SentrySdk;
use function Sentry\logger;
use function Sentry\trace_metrics;
class AwardService
{
use LocatorAwareTrait;
/**
* @param \App\Model\Entity\User $fromUser User who did gib the potato.
* @param array<\App\Model\Entity\User> $toUsers Users who will receive the potato.
* @param \App\Event\MessageEvent|\App\Event\ReactionAddedEvent $event The event.
* @return void
*/
public function gib(
User $fromUser,
array $toUsers,
MessageEvent|ReactionAddedEvent $event,
): void {
foreach ($toUsers as $toUser) {
$this->gibToUser(
fromUser: $fromUser,
toUser: $toUser,
event: $event,
);
}
}
/**
* @param \App\Model\Entity\User $fromUser User who did gib the potato.
* @param \App\Model\Entity\User $toUser User who will receive the potato.
* @param \App\Event\MessageEvent|\App\Event\ReactionAddedEvent $event The event.
* @return string created message id
*/
private function gibToUser(
User $fromUser,
User $toUser,
MessageEvent|ReactionAddedEvent $event,
): void {
$messagesTable = $this->fetchTable('Messages');
$message = $messagesTable->newEntity([
'sender_user_id' => $fromUser->id,
'receiver_user_id' => $toUser->id,
'amount' => $event->amount,
'type' => str_replace(':', '', $event->reaction),
], [
'accessibleFields' => [
'sender_user_id' => true,
'receiver_user_id' => true,
'amount' => true,
'type' => true,
],
]);
// Skip ORM validation since User entities are already loaded and guaranteed to exist.
// Database foreign key constraints will enforce referential integrity.
$messagesTable->saveOrFail($message, ['validate' => false]);
$span = SentrySdk::getCurrentHub()->getSpan();
if ($span !== null) {
$span->setData([
'gibpotato.potatoes.given_out' => $event->amount,
'gibpotato.event_type' => $event->type,
]);
}
trace_metrics()->count(
'gibpotato.potatoes.given_out',
(float)$event->amount,
[
'gibpotato.event_type' => $event->type,
],
);
logger()->info(
message: '"%s" gave "%s" %s 🥔',
values: [
$fromUser->slack_name,
$toUser->slack_name,
$event->amount,
],
);
}
}