episodeActionRepository = $episodeActionRepository; $this->userId = $UserId ?? ''; $this->episodeActionSaver = $episodeActionSaver; $this->request = $request; } /** * * @NoAdminRequired * @NoCSRFRequired * * @return JSONResponse */ public function create(): JSONResponse { $episodeActionsArray = $this->filterEpisodesFromRequestParams($this->request->getParams()); $episodeActionsArray = $this->filterOnlyPlays($episodeActionsArray); $this->episodeActionSaver->saveEpisodeActions($episodeActionsArray, $this->userId); return new JSONResponse(["timestamp" => time()]); } /** * @NoAdminRequired * @NoCSRFRequired * * @param int $since * @return JSONResponse */ public function list(int $since = 0): JSONResponse { $episodeActions = $this->episodeActionRepository->findAll($since, $this->userId); $untypedEpisodeActionData = []; foreach ($episodeActions as $episodeAction) { $untypedEpisodeActionData[] = $episodeAction->toArray(); } return new JSONResponse([ "actions" => $untypedEpisodeActionData, "timestamp" => time() ]); } /** * @param array $data * @return array $episodeActionsArray */ public function filterEpisodesFromRequestParams(array $data): array { return array_filter($data, "is_numeric", ARRAY_FILTER_USE_KEY); } /** * @param array $data * @return array $episodeActionsArray */ private function filterOnlyPlays(array $data): array { return array_filter($data, fn($ep) => isset($ep['action']) && strtolower($ep['action']) === 'play'); } }