PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
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 years ago
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