CouponEventRepository.php
3 months ago
CouponPackageRepository.php
3 months ago
CouponRepository.php
2 months ago
CouponServiceRepository.php
3 months ago
CouponPackageRepository.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Coupon; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Bookable\Service\Package; |
| 6 | use AmeliaBooking\Domain\Entity\Coupon\Coupon; |
| 7 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 8 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 9 | |
| 10 | /** |
| 11 | * Class CouponPackageRepository |
| 12 | * |
| 13 | * @package AmeliaBooking\Infrastructure\Repository\Coupon |
| 14 | */ |
| 15 | class CouponPackageRepository extends AbstractRepository |
| 16 | { |
| 17 | /** |
| 18 | * @param Coupon $coupon |
| 19 | * @param Package $package |
| 20 | * |
| 21 | * @return mixed |
| 22 | * @throws QueryExecutionException |
| 23 | */ |
| 24 | public function add($coupon, $package) |
| 25 | { |
| 26 | $couponData = $coupon->toArray(); |
| 27 | $packageData = $package->toArray(); |
| 28 | |
| 29 | $params = [ |
| 30 | ':couponId' => $couponData['id'], |
| 31 | ':packageId' => $packageData['id'], |
| 32 | ]; |
| 33 | |
| 34 | try { |
| 35 | $statement = $this->connection->prepare( |
| 36 | "INSERT INTO {$this->table} |
| 37 | ( |
| 38 | `couponId`, |
| 39 | `packageId` |
| 40 | ) |
| 41 | VALUES ( |
| 42 | :couponId, |
| 43 | :packageId |
| 44 | )" |
| 45 | ); |
| 46 | |
| 47 | $statement->execute($params); |
| 48 | |
| 49 | return $this->connection->lastInsertId(); |
| 50 | } catch (\Exception $e) { |
| 51 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param int $couponId |
| 57 | * @param int $packageId |
| 58 | * |
| 59 | * @return mixed |
| 60 | * @throws QueryExecutionException |
| 61 | */ |
| 62 | public function deleteForPackage($couponId, $packageId) |
| 63 | { |
| 64 | $params = [ |
| 65 | ':couponId' => $couponId, |
| 66 | ':packageId' => $packageId, |
| 67 | ]; |
| 68 | |
| 69 | try { |
| 70 | $statement = $this->connection->prepare( |
| 71 | "DELETE FROM {$this->table} WHERE couponId = :couponId AND packageId = :packageId" |
| 72 | ); |
| 73 | |
| 74 | $statement->execute($params); |
| 75 | return true; |
| 76 | } catch (\Exception $e) { |
| 77 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |