GoogleCalendarRepository.php
115 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 | const FACTORY = GoogleCalendarFactory::class; |
| 19 | |
| 20 | /** |
| 21 | * @param GoogleCalendar $googleCalendar |
| 22 | * @param int $userId |
| 23 | * |
| 24 | * @return string |
| 25 | * @throws QueryExecutionException |
| 26 | */ |
| 27 | public function add($googleCalendar, $userId) |
| 28 | { |
| 29 | $data = $googleCalendar->toArray(); |
| 30 | |
| 31 | $params = [ |
| 32 | ':userId' => $userId, |
| 33 | ':token' => $data['token'] |
| 34 | ]; |
| 35 | |
| 36 | try { |
| 37 | $statement = $this->connection->prepare( |
| 38 | "INSERT INTO {$this->table} |
| 39 | (`userId`, `token`) |
| 40 | VALUES |
| 41 | (:userId, :token)" |
| 42 | ); |
| 43 | |
| 44 | $res = $statement->execute($params); |
| 45 | |
| 46 | if (!$res) { |
| 47 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 48 | } |
| 49 | } catch (\Exception $e) { |
| 50 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 51 | } |
| 52 | |
| 53 | return $this->connection->lastInsertId(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param GoogleCalendar $googleCalendar |
| 58 | * @param int $id |
| 59 | * |
| 60 | * @return mixed |
| 61 | * @throws QueryExecutionException |
| 62 | */ |
| 63 | public function update($googleCalendar, $id) |
| 64 | { |
| 65 | $data = $googleCalendar->toArray(); |
| 66 | |
| 67 | $params = [ |
| 68 | ':token' => $data['token'], |
| 69 | ':calendarId' => $data['calendarId'], |
| 70 | ':id' => $id |
| 71 | ]; |
| 72 | |
| 73 | try { |
| 74 | $statement = $this->connection->prepare( |
| 75 | "UPDATE {$this->table} |
| 76 | SET `token` = :token, `calendarId` = :calendarId WHERE id = :id" |
| 77 | ); |
| 78 | |
| 79 | $res = $statement->execute($params); |
| 80 | if (!$res) { |
| 81 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 82 | } |
| 83 | |
| 84 | return $res; |
| 85 | } catch (\Exception $e) { |
| 86 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @param $userId |
| 92 | * |
| 93 | * @return mixed |
| 94 | * @throws NotFoundException |
| 95 | * @throws QueryExecutionException |
| 96 | */ |
| 97 | public function getByProviderId($userId) |
| 98 | { |
| 99 | try { |
| 100 | $statement = $this->connection->prepare($this->selectQuery() . " WHERE {$this->table}.userId = :userId"); |
| 101 | $statement->bindParam(':userId', $userId); |
| 102 | $statement->execute(); |
| 103 | $row = $statement->fetch(); |
| 104 | } catch (\Exception $e) { |
| 105 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 106 | } |
| 107 | |
| 108 | if (!$row) { |
| 109 | throw new NotFoundException('Data not found in ' . __CLASS__); |
| 110 | } |
| 111 | |
| 112 | return call_user_func([static::FACTORY, 'create'], $row); |
| 113 | } |
| 114 | } |
| 115 |