DayOffRepository.php
5 years ago
PeriodLocationRepository.php
3 years ago
PeriodRepository.php
2 years ago
PeriodServiceRepository.php
3 years ago
SpecialDayPeriodLocationRepository.php
3 years ago
SpecialDayPeriodRepository.php
2 years ago
SpecialDayPeriodServiceRepository.php
6 years ago
SpecialDayRepository.php
6 years ago
TimeOutRepository.php
6 years ago
WeekDayRepository.php
6 years ago
PeriodRepository.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Schedule; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Schedule\Period; |
| 6 | use AmeliaBooking\Domain\Factory\Schedule\PeriodFactory; |
| 7 | use AmeliaBooking\Infrastructure\Licence; |
| 8 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 9 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 10 | |
| 11 | /** |
| 12 | * Class PeriodRepository |
| 13 | * |
| 14 | * @package AmeliaBooking\Infrastructure\Repository\Schedule |
| 15 | */ |
| 16 | class PeriodRepository extends AbstractRepository |
| 17 | { |
| 18 | const FACTORY = PeriodFactory::class; |
| 19 | |
| 20 | /** |
| 21 | * @param Period $entity |
| 22 | * @param int $weekDayId |
| 23 | * |
| 24 | * @return bool |
| 25 | * @throws QueryExecutionException |
| 26 | */ |
| 27 | public function add($entity, $weekDayId) |
| 28 | { |
| 29 | $data = $entity->toArray(); |
| 30 | |
| 31 | $params = [ |
| 32 | ':weekDayId' => $weekDayId, |
| 33 | ':startTime' => $data['startTime'], |
| 34 | ':endTime' => $data['endTime'], |
| 35 | ]; |
| 36 | |
| 37 | $additionalData = Licence\DataModifier::getPeriodRepositoryData($data); |
| 38 | |
| 39 | $params = array_merge($params, $additionalData['values']); |
| 40 | |
| 41 | try { |
| 42 | $statement = $this->connection->prepare( |
| 43 | "INSERT INTO |
| 44 | {$this->table} |
| 45 | ( |
| 46 | {$additionalData['columns']} |
| 47 | `weekDayId`, |
| 48 | `startTime`, |
| 49 | `endTime` |
| 50 | ) VALUES ( |
| 51 | {$additionalData['placeholders']} |
| 52 | :weekDayId, |
| 53 | :startTime, |
| 54 | :endTime |
| 55 | )" |
| 56 | ); |
| 57 | |
| 58 | $res = $statement->execute($params); |
| 59 | if (!$res) { |
| 60 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 61 | } |
| 62 | } catch (\Exception $e) { |
| 63 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 64 | } |
| 65 | |
| 66 | return $this->connection->lastInsertId(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param Period $entity |
| 71 | * @param int $id |
| 72 | * |
| 73 | * @return int |
| 74 | * @throws QueryExecutionException |
| 75 | */ |
| 76 | public function update($entity, $id) |
| 77 | { |
| 78 | $data = $entity->toArray(); |
| 79 | |
| 80 | $params = [ |
| 81 | ':id' => $id, |
| 82 | ':startTime' => $data['startTime'], |
| 83 | ':endTime' => $data['endTime'], |
| 84 | ]; |
| 85 | |
| 86 | $additionalData = Licence\DataModifier::getPeriodRepositoryData($data); |
| 87 | |
| 88 | $params = array_merge($params, $additionalData['values']); |
| 89 | |
| 90 | try { |
| 91 | $statement = $this->connection->prepare( |
| 92 | "UPDATE {$this->table} |
| 93 | SET |
| 94 | {$additionalData['columnsPlaceholders']} |
| 95 | `startTime` = :startTime, |
| 96 | `endTime` = :endTime |
| 97 | WHERE |
| 98 | id = :id" |
| 99 | ); |
| 100 | |
| 101 | $res = $statement->execute($params); |
| 102 | if (!$res) { |
| 103 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 104 | } |
| 105 | |
| 106 | return $res; |
| 107 | } catch (\Exception $e) { |
| 108 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 |