-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapcard.php
146 lines (120 loc) · 5.16 KB
/
Swapcard.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php declare (strict_types=1);
namespace Entrydo\Infrastructure\Swapcard;
use Doctrine\ORM\EntityManagerInterface;
use Entrydo\Domain\Model\Ticket\Ticket;
use Entrydo\Domain\Model\Ticket\TicketId;
use Entrydo\Domain\Model\Ticket\TicketRepository;
use GuzzleHttp\Exception\ClientException;
use Nette\Utils\Json;
use Nette\Utils\Validators;
use Psr\Log\LoggerInterface;
class Swapcard
{
/** @var TicketRepository */
private $ticketRepository;
/** @var bool */
private $testMode;
/** @var EntityManagerInterface */
private $entityManager;
/** @var LoggerInterface */
private $logger;
private $ignoreDuplicates = TRUE;
/** @var SwapcardFieldSearcher */
private $fieldSearcher;
public function __construct(
bool $testMode,
TicketRepository $ticketRepository,
EntityManagerInterface $entityManager,
LoggerInterface $logger,
SwapcardFieldSearcher $fieldSearcher
){
$this->ticketRepository = $ticketRepository;
$this->testMode = $testMode;
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->fieldSearcher = $fieldSearcher;
}
public function saveTicketAttendee(TicketId $ticketId): ?int
{
$ticket = $this->ticketRepository->ticketOfId($ticketId);
$order = $ticket->ofOrder();
$event = $ticket->variant()->event();
$swapcardApiKey = $event->getSwapcardApiKey();
if ($swapcardApiKey === null) {
return null;
}
if ($ticket->isCanceled() || ($order && ! $order->isPaid()) || $ticket->isProcessedWithSwapcard()) {
$this->logger->debug('Skipped Swapcard pairing - ticket cancelled/not paid/already processed', [
'ticketId' => (string) $ticketId,
]);
return null;
}
if (! Validators::isEmail((string) $ticket->email())) {
$this->processTicket($ticket, null);
$this->logger->debug('Skipped Swapcard pairing - invalid email', [
'ticketId' => (string) $ticketId,
]);
return null;
}
if (! $ticket->firstName() || ! $ticket->lastName() || ! $ticket->email()) {
$this->processTicket($ticket, null);
$this->logger->debug('Skipped Swapcard pairing - missing firstname/lastname/email', [
'ticketId' => (string) $ticketId,
]);
return null;
}
$client = new Client($swapcardApiKey, $this->testMode);
$request = new AttendeeRequest(
$ticket->firstName(),
$ticket->lastName(),
(string) $ticket->email(),
'en',
null,
$this->fieldSearcher->search($ticketId, SwapcardField::JOB_TITLE) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::JOB_TITLE_2) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::COMPANY) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::KEYWORDS) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::PHOTO) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::MOBILE_PHONE) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::LANDLINE_PHONE) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::ADDRESS) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::ZIP_CODE) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::CITY) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::COUNTRY) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::WEBSITE) ?: NULL,
$this->fieldSearcher->search($ticketId, SwapcardField::BIOGRAPHY) ?: NULL,
[
'badge_codes' => [(string) $ticket->code()],
'pin_code' => (string) $ticket->code(),
]
);
try {
$attendeeId = $client->sendAttendeeRequest($request);
}
catch (ClientException $e) {
$response = $e->getResponse();
$attendeeId = null;
if ($response) {
$result = Json::decode($response->getBody()->getContents());
if ($result->status !== 400 || $result->errors[0] !== 'email: A user already exists with this value') {
$this->logger->error('Adding swapcard attendee error', ['exception' => $e, 'ticketId' => (string) $ticket->id()]);
return null;
}
if ($this->ignoreDuplicates === FALSE) {
$attendeeId = $result->data->id;
}
} else {
$this->logger->error('Adding swapcard attendee error', ['exception' => $e, 'ticketId' => (string) $ticket->id()]);
return null;
}
}
$this->processTicket($ticket, $attendeeId);
return $attendeeId;
}
private function processTicket(Ticket $ticket, ?int $attendeeId): void
{
$ticket->processSwapcardAttendee($attendeeId);
$this->ticketRepository->save($ticket);
$this->entityManager->flush();
}
}