CategoryFactory.php
6 months ago
ExtraFactory.php
2 years ago
PackageCustomerFactory.php
2 weeks ago
PackageCustomerServiceFactory.php
2 weeks ago
PackageFactory.php
2 months ago
PackageServiceFactory.php
6 months ago
ResourceFactory.php
6 months ago
ServiceFactory.php
6 months ago
PackageFactory.php
304 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\Domain\Factory\Bookable\Service; |
| 9 | |
| 10 | use AmeliaBooking\Domain\Collection\Collection; |
| 11 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 12 | use AmeliaBooking\Domain\Entity\Bookable\Service\Package; |
| 13 | use AmeliaBooking\Domain\Entity\Entities; |
| 14 | use AmeliaBooking\Domain\Entity\Gallery\GalleryImage; |
| 15 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 16 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 17 | use AmeliaBooking\Domain\ValueObjects\DateTime\DateTimeValue; |
| 18 | use AmeliaBooking\Domain\ValueObjects\DiscountPercentageValue; |
| 19 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 20 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\PositiveInteger; |
| 21 | use AmeliaBooking\Domain\ValueObjects\Picture; |
| 22 | use AmeliaBooking\Domain\ValueObjects\Number\Float\Price; |
| 23 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 24 | use AmeliaBooking\Domain\ValueObjects\String\DepositType; |
| 25 | use AmeliaBooking\Domain\ValueObjects\String\EntityType; |
| 26 | use AmeliaBooking\Domain\ValueObjects\String\Color; |
| 27 | use AmeliaBooking\Domain\ValueObjects\String\Description; |
| 28 | use AmeliaBooking\Domain\ValueObjects\String\Label; |
| 29 | use AmeliaBooking\Domain\ValueObjects\String\Name; |
| 30 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 31 | |
| 32 | /** |
| 33 | * Class PackageFactory |
| 34 | * |
| 35 | * @package AmeliaBooking\Domain\Factory\Bookable\Service |
| 36 | */ |
| 37 | class PackageFactory |
| 38 | { |
| 39 | /** |
| 40 | * @param $data |
| 41 | * |
| 42 | * @return Package |
| 43 | * @throws InvalidArgumentException |
| 44 | */ |
| 45 | public static function create($data) |
| 46 | { |
| 47 | /** @var Package $package */ |
| 48 | $package = new Package(); |
| 49 | |
| 50 | if (isset($data['id'])) { |
| 51 | $package->setId(new Id($data['id'])); |
| 52 | } |
| 53 | |
| 54 | if (isset($data['name'])) { |
| 55 | $package->setName(new Name($data['name'])); |
| 56 | } |
| 57 | |
| 58 | if (isset($data['price'])) { |
| 59 | $package->setPrice(new Price($data['price'])); |
| 60 | } |
| 61 | |
| 62 | if (isset($data['description'])) { |
| 63 | $package->setDescription(new Description($data['description'])); |
| 64 | } |
| 65 | |
| 66 | if (isset($data['color'])) { |
| 67 | $package->setColor(new Color($data['color'])); |
| 68 | } |
| 69 | |
| 70 | if (!empty($data['pictureFullPath']) && !empty($data['pictureThumbPath'])) { |
| 71 | $package->setPicture(new Picture($data['pictureFullPath'], $data['pictureThumbPath'])); |
| 72 | } |
| 73 | |
| 74 | if (!empty($data['position'])) { |
| 75 | $package->setPosition(new PositiveInteger($data['position'])); |
| 76 | } |
| 77 | |
| 78 | if (!empty($data['status'])) { |
| 79 | $package->setStatus(new Status($data['status'])); |
| 80 | } |
| 81 | |
| 82 | if (isset($data['calculatedPrice'])) { |
| 83 | $package->setCalculatedPrice(new BooleanValueObject($data['calculatedPrice'])); |
| 84 | } |
| 85 | |
| 86 | if (isset($data['discount'])) { |
| 87 | $package->setDiscount(new DiscountPercentageValue($data['discount'])); |
| 88 | } |
| 89 | |
| 90 | if (!empty($data['settings'])) { |
| 91 | $package->setSettings(new Json($data['settings'])); |
| 92 | } |
| 93 | |
| 94 | if (!empty($data['endDate'])) { |
| 95 | $package->setEndDate( |
| 96 | new DateTimeValue(DateTimeService::getCustomDateTimeObject($data['endDate'])) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | if (!empty($data['durationCount'])) { |
| 101 | $package->setDurationCount(new PositiveInteger($data['durationCount'])); |
| 102 | } |
| 103 | |
| 104 | if (!empty($data['durationType'])) { |
| 105 | $package->setDurationType(new Label($data['durationType'])); |
| 106 | } |
| 107 | |
| 108 | if (isset($data['deposit'])) { |
| 109 | $package->setDeposit(new Price($data['deposit'])); |
| 110 | } |
| 111 | |
| 112 | if (isset($data['depositPayment'])) { |
| 113 | $package->setDepositPayment(new DepositType($data['depositPayment'])); |
| 114 | } |
| 115 | |
| 116 | if (isset($data['fullPayment'])) { |
| 117 | $package->setFullPayment(new BooleanValueObject($data['fullPayment'])); |
| 118 | } |
| 119 | |
| 120 | if (isset($data['limitPerCustomer'])) { |
| 121 | $package->setLimitPerCustomer(new Json($data['limitPerCustomer'])); |
| 122 | } |
| 123 | |
| 124 | /** @var Collection $gallery */ |
| 125 | $gallery = new Collection(); |
| 126 | |
| 127 | if (!empty($data['gallery'])) { |
| 128 | foreach ((array)$data['gallery'] as $image) { |
| 129 | $galleryImage = new GalleryImage( |
| 130 | new EntityType(Entities::PACKAGE), |
| 131 | new Picture($image['pictureFullPath'], $image['pictureThumbPath']), |
| 132 | new PositiveInteger($image['position']) |
| 133 | ); |
| 134 | |
| 135 | if (!empty($image['id'])) { |
| 136 | $galleryImage->setId(new Id($image['id'])); |
| 137 | } |
| 138 | |
| 139 | if ($package->getId()) { |
| 140 | $galleryImage->setEntityId($package->getId()); |
| 141 | } |
| 142 | |
| 143 | $gallery->addItem($galleryImage); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | $package->setGallery($gallery); |
| 148 | |
| 149 | if (!empty($data['translations'])) { |
| 150 | $package->setTranslations(new Json($data['translations'])); |
| 151 | } |
| 152 | |
| 153 | if (isset($data['sharedCapacity'])) { |
| 154 | $package->setSharedCapacity(new BooleanValueObject($data['sharedCapacity'])); |
| 155 | } |
| 156 | |
| 157 | if (isset($data['quantity'])) { |
| 158 | $package->setQuantity(new PositiveInteger($data['quantity'])); |
| 159 | } |
| 160 | |
| 161 | /** @var Collection $bookable */ |
| 162 | $bookable = new Collection(); |
| 163 | |
| 164 | if (!empty($data['bookable'])) { |
| 165 | foreach ($data['bookable'] as $key => $value) { |
| 166 | $bookable->addItem(PackageServiceFactory::create($value), $key); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $package->setBookable($bookable); |
| 171 | |
| 172 | return $package; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param array $rows |
| 177 | * |
| 178 | * @return Collection |
| 179 | * @throws InvalidArgumentException |
| 180 | */ |
| 181 | public static function createCollection($rows) |
| 182 | { |
| 183 | $packages = []; |
| 184 | |
| 185 | foreach ($rows as $row) { |
| 186 | $packageId = $row['package_id']; |
| 187 | $bookableId = $row['package_service_id']; |
| 188 | $galleryId = isset($row['gallery_id']) ? $row['gallery_id'] : null; |
| 189 | $providerId = isset($row['provider_id']) ? $row['provider_id'] : null; |
| 190 | $locationId = isset($row['location_id']) ? $row['location_id'] : null; |
| 191 | |
| 192 | $packages[$packageId]['id'] = $row['package_id']; |
| 193 | $packages[$packageId]['name'] = $row['package_name']; |
| 194 | $packages[$packageId]['description'] = $row['package_description']; |
| 195 | $packages[$packageId]['color'] = $row['package_color']; |
| 196 | $packages[$packageId]['price'] = $row['package_price']; |
| 197 | $packages[$packageId]['status'] = $row['package_status']; |
| 198 | $packages[$packageId]['pictureFullPath'] = $row['package_picture_full']; |
| 199 | $packages[$packageId]['pictureThumbPath'] = $row['package_picture_thumb']; |
| 200 | $packages[$packageId]['position'] = isset($row['package_position']) ? $row['package_position'] : 0; |
| 201 | $packages[$packageId]['calculatedPrice'] = $row['package_calculated_price']; |
| 202 | $packages[$packageId]['sharedCapacity'] = isset($row['package_sharedCapacity']) ? |
| 203 | $row['package_sharedCapacity'] : null; |
| 204 | $packages[$packageId]['quantity'] = isset($row['package_quantity']) ? |
| 205 | $row['package_quantity'] : null; |
| 206 | $packages[$packageId]['discount'] = $row['package_discount']; |
| 207 | $packages[$packageId]['settings'] = $row['package_settings']; |
| 208 | $packages[$packageId]['endDate'] = $row['package_endDate']; |
| 209 | $packages[$packageId]['durationCount'] = $row['package_durationCount']; |
| 210 | $packages[$packageId]['durationType'] = $row['package_durationType']; |
| 211 | $packages[$packageId]['translations'] = $row['package_translations']; |
| 212 | $packages[$packageId]['deposit'] = isset($row['package_deposit']) ? $row['package_deposit'] : null; |
| 213 | $packages[$packageId]['depositPayment'] = isset($row['package_depositPayment']) ? |
| 214 | $row['package_depositPayment'] : null; |
| 215 | $packages[$packageId]['fullPayment'] = isset($row['package_fullPayment']) ? |
| 216 | $row['package_fullPayment'] : 0; |
| 217 | $packages[$packageId]['limitPerCustomer'] = isset($row['package_limitPerCustomer']) ? |
| 218 | $row['package_limitPerCustomer'] : null; |
| 219 | |
| 220 | if ($bookableId) { |
| 221 | $packages[$packageId]['bookable'][$bookableId]['id'] = $bookableId; |
| 222 | $packages[$packageId]['bookable'][$bookableId]['service']['id'] = $row['service_id']; |
| 223 | $packages[$packageId]['bookable'][$bookableId]['service']['name'] = $row['service_name']; |
| 224 | $packages[$packageId]['bookable'][$bookableId]['service']['description'] = |
| 225 | !empty($row['service_description']) ? |
| 226 | $row['service_description'] : |
| 227 | null; |
| 228 | $packages[$packageId]['bookable'][$bookableId]['service']['status'] = $row['service_status']; |
| 229 | $packages[$packageId]['bookable'][$bookableId]['service']['categoryId'] = $row['service_categoryId']; |
| 230 | $packages[$packageId]['bookable'][$bookableId]['service']['duration'] = $row['service_duration']; |
| 231 | $packages[$packageId]['bookable'][$bookableId]['service']['timeBefore'] = $row['service_timeBefore']; |
| 232 | $packages[$packageId]['bookable'][$bookableId]['service']['timeAfter'] = $row['service_timeAfter']; |
| 233 | $packages[$packageId]['bookable'][$bookableId]['service']['price'] = $row['service_price']; |
| 234 | $packages[$packageId]['bookable'][$bookableId]['service']['minCapacity'] = $row['service_minCapacity']; |
| 235 | $packages[$packageId]['bookable'][$bookableId]['service']['maxCapacity'] = $row['service_maxCapacity']; |
| 236 | $packages[$packageId]['bookable'][$bookableId]['service']['pictureFullPath'] = $row['service_picture_full']; |
| 237 | $packages[$packageId]['bookable'][$bookableId]['service']['pictureThumbPath'] = $row['service_picture_thumb']; |
| 238 | $packages[$packageId]['bookable'][$bookableId]['service']['translations'] = |
| 239 | !empty($row['service_translations']) ? $row['service_translations'] : null; |
| 240 | $packages[$packageId]['bookable'][$bookableId]['service']['color'] = $row['service_color']; |
| 241 | $packages[$packageId]['bookable'][$bookableId]['quantity'] = $row['package_service_quantity']; |
| 242 | $packages[$packageId]['bookable'][$bookableId]['minimumScheduled'] = $row['package_service_minimumScheduled']; |
| 243 | $packages[$packageId]['bookable'][$bookableId]['maximumScheduled'] = $row['package_service_maximumScheduled']; |
| 244 | $packages[$packageId]['bookable'][$bookableId]['allowProviderSelection'] = $row['package_service_allowProviderSelection']; |
| 245 | $packages[$packageId]['bookable'][$bookableId]['position'] = $row['package_service_position']; |
| 246 | $packages[$packageId]['bookable'][$bookableId]['service']['show'] = !empty($row['service_show']) ? $row['service_show'] : null; |
| 247 | } |
| 248 | |
| 249 | if ($galleryId) { |
| 250 | $packages[$packageId]['gallery'][$galleryId]['id'] = $row['gallery_id']; |
| 251 | $packages[$packageId]['gallery'][$galleryId]['pictureFullPath'] = $row['gallery_picture_full']; |
| 252 | $packages[$packageId]['gallery'][$galleryId]['pictureThumbPath'] = $row['gallery_picture_thumb']; |
| 253 | $packages[$packageId]['gallery'][$galleryId]['position'] = $row['gallery_position']; |
| 254 | } |
| 255 | |
| 256 | if ($providerId) { |
| 257 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['id'] = $row['provider_id']; |
| 258 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['firstName'] = $row['provider_firstName']; |
| 259 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['lastName'] = $row['provider_lastName']; |
| 260 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['email'] = $row['provider_email']; |
| 261 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['phone'] = |
| 262 | !empty($row['provider_phone']) ? $row['provider_phone'] : null; |
| 263 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['countryPhoneIso'] = |
| 264 | !empty($row['provider_countryPhoneIso']) ? $row['provider_countryPhoneIso'] : null; |
| 265 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['status'] = |
| 266 | !empty($row['provider_status']) ? $row['provider_status'] : Status::VISIBLE; |
| 267 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['type'] = Entities::PROVIDER; |
| 268 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['pictureFullPath'] = |
| 269 | !empty($row['provider_picture_full']) ? $row['provider_picture_full'] : null; |
| 270 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['pictureThumbPath'] = |
| 271 | !empty($row['provider_picture_thumb']) ? $row['provider_picture_thumb'] : null; |
| 272 | $packages[$packageId]['bookable'][$bookableId]['providers'][$providerId]['stripeConnect'] = |
| 273 | !empty($row['provider_stripeConnect']) |
| 274 | ? json_decode($row['provider_stripeConnect'], true) |
| 275 | : null; |
| 276 | } |
| 277 | |
| 278 | if ($locationId) { |
| 279 | $packages[$packageId]['bookable'][$bookableId]['locations'][$locationId]['id'] = $row['location_id']; |
| 280 | $packages[$packageId]['bookable'][$bookableId]['locations'][$locationId]['name'] = $row['location_name']; |
| 281 | $packages[$packageId]['bookable'][$bookableId]['locations'][$locationId]['address'] = $row['location_address']; |
| 282 | $packages[$packageId]['bookable'][$bookableId]['locations'][$locationId]['phone'] = $row['location_phone']; |
| 283 | $packages[$packageId]['bookable'][$bookableId]['locations'][$locationId]['latitude'] = $row['location_latitude']; |
| 284 | $packages[$packageId]['bookable'][$bookableId]['locations'][$locationId]['longitude'] = $row['location_longitude']; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | $packagesCollection = new Collection(); |
| 289 | |
| 290 | foreach ($packages as $packageKey => $packageArray) { |
| 291 | if (!array_key_exists('gallery', $packageArray)) { |
| 292 | $packageArray['gallery'] = []; |
| 293 | } |
| 294 | |
| 295 | $packagesCollection->addItem( |
| 296 | self::create($packageArray), |
| 297 | $packageKey |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | return $packagesCollection; |
| 302 | } |
| 303 | } |
| 304 |