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