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