PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / trunk
Booking for Appointments and Events Calendar – Amelia vtrunk
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 3 months ago
GalleryRepository.php
136 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 $statement->execute($params);
53 } catch (\Exception $e) {
54 throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
55 }
56
57 return $this->connection->lastInsertId();
58 }
59
60 /**
61 * @param int $id
62 * @param GalleryImage $entity
63 *
64 * @return bool
65 * @throws QueryExecutionException
66 */
67 public function update($id, $entity)
68 {
69 $data = $entity->toArray();
70
71 $params = [
72 ':id' => $id,
73 ':pictureFullPath' => $data['pictureFullPath'],
74 ':pictureThumbPath' => $data['pictureThumbPath'],
75 ':position' => $data['position']
76 ];
77
78 try {
79 $statement = $this->connection->prepare(
80 "UPDATE {$this->table}
81 SET
82 `pictureFullPath` = :pictureFullPath,
83 `pictureThumbPath` = :pictureThumbPath,
84 `position` = :position
85 WHERE
86 id = :id"
87 );
88
89 $statement->execute($params);
90
91 return true;
92 } catch (\Exception $e) {
93 throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
94 }
95 }
96
97 /**
98 *
99 * It will delete all relations for one entity except ones that are sent in images array
100 *
101 * @param array $imagesIds
102 * @param int $entityId
103 * @param string $entityType
104 *
105 * @return bool
106 * @throws QueryExecutionException
107 */
108 public function deleteAllNotInImagesArray($imagesIds, $entityId, $entityType)
109 {
110 $images = ' ';
111
112 if (!empty($imagesIds)) {
113 foreach ($imagesIds as $index => $value) {
114 ++$index;
115 $images .= ':id' . $index . ', ';
116 $params[':id' . $index] = $value;
117 }
118 $images = 'AND `id` NOT IN (' . rtrim($images, ', ') . ')';
119 }
120
121 $params[':entityType'] = $entityType;
122 $params[':entityId'] = $entityId;
123
124 try {
125 $statement = $this->connection->prepare(
126 "DELETE FROM {$this->table} WHERE entityType = :entityType AND entityId = :entityId $images"
127 );
128
129 $statement->execute($params);
130 return true;
131 } catch (\Exception $e) {
132 throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
133 }
134 }
135 }
136