GalleryRepository.php
144 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 | /** |
| 19 | * @param GalleryImage $entity |
| 20 | * |
| 21 | * @return int |
| 22 | * @throws QueryExecutionException |
| 23 | */ |
| 24 | public function add($entity) |
| 25 | { |
| 26 | $data = $entity->toArray(); |
| 27 | |
| 28 | $params = [ |
| 29 | ':entityId' => $data['entityId'], |
| 30 | ':entityType' => $data['entityType'], |
| 31 | ':pictureFullPath' => $data['pictureFullPath'], |
| 32 | ':pictureThumbPath' => $data['pictureThumbPath'], |
| 33 | ':position' => $data['position'] |
| 34 | ]; |
| 35 | |
| 36 | try { |
| 37 | $statement = $this->connection->prepare( |
| 38 | "INSERT INTO {$this->table} ( |
| 39 | `entityId`, |
| 40 | `entityType`, |
| 41 | `pictureFullPath`, |
| 42 | `pictureThumbPath`, |
| 43 | `position` |
| 44 | ) VALUES ( |
| 45 | :entityId, |
| 46 | :entityType, |
| 47 | :pictureFullPath, |
| 48 | :pictureThumbPath, |
| 49 | :position |
| 50 | )" |
| 51 | ); |
| 52 | |
| 53 | $res = $statement->execute($params); |
| 54 | |
| 55 | if (!$res) { |
| 56 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 57 | } |
| 58 | } catch (\Exception $e) { |
| 59 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 60 | } |
| 61 | |
| 62 | return $this->connection->lastInsertId(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param int $id |
| 67 | * @param GalleryImage $entity |
| 68 | * |
| 69 | * @return bool |
| 70 | * @throws QueryExecutionException |
| 71 | */ |
| 72 | public function update($id, $entity) |
| 73 | { |
| 74 | $data = $entity->toArray(); |
| 75 | |
| 76 | $params = [ |
| 77 | ':id' => $id, |
| 78 | ':pictureFullPath' => $data['pictureFullPath'], |
| 79 | ':pictureThumbPath' => $data['pictureThumbPath'], |
| 80 | ':position' => $data['position'] |
| 81 | ]; |
| 82 | |
| 83 | try { |
| 84 | $statement = $this->connection->prepare( |
| 85 | "UPDATE {$this->table} |
| 86 | SET |
| 87 | `pictureFullPath` = :pictureFullPath, |
| 88 | `pictureThumbPath` = :pictureThumbPath, |
| 89 | `position` = :position |
| 90 | WHERE |
| 91 | id = :id" |
| 92 | ); |
| 93 | |
| 94 | $res = $statement->execute($params); |
| 95 | |
| 96 | if (!$res) { |
| 97 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 98 | } |
| 99 | |
| 100 | return $res; |
| 101 | } catch (\Exception $e) { |
| 102 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * |
| 108 | * It will delete all relations for one entity except ones that are sent in images array |
| 109 | * |
| 110 | * @param array $imagesIds |
| 111 | * @param int $entityId |
| 112 | * @param string $entityType |
| 113 | * |
| 114 | * @return bool |
| 115 | * @throws QueryExecutionException |
| 116 | */ |
| 117 | public function deleteAllNotInImagesArray($imagesIds, $entityId, $entityType) |
| 118 | { |
| 119 | $images = ' '; |
| 120 | |
| 121 | if (!empty($imagesIds)) { |
| 122 | foreach ($imagesIds as $index => $value) { |
| 123 | ++$index; |
| 124 | $images .= ':id' . $index . ', '; |
| 125 | $params[':id' . $index] = $value; |
| 126 | } |
| 127 | $images = 'AND `id` NOT IN (' . rtrim($images, ', ') . ')'; |
| 128 | } |
| 129 | |
| 130 | $params[':entityType'] = $entityType; |
| 131 | $params[':entityId'] = $entityId; |
| 132 | |
| 133 | try { |
| 134 | $statement = $this->connection->prepare( |
| 135 | "DELETE FROM {$this->table} WHERE entityType = :entityType AND entityId = :entityId $images" |
| 136 | ); |
| 137 | |
| 138 | return $statement->execute($params); |
| 139 | } catch (\Exception $e) { |
| 140 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 |