CustomFieldEventRepository.php
3 months ago
CustomFieldOptionRepository.php
3 months ago
CustomFieldRepository.php
3 months ago
CustomFieldServiceRepository.php
3 months ago
CustomFieldOptionRepository.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure\Repository\CustomField; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Entity\CustomField\CustomFieldOption; |
| 11 | use AmeliaBooking\Domain\Factory\CustomField\CustomFieldOptionFactory; |
| 12 | use AmeliaBooking\Domain\Repository\CustomField\CustomFieldOptionRepositoryInterface; |
| 13 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 14 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 15 | |
| 16 | /** |
| 17 | * Class CustomFieldOptionRepository |
| 18 | * |
| 19 | * @package AmeliaBooking\Infrastructure\Repository\CustomField |
| 20 | */ |
| 21 | class CustomFieldOptionRepository extends AbstractRepository implements CustomFieldOptionRepositoryInterface |
| 22 | { |
| 23 | public const FACTORY = CustomFieldOptionFactory::class; |
| 24 | |
| 25 | /** |
| 26 | * @param CustomFieldOption $entity |
| 27 | * |
| 28 | * @return int |
| 29 | * @throws QueryExecutionException |
| 30 | */ |
| 31 | public function add($entity) |
| 32 | { |
| 33 | $data = $entity->toArray(); |
| 34 | |
| 35 | $params = [ |
| 36 | ':customFieldId' => $data['customFieldId'], |
| 37 | ':label' => isset($data['label']) ? $data['label'] : '', |
| 38 | ':position' => $data['position'], |
| 39 | ':translations' => $data['translations'], |
| 40 | ]; |
| 41 | |
| 42 | try { |
| 43 | $statement = $this->connection->prepare( |
| 44 | "INSERT INTO |
| 45 | {$this->table} |
| 46 | ( |
| 47 | `customFieldId`, `label`, `position`, `translations` |
| 48 | ) VALUES ( |
| 49 | :customFieldId, :label, :position, :translations |
| 50 | )" |
| 51 | ); |
| 52 | |
| 53 | |
| 54 | $statement->execute($params); |
| 55 | } catch (\Exception $e) { |
| 56 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 57 | } |
| 58 | |
| 59 | return $this->connection->lastInsertId(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param int $id |
| 64 | * @param CustomFieldOption $entity |
| 65 | * |
| 66 | * @return bool |
| 67 | * @throws QueryExecutionException |
| 68 | */ |
| 69 | public function update($id, $entity) |
| 70 | { |
| 71 | $data = $entity->toArray(); |
| 72 | |
| 73 | $params = [ |
| 74 | ':customFieldId' => $data['customFieldId'], |
| 75 | ':label' => $data['label'], |
| 76 | ':position' => $data['position'], |
| 77 | ':translations' => $data['translations'], |
| 78 | ':id' => $id, |
| 79 | ]; |
| 80 | |
| 81 | try { |
| 82 | $statement = $this->connection->prepare( |
| 83 | "UPDATE {$this->table} |
| 84 | SET |
| 85 | `customFieldId` = :customFieldId, |
| 86 | `label` = :label, |
| 87 | `position` = :position, |
| 88 | `translations` = :translations |
| 89 | WHERE |
| 90 | id = :id" |
| 91 | ); |
| 92 | |
| 93 | $statement->execute($params); |
| 94 | } catch (\Exception $e) { |
| 95 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | } |
| 101 |