-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDoctorSlotsSynchronizer.php
157 lines (134 loc) · 4.26 KB
/
DoctorSlotsSynchronizer.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
147
148
149
150
151
152
153
154
155
156
157
<?php
declare(strict_types=1);
namespace App;
use App\Entity\Doctor;
use App\Entity\Slot;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use JsonException;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
class DoctorSlotsSynchronizer
{
protected const ENDPOINT = 'http://localhost:2137/api/doctors';
protected const USERNAME = 'docplanner';
protected const PASSWORD = 'docplanner';
protected EntityRepository $repository;
protected EntityRepository $slots;
protected Logger $logger;
public function __construct(EntityManagerInterface $em, string $logFile = 'php://stderr')
{
$this->repository = $em->getRepository(Doctor::class);
$this->slots = $em->getRepository(Slot::class);
$this->logger = new Logger('logger', [new StreamHandler($logFile)]);
}
/**
* @throws JsonException
*/
public function synchronizeDoctorSlots(): void
{
$doctors = $this->getJsonDecode($this->getDoctors());
foreach ($doctors as $doctor) {
$name = $this->normalizeName($doctor['name']);
/** @var Doctor $entity */
$entity = $this->repository->find($doctor['id']) ?? new Doctor((string)$doctor['id'], $name);
$entity->setName($name);
$entity->clearError();
$this->save($entity);
foreach ($this->fetchDoctorSlots($doctor['id']) as $slot) {
if (false === $slot) {
$entity->markError();
$this->save($entity);
} else {
$this->save($slot);
}
}
}
}
/**
* @throws JsonException
*/
protected function getJsonDecode(string|bool $json): mixed
{
return json_decode(
json: false === $json ? '' : $json,
associative: true,
depth: 16,
flags: JSON_THROW_ON_ERROR,
);
}
protected function getDoctors(): string
{
return $this->fetchData(self::ENDPOINT);
}
protected function fetchData(string $url): string|false
{
$auth = base64_encode(
sprintf(
'%s:%s',
self::USERNAME,
self::PASSWORD,
),
);
return @file_get_contents(
filename: $url,
context: stream_context_create(
[
'http' => [
'header' => 'Authorization: Basic ' . $auth,
],
],
),
);
}
protected function normalizeName(string $fullName): string
{
[, $surname] = explode(' ', $fullName);
/** @see https://www.youtube.com/watch?v=PUhU3qCf0Nk */
if (0 === stripos($surname, "o'")) {
return ucwords($fullName, ' \'');
}
return ucwords($fullName);
}
protected function save(Doctor|Slot $entity): void
{
$em = $this->repository->createQueryBuilder('alias')->getEntityManager();
$em->persist($entity);
$em->flush();
}
protected function fetchDoctorSlots(int $id): iterable
{
try {
$slots = $this->getJsonDecode($this->getSlots($id));
yield from $this->parseSlots($slots, $id);
} catch (JsonException) {
if ($this->shouldReportErrors()) {
$this->logger->info('Error fetching slots for doctor', ['doctorId' => $id]);
}
yield false;
}
}
protected function getSlots(int $id): string|false
{
return $this->fetchData(self::ENDPOINT . '/' . $id . '/slots');
}
protected function parseSlots(mixed $slots, int $id): iterable
{
foreach ($slots as $slot) {
$start = new DateTime($slot['start']);
$end = new DateTime($slot['end']);
/** @var Slot $entity */
$entity = $this->slots->findOneBy(['doctorId' => $id, 'start' => $start])
?: new Slot($id, $start, $end);
if ($entity->isStale()) {
$entity->setEnd($end);
}
yield $entity;
}
}
protected function shouldReportErrors(): bool
{
return (new DateTime())->format('D') !== 'Sun';
}
}