-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathEpisodeActionSaver.php
136 lines (113 loc) · 5.18 KB
/
EpisodeActionSaver.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
<?php
declare(strict_types=1);
namespace OCA\GPodderSync\Core\EpisodeAction;
use DateTime;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionEntity;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionWriter;
use OCP\DB\Exception;
class EpisodeActionSaver
{
private EpisodeActionRepository $episodeActionRepository;
private EpisodeActionWriter $episodeActionWriter;
private EpisodeActionReader $episodeActionReader;
private const DATETIME_FORMAT = 'Y-m-d\TH:i:s';
public function __construct(
EpisodeActionRepository $episodeActionRepository,
EpisodeActionWriter $episodeActionWriter,
EpisodeActionReader $episodeActionReader
)
{
$this->episodeActionRepository = $episodeActionRepository;
$this->episodeActionWriter = $episodeActionWriter;
$this->episodeActionReader = $episodeActionReader;
}
public function saveEpisodeActions(array $episodeActionsArray, string $userId): array
{
$episodeActions = $this->episodeActionReader->fromArray($episodeActionsArray);
$episodeActionEntities = [];
foreach ($episodeActions as $episodeAction) {
$episodeActionEntity = $this->hydrateEpisodeActionEntity($episodeAction, $userId);
try {
$episodeActionEntities[] = $this->episodeActionWriter->save($episodeActionEntity);
} catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$episodeActionEntities[] = $this->updateEpisodeAction($episodeActionEntity, $userId);
}
}
}
return $episodeActionEntities;
}
private function convertTimestampToUnixEpoch(string $timestamp): string
{
return DateTime::createFromFormat(self::DATETIME_FORMAT, $timestamp)
->format("U");
}
private function updateEpisodeAction(
EpisodeActionEntity $episodeActionEntity,
string $userId
): EpisodeActionEntity
{
$episodeActionToUpdate = $this->findEpisodeActionToUpdate($episodeActionEntity, $userId);
$episodeActionEntity->setId($episodeActionToUpdate->getId());
$this->ensureGuidDoesNotGetNulledWithOldData($episodeActionToUpdate, $episodeActionEntity);
try {
return $this->episodeActionWriter->update($episodeActionEntity);
} catch (Exception $exception) {
if ($exception->getReason() === Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$this->deleteConflictingEpisodeAction($episodeActionEntity, $userId);
}
}
return $this->episodeActionWriter->update($episodeActionEntity);
}
private function ensureGuidDoesNotGetNulledWithOldData(EpisodeAction $episodeActionToUpdate, EpisodeActionEntity $episodeActionEntity): void
{
$existingGuid = $episodeActionToUpdate->getGuid();
if ($existingGuid !== null && $episodeActionEntity->getGuid() === null) {
$episodeActionEntity->setGuid($existingGuid);
}
}
private function hydrateEpisodeActionEntity(EpisodeAction $episodeAction, string $userId): EpisodeActionEntity
{
$episodeActionEntity = new EpisodeActionEntity();
$episodeActionEntity->setPodcast($episodeAction->getPodcast());
$episodeActionEntity->setEpisode($episodeAction->getEpisode());
$episodeActionEntity->setGuid($episodeAction->getGuid());
$episodeActionEntity->setAction($episodeAction->getAction());
$episodeActionEntity->setPosition($episodeAction->getPosition());
$episodeActionEntity->setStarted($episodeAction->getStarted());
$episodeActionEntity->setTotal($episodeAction->getTotal());
$episodeActionEntity->setTimestampEpoch($this->convertTimestampToUnixEpoch($episodeAction->getTimestamp()));
$episodeActionEntity->setUserId($userId);
return $episodeActionEntity;
}
private function findEpisodeActionToUpdate(EpisodeActionEntity $episodeActionEntity, string $userId): ?EpisodeAction
{
$episodeAction = null;
if ($episodeActionEntity->getGuid() !== null) {
$episodeAction = $this->episodeActionRepository->findByGuid(
$episodeActionEntity->getGuid(),
$userId
);
}
if ($episodeAction === null) {
$episodeAction = $this->episodeActionRepository->findByEpisodeUrl(
$episodeActionEntity->getEpisode(),
$userId
);
}
return $episodeAction;
}
/**
* @param EpisodeActionEntity $episodeActionEntity
* @param string $userId
* @return void
*/
private function deleteConflictingEpisodeAction(EpisodeActionEntity $episodeActionEntity, string $userId): void
{
$collidingEpisodeActionId = $this->episodeActionRepository->findByEpisodeUrl($episodeActionEntity->getEpisode(), $userId)->getId();
if ($collidingEpisodeActionId !== $episodeActionEntity->getId()) {
$this->episodeActionRepository->deleteEpisodeActionByEpisodeUrl($episodeActionEntity->getEpisode(), $userId);
}
}
}