CacheRepository.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © TMS-Plugins. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure\Repository\Cache; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Entity\Cache\Cache; |
| 11 | use AmeliaBooking\Domain\Factory\Cache\CacheFactory; |
| 12 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 14 | use AmeliaBooking\Infrastructure\Connection; |
| 15 | |
| 16 | /** |
| 17 | * Class CacheRepository |
| 18 | * |
| 19 | * @package AmeliaBooking\Infrastructure\Repository\Cache |
| 20 | */ |
| 21 | class CacheRepository extends AbstractRepository |
| 22 | { |
| 23 | /** |
| 24 | * @param Connection $connection |
| 25 | * @param string $table |
| 26 | */ |
| 27 | public function __construct( |
| 28 | Connection $connection, |
| 29 | $table |
| 30 | ) { |
| 31 | parent::__construct($connection, $table); |
| 32 | } |
| 33 | |
| 34 | public const FACTORY = CacheFactory::class; |
| 35 | |
| 36 | /** |
| 37 | * @param Cache $entity |
| 38 | * |
| 39 | * @return bool |
| 40 | * @throws QueryExecutionException |
| 41 | */ |
| 42 | public function add($entity) |
| 43 | { |
| 44 | $data = $entity->toArray(); |
| 45 | |
| 46 | $params = [ |
| 47 | ':name' => $data['name'], |
| 48 | ':data' => $data['data'], |
| 49 | ]; |
| 50 | |
| 51 | try { |
| 52 | $statement = $this->connection->prepare( |
| 53 | "INSERT INTO |
| 54 | {$this->table} |
| 55 | ( |
| 56 | `name`, |
| 57 | `data` |
| 58 | ) VALUES ( |
| 59 | :name, |
| 60 | :data |
| 61 | )" |
| 62 | ); |
| 63 | |
| 64 | $response = $statement->execute($params); |
| 65 | } catch (\Exception $e) { |
| 66 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 67 | } |
| 68 | |
| 69 | if (!$response) { |
| 70 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 71 | } |
| 72 | |
| 73 | return $this->connection->lastInsertId(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param int $id |
| 78 | * @param Cache $entity |
| 79 | * |
| 80 | * @return bool |
| 81 | * @throws QueryExecutionException |
| 82 | */ |
| 83 | public function update($id, $entity) |
| 84 | { |
| 85 | $data = $entity->toArray(); |
| 86 | |
| 87 | $params = [ |
| 88 | ':paymentId' => $data['paymentId'], |
| 89 | ':data' => $data['data'], |
| 90 | ':id' => $id, |
| 91 | ]; |
| 92 | |
| 93 | try { |
| 94 | $statement = $this->connection->prepare( |
| 95 | "UPDATE {$this->table} |
| 96 | SET |
| 97 | `paymentId` = :paymentId, |
| 98 | `data` = :data |
| 99 | WHERE |
| 100 | id = :id" |
| 101 | ); |
| 102 | |
| 103 | $response = $statement->execute($params); |
| 104 | } catch (\Exception $e) { |
| 105 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 106 | } |
| 107 | |
| 108 | if (!$response) { |
| 109 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 110 | } |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param int $id |
| 117 | * @param string $name |
| 118 | * |
| 119 | * @return Cache |
| 120 | * @throws QueryExecutionException |
| 121 | */ |
| 122 | public function getByIdAndName($id, $name) |
| 123 | { |
| 124 | try { |
| 125 | $statement = $this->connection->prepare( |
| 126 | $this->selectQuery() . " WHERE id = :id AND name = :name" |
| 127 | ); |
| 128 | |
| 129 | $params = [ |
| 130 | ':id' => $id, |
| 131 | ':name' => $name |
| 132 | ]; |
| 133 | |
| 134 | $statement->execute($params); |
| 135 | |
| 136 | $row = $statement->fetch(); |
| 137 | } catch (\Exception $e) { |
| 138 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 139 | } |
| 140 | |
| 141 | if (!$row) { |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | return call_user_func([static::FACTORY, 'create'], $row); |
| 146 | } |
| 147 | } |
| 148 |