GoogleCalendarRepository.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Google; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Google\GoogleCalendar; |
| 6 | use AmeliaBooking\Domain\Factory\Google\GoogleCalendarFactory; |
| 7 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 8 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 9 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 10 | |
| 11 | /** |
| 12 | * Class GoogleRepository |
| 13 | * |
| 14 | * @package AmeliaBooking\Infrastructure\Repository\Google |
| 15 | */ |
| 16 | class GoogleCalendarRepository extends AbstractRepository |
| 17 | { |
| 18 | public const FACTORY = GoogleCalendarFactory::class; |
| 19 | |
| 20 | /** |
| 21 | * @param GoogleCalendar $googleCalendar |
| 22 | * @param int $userId |
| 23 | * @param array|null $additionalSettings |
| 24 | * |
| 25 | * @return int |
| 26 | * @throws QueryExecutionException |
| 27 | */ |
| 28 | public function add($googleCalendar, $userId, $additionalSettings = null) |
| 29 | { |
| 30 | $data = $googleCalendar->toArray(); |
| 31 | |
| 32 | $params = [ |
| 33 | ':userId' => $userId, |
| 34 | ':token' => $data['token'], |
| 35 | ':calendarId' => $data['calendarId'] |
| 36 | ]; |
| 37 | |
| 38 | $fields = ['userId', 'token', 'calendarId']; |
| 39 | $placeholders = [':userId', ':token', ':calendarId']; |
| 40 | |
| 41 | if ($additionalSettings !== null) { |
| 42 | if (isset($additionalSettings['insertPendingAppointments'])) { |
| 43 | $fields[] = 'insertPendingAppointments'; |
| 44 | $placeholders[] = ':insertPendingAppointments'; |
| 45 | $params[':insertPendingAppointments'] = (int)$additionalSettings['insertPendingAppointments']; |
| 46 | } |
| 47 | |
| 48 | if (isset($additionalSettings['includeBufferTime'])) { |
| 49 | $fields[] = 'includeBufferTime'; |
| 50 | $placeholders[] = ':includeBufferTime'; |
| 51 | $params[':includeBufferTime'] = (int)$additionalSettings['includeBufferTime']; |
| 52 | } |
| 53 | |
| 54 | if (isset($additionalSettings['title'])) { |
| 55 | $fields[] = 'title'; |
| 56 | $placeholders[] = ':title'; |
| 57 | $params[':title'] = is_array($additionalSettings['title']) |
| 58 | ? json_encode($additionalSettings['title']) |
| 59 | : $additionalSettings['title']; |
| 60 | } |
| 61 | |
| 62 | if (isset($additionalSettings['description'])) { |
| 63 | $fields[] = 'description'; |
| 64 | $placeholders[] = ':description'; |
| 65 | $params[':description'] = is_array($additionalSettings['description']) |
| 66 | ? json_encode($additionalSettings['description']) |
| 67 | : $additionalSettings['description']; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | try { |
| 72 | $statement = $this->connection->prepare( |
| 73 | "INSERT INTO {$this->table} |
| 74 | (`" . implode('`, `', $fields) . "`) |
| 75 | VALUES |
| 76 | (" . implode(', ', $placeholders) . ")" |
| 77 | ); |
| 78 | |
| 79 | $statement->execute($params); |
| 80 | } catch (\Exception $e) { |
| 81 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 82 | } |
| 83 | |
| 84 | return $this->connection->lastInsertId(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param GoogleCalendar $googleCalendar |
| 89 | * @param int $id |
| 90 | * |
| 91 | * @return mixed |
| 92 | * @throws QueryExecutionException |
| 93 | */ |
| 94 | public function update($googleCalendar, $id) |
| 95 | { |
| 96 | $data = $googleCalendar->toArray(); |
| 97 | |
| 98 | $params = [ |
| 99 | ':token' => $data['token'], |
| 100 | ':calendarId' => $data['calendarId'], |
| 101 | ':id' => $id |
| 102 | ]; |
| 103 | |
| 104 | try { |
| 105 | $statement = $this->connection->prepare( |
| 106 | "UPDATE {$this->table} |
| 107 | SET `token` = :token, `calendarId` = :calendarId WHERE id = :id" |
| 108 | ); |
| 109 | |
| 110 | $statement->execute($params); |
| 111 | |
| 112 | return true; |
| 113 | } catch (\Exception $e) { |
| 114 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param $userId |
| 120 | * |
| 121 | * @return mixed |
| 122 | * @throws NotFoundException |
| 123 | * @throws QueryExecutionException |
| 124 | */ |
| 125 | public function getByProviderId($userId) |
| 126 | { |
| 127 | try { |
| 128 | $statement = $this->connection->prepare($this->selectQuery() . " WHERE {$this->table}.userId = :userId"); |
| 129 | $statement->bindParam(':userId', $userId); |
| 130 | $statement->execute(); |
| 131 | $row = $statement->fetch(); |
| 132 | } catch (\Exception $e) { |
| 133 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 134 | } |
| 135 | |
| 136 | if (!$row) { |
| 137 | throw new NotFoundException('Data not found in ' . __CLASS__); |
| 138 | } |
| 139 | |
| 140 | return call_user_func([static::FACTORY, 'create'], $row); |
| 141 | } |
| 142 | } |
| 143 |