-
Notifications
You must be signed in to change notification settings - Fork 7
BRND Booking #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
BRND Booking #313
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2b85f26
Initial commit
agnetemoos 45b3f94
Change API endpoint
agnetemoos e90f378
Change secrets settings
agnetemoos 0519fee
Change time formats
agnetemoos 5d60da1
Various bugfixes
agnetemoos 5a27c01
Better data example
agnetemoos ac26287
Update CHANGELOG
agnetemoos a90f6c7
Update CHANGELOG
agnetemoos a56cc6e
Fix markdownlint errors
agnetemoos 64f6dad
Fix psalm errors
agnetemoos 13597d1
Fix psalm errors
agnetemoos fb5d3c0
Correct php-cs-fixer errors
agnetemoos e2b4b67
Correct php-cs-fixer errors
agnetemoos b1126f3
Update src/Feed/BrndFeedType.php
agnetemoos 2ac6e37
Improve error handling
agnetemoos 7a1df72
Validate required fields
agnetemoos f9ae36d
Refactor getData method to improve error handling
agnetemoos b06ca77
Remove trailing white space
agnetemoos 2bba4a9
Improve variable naming
agnetemoos 83d4d81
Update src/Feed/BrndFeedType.php
agnetemoos 3088278
Add LoggerInterface
agnetemoos e1509fa
Fix indentation
agnetemoos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Feed; | ||
|
|
||
| use App\Entity\Tenant\Feed; | ||
| use App\Entity\Tenant\FeedSource; | ||
| use App\Feed\SourceType\Brnd\ApiClient; | ||
| use App\Feed\SourceType\Brnd\SecretsDTO; | ||
| use App\Service\FeedService; | ||
| use Psr\Cache\CacheItemPoolInterface; | ||
| use Psr\Log\LoggerInterface; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| /** | ||
| * Brnd Bookingsystem Feed. | ||
| * | ||
| * @see https://brndapi.brnd.com/swagger/index.html | ||
| */ | ||
| class BrndFeedType implements FeedTypeInterface | ||
| { | ||
| public const int CACHE_TTL = 3600; | ||
|
|
||
| final public const string SUPPORTED_FEED_TYPE = FeedOutputModels::BRND_BOOKING_OUTPUT; | ||
|
|
||
| public function __construct( | ||
| private readonly FeedService $feedService, | ||
| private readonly ApiClient $apiClient, | ||
| private readonly CacheItemPoolInterface $feedsCache, | ||
| private readonly LoggerInterface $logger, | ||
| ) {} | ||
|
|
||
| public function getAdminFormOptions(FeedSource $feedSource): array | ||
| { | ||
| $feedEntryRecipients = $this->feedService->getFeedSourceConfigUrl($feedSource, 'sport-center'); | ||
|
|
||
| return [ | ||
| [ | ||
| 'key' => 'brnd-sport-center-id', | ||
| 'input' => 'input', | ||
| 'type' => 'text', | ||
| 'name' => 'sport_center_id', | ||
| 'label' => 'Sportcenter ID', | ||
| 'formGroupClasses' => 'mb-3', | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function getData(Feed $feed): array | ||
| { | ||
| $result = [ | ||
| 'title' => 'BRND Booking', | ||
| 'bookings' => [], | ||
| ]; | ||
|
|
||
| try { | ||
| $configuration = $feed->getConfiguration(); | ||
| $feedSource = $feed->getFeedSource(); | ||
|
|
||
| if (null == $feedSource) { | ||
| return $result; | ||
| } | ||
|
|
||
| $secrets = new SecretsDTO($feedSource); | ||
|
|
||
| $baseUri = $secrets->apiBaseUri; | ||
| $sportCenterId = $configuration['sport_center_id'] ?? null; | ||
|
|
||
| if ('' === $baseUri || null === $sportCenterId || '' === $sportCenterId) { | ||
| return $result; | ||
| } | ||
|
|
||
| $bookings = $this->apiClient->getInfomonitorBookingsDetails($feedSource, $sportCenterId); | ||
|
|
||
| $result['bookings'] = array_reduce($bookings, function (array $carry, array $booking): array { | ||
| $parsedBooking = $this->parseBrndBooking($booking); | ||
|
|
||
| // Validate that booking has required fields | ||
| if (!empty($parsedBooking['bookingcode']) && !empty($parsedBooking['bookingBy'])) { | ||
| $carry[] = $parsedBooking; | ||
| } | ||
|
|
||
| return $carry; | ||
| }, []); | ||
| } catch (\Throwable $throwable) { | ||
| $this->logger->error($throwable->getMessage()); | ||
| // Silently catch all exceptions and return empty result | ||
| // $result is already initialized with empty bookings array | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| private function parseBrndBooking(array $booking): array | ||
| { | ||
| // Parse start time | ||
| $startDateTime = null; | ||
| if (!empty($booking['dato']) && isset($booking['starttid']) && is_string($booking['starttid'])) { | ||
| try { | ||
| // Trim starttime to 6 digits after dot for microseconds | ||
| $startTimeString = preg_replace('/\.(\d{6})\d+$/', '.$1', $booking['starttid']); | ||
| $dateOnly = substr($booking['dato'], 0, 10); | ||
| $dateTimeString = $dateOnly.' '.$startTimeString; | ||
| $startDateTime = \DateTimeImmutable::createFromFormat('m/d/Y H:i:s.u', $dateTimeString); | ||
| if (false === $startDateTime) { | ||
| $startDateTime = null; | ||
| } | ||
| } catch (\ValueError) { | ||
| $startDateTime = null; | ||
| } | ||
| } | ||
|
|
||
| // Parse end time | ||
| $endDateTime = null; | ||
| if (!empty($booking['dato']) && isset($booking['sluttid']) && is_string($booking['sluttid'])) { | ||
| try { | ||
| $endTimeString = preg_replace('/\.(\d{6})\d+$/', '.$1', $booking['sluttid']); | ||
| $dateOnly = substr($booking['dato'], 0, 10); | ||
| $dateTimeString = $dateOnly.' '.$endTimeString; | ||
| $endDateTime = \DateTimeImmutable::createFromFormat('m/d/Y H:i:s.u', $dateTimeString); | ||
| if (false === $endDateTime) { | ||
| $endDateTime = null; | ||
| } | ||
| } catch (\ValueError) { | ||
| $endDateTime = null; | ||
| } | ||
| } | ||
|
|
||
| return [ | ||
| 'bookingcode' => $booking['ansøgning'] ?? '', | ||
| 'remarks' => $booking['bemærkninger'] ?? '', | ||
| 'startTime' => $startDateTime ? $startDateTime->getTimestamp() : null, | ||
| 'endTime' => $endDateTime ? $endDateTime->getTimestamp() : null, | ||
| 'complex' => $booking['anlæg'] ?? '', | ||
| 'area' => $booking['område'] ?? '', | ||
| 'facility' => $booking['facilitet'] ?? '', | ||
| 'activity' => $booking['aktivitet'] ?? '', | ||
| 'team' => $booking['hold'] ?? '', | ||
| 'status' => $booking['status'] ?? '', | ||
| 'checkIn' => $booking['checK_IN'] ?? '', | ||
| 'bookingBy' => $booking['ansøgt_af'] ?? '', | ||
| 'changingRooms' => $booking['omklædningsrum'] ?? '', | ||
| ]; | ||
| } | ||
|
|
||
| public function getConfigOptions(Request $request, FeedSource $feedSource, string $name): ?array | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| public function getRequiredSecrets(): array | ||
| { | ||
| return [ | ||
| 'api_base_uri' => [ | ||
| 'type' => 'string', | ||
| 'exposeValue' => true, | ||
| ], | ||
| 'company_id' => [ | ||
| 'type' => 'string', | ||
| 'exposeValue' => true, | ||
| ], | ||
| 'api_auth_key' => [ | ||
| 'type' => 'string', | ||
| 'exposeValue' => false, | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function getRequiredConfiguration(): array | ||
| { | ||
| return ['sport_center_id']; | ||
| } | ||
|
|
||
| public function getSupportedFeedOutputType(): string | ||
| { | ||
| return self::SUPPORTED_FEED_TYPE; | ||
| } | ||
|
|
||
| public function getSchema(): array | ||
| { | ||
| return [ | ||
| '$schema' => 'http://json-schema.org/draft-04/schema#', | ||
| 'type' => 'object', | ||
| 'properties' => [ | ||
| 'api_base_uri' => [ | ||
| 'type' => 'string', | ||
| 'format' => 'uri', | ||
| ], | ||
| 'company_id' => [ | ||
| 'type' => 'string', | ||
| ], | ||
| 'api_auth_key' => [ | ||
| 'type' => 'string', | ||
| ], | ||
| ], | ||
| 'required' => ['api_base_uri', 'company_id', 'api_auth_key'], | ||
| ]; | ||
| } | ||
|
|
||
| public static function getIdKey(FeedSource $feedSource): string | ||
| { | ||
| $ulid = $feedSource->getId(); | ||
| assert(null !== $ulid); | ||
|
|
||
| return $ulid->toBase32(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.