PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.32
Booking for Appointments and Events Calendar – Amelia v1.2.32
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 / Gallery / GalleryRepository.php
ameliabooking / src / Infrastructure / Repository / Gallery Last commit date
GalleryRepository.php 1 year ago
GalleryRepository.php
143 lines
1 <?php
2
3 namespace AmeliaBooking\Infrastructure\Repository\Gallery;
4
5 use AmeliaBooking\Domain\Entity\Gallery\GalleryImage;
6 use AmeliaBooking\Domain\Repository\Gallery\GalleryRepositoryInterface;
7 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
8 use AmeliaBooking\Infrastructure\Repository\AbstractRepository;
9
10 /**
11 * Class GalleryRepository
12 *
13 * @package AmeliaBooking\Infrastructure\Repository
14 */
15 class GalleryRepository extends AbstractRepository implements GalleryRepositoryInterface
16 {
17 /**
18 * @param GalleryImage $entity
19 *
20 * @return int
21 * @throws QueryExecutionException
22 */
23 public function add($entity)
24 {
25 $data = $entity->toArray();
26
27 $params = [
28 ':entityId' => $data['entityId'],
29 ':entityType' => $data['entityType'],
30 ':pictureFullPath' => $data['pictureFullPath'],
31 ':pictureThumbPath' => $data['pictureThumbPath'],
32 ':position' => $data['position']
33 ];
34
35 try {
36 $statement = $this->connection->prepare(
37 "INSERT INTO {$this->table} (
38 `entityId`,
39 `entityType`,
40 `pictureFullPath`,
41 `pictureThumbPath`,
42 `position`
43 ) VALUES (
44 :entityId,
45 :entityType,
46 :pictureFullPath,
47 :pictureThumbPath,
48 :position
49 )"
50 );
51
52 $res = $statement->execute($params);
53
54 if (!$res) {
55 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
56 }
57 } catch (\Exception $e) {
58 throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e);
59 }
60
61 return $this->connection->lastInsertId();
62 }
63
64 /**
65 * @param int $id
66 * @param GalleryImage $entity
67 *
68 * @return bool
69 * @throws QueryExecutionException
70 */
71 public function update($id, $entity)
72 {
73 $data = $entity->toArray();
74
75 $params = [
76 ':id' => $id,
77 ':pictureFullPath' => $data['pictureFullPath'],
78 ':pictureThumbPath' => $data['pictureThumbPath'],
79 ':position' => $data['position']
80 ];
81
82 try {
83 $statement = $this->connection->prepare(
84 "UPDATE {$this->table}
85 SET
86 `pictureFullPath` = :pictureFullPath,
87 `pictureThumbPath` = :pictureThumbPath,
88 `position` = :position
89 WHERE
90 id = :id"
91 );
92
93 $res = $statement->execute($params);
94
95 if (!$res) {
96 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
97 }
98
99 return $res;
100 } catch (\Exception $e) {
101 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
102 }
103 }
104
105 /**
106 *
107 * It will delete all relations for one entity except ones that are sent in images array
108 *
109 * @param array $imagesIds
110 * @param int $entityId
111 * @param string $entityType
112 *
113 * @return bool
114 * @throws QueryExecutionException
115 */
116 public function deleteAllNotInImagesArray($imagesIds, $entityId, $entityType)
117 {
118 $images = ' ';
119
120 if (!empty($imagesIds)) {
121 foreach ($imagesIds as $index => $value) {
122 ++$index;
123 $images .= ':id' . $index . ', ';
124 $params[':id' . $index] = $value;
125 }
126 $images = 'AND `id` NOT IN (' . rtrim($images, ', ') . ')';
127 }
128
129 $params[':entityType'] = $entityType;
130 $params[':entityId'] = $entityId;
131
132 try {
133 $statement = $this->connection->prepare(
134 "DELETE FROM {$this->table} WHERE entityType = :entityType AND entityId = :entityId $images"
135 );
136
137 return $statement->execute($params);
138 } catch (\Exception $e) {
139 throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e);
140 }
141 }
142 }
143