PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.1.3
Booking for Appointments and Events Calendar – Amelia v2.1.3
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / Repository / Google / GoogleCalendarRepository.php
ameliabooking / src / Infrastructure / Repository / Google Last commit date
GoogleCalendarRepository.php 5 months ago
GoogleCalendarRepository.php
108 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 *
24 * @return int
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 $statement->execute($params);
45 } catch (\Exception $e) {
46 throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
47 }
48
49 return $this->connection->lastInsertId();
50 }
51
52 /**
53 * @param GoogleCalendar $googleCalendar
54 * @param int $id
55 *
56 * @return mixed
57 * @throws QueryExecutionException
58 */
59 public function update($googleCalendar, $id)
60 {
61 $data = $googleCalendar->toArray();
62
63 $params = [
64 ':token' => $data['token'],
65 ':calendarId' => $data['calendarId'],
66 ':id' => $id
67 ];
68
69 try {
70 $statement = $this->connection->prepare(
71 "UPDATE {$this->table}
72 SET `token` = :token, `calendarId` = :calendarId WHERE id = :id"
73 );
74
75 $statement->execute($params);
76
77 return true;
78 } catch (\Exception $e) {
79 throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
80 }
81 }
82
83 /**
84 * @param $userId
85 *
86 * @return mixed
87 * @throws NotFoundException
88 * @throws QueryExecutionException
89 */
90 public function getByProviderId($userId)
91 {
92 try {
93 $statement = $this->connection->prepare($this->selectQuery() . " WHERE {$this->table}.userId = :userId");
94 $statement->bindParam(':userId', $userId);
95 $statement->execute();
96 $row = $statement->fetch();
97 } catch (\Exception $e) {
98 throw new QueryExecutionException('Unable to find by id in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
99 }
100
101 if (!$row) {
102 throw new NotFoundException('Data not found in ' . __CLASS__);
103 }
104
105 return call_user_func([static::FACTORY, 'create'], $row);
106 }
107 }
108