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
ServiceFactory.php
341 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\Category; |
| 13 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 14 | use AmeliaBooking\Domain\Entity\Entities; |
| 15 | use AmeliaBooking\Domain\Entity\Gallery\GalleryImage; |
| 16 | use AmeliaBooking\Domain\Factory\Coupon\CouponFactory; |
| 17 | use AmeliaBooking\Domain\ValueObjects\BooleanValueObject; |
| 18 | use AmeliaBooking\Domain\ValueObjects\Duration; |
| 19 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 20 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\PositiveInteger; |
| 21 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\WholeNumber; |
| 22 | use AmeliaBooking\Domain\ValueObjects\Picture; |
| 23 | use AmeliaBooking\Domain\ValueObjects\PositiveDuration; |
| 24 | use AmeliaBooking\Domain\ValueObjects\Number\Float\Price; |
| 25 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 26 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\IntegerValue; |
| 27 | use AmeliaBooking\Domain\ValueObjects\String\Cycle; |
| 28 | use AmeliaBooking\Domain\ValueObjects\String\DepositType; |
| 29 | use AmeliaBooking\Domain\ValueObjects\String\EntityType; |
| 30 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 31 | use AmeliaBooking\Domain\ValueObjects\Priority; |
| 32 | use AmeliaBooking\Domain\ValueObjects\String\Color; |
| 33 | use AmeliaBooking\Domain\ValueObjects\String\Description; |
| 34 | use AmeliaBooking\Domain\ValueObjects\String\Name; |
| 35 | use AmeliaBooking\Infrastructure\Licence; |
| 36 | |
| 37 | /** |
| 38 | * Class ServiceFactory |
| 39 | * |
| 40 | * @package AmeliaBooking\Domain\Factory\Bookable\Service |
| 41 | */ |
| 42 | class ServiceFactory |
| 43 | { |
| 44 | /** |
| 45 | * @param $data |
| 46 | * |
| 47 | * @return Service |
| 48 | * @throws InvalidArgumentException |
| 49 | */ |
| 50 | public static function create($data) |
| 51 | { |
| 52 | Licence\DataModifier::serviceFactory($data); |
| 53 | |
| 54 | $service = new Service(); |
| 55 | |
| 56 | if (isset($data['id'])) { |
| 57 | $service->setId(new Id($data['id'])); |
| 58 | } |
| 59 | |
| 60 | if (isset($data['name'])) { |
| 61 | $service->setName(new Name($data['name'])); |
| 62 | } |
| 63 | |
| 64 | if (isset($data['price'])) { |
| 65 | $service->setPrice(new Price($data['price'])); |
| 66 | } |
| 67 | |
| 68 | if (isset($data['status'])) { |
| 69 | $service->setStatus(new Status($data['status'])); |
| 70 | } |
| 71 | |
| 72 | if (isset($data['categoryId'])) { |
| 73 | $service->setCategoryId(new Id($data['categoryId'])); |
| 74 | } |
| 75 | |
| 76 | if (!empty($data['category'])) { |
| 77 | $name = !empty($data['category']['name']) ? $data['category']['name'] : null; |
| 78 | $service->setCategory(new Category(new Name($name))); |
| 79 | } |
| 80 | |
| 81 | if (isset($data['minCapacity'])) { |
| 82 | $service->setMinCapacity(new IntegerValue($data['minCapacity'])); |
| 83 | } |
| 84 | |
| 85 | if (isset($data['maxCapacity'])) { |
| 86 | $service->setMaxCapacity(new IntegerValue($data['maxCapacity'])); |
| 87 | } |
| 88 | |
| 89 | if (isset($data['duration'])) { |
| 90 | $service->setDuration(new PositiveDuration($data['duration'])); |
| 91 | } |
| 92 | |
| 93 | if (isset($data['description'])) { |
| 94 | $service->setDescription(new Description($data['description'])); |
| 95 | } |
| 96 | |
| 97 | if (isset($data['color'])) { |
| 98 | $service->setColor(new Color($data['color'])); |
| 99 | } |
| 100 | |
| 101 | if (!empty($data['timeBefore'])) { |
| 102 | $service->setTimeBefore(new Duration($data['timeBefore'])); |
| 103 | } |
| 104 | |
| 105 | if (!empty($data['timeAfter'])) { |
| 106 | $service->setTimeAfter(new Duration($data['timeAfter'])); |
| 107 | } |
| 108 | |
| 109 | if (isset($data['bringingAnyone'])) { |
| 110 | $service->setBringingAnyone(new BooleanValueObject($data['bringingAnyone'])); |
| 111 | } |
| 112 | |
| 113 | if (isset($data['aggregatedPrice'])) { |
| 114 | $service->setAggregatedPrice(new BooleanValueObject($data['aggregatedPrice'])); |
| 115 | } |
| 116 | |
| 117 | if (!empty($data['priority'])) { |
| 118 | $service->setPriority(new Priority($data['priority'])); |
| 119 | } |
| 120 | |
| 121 | if (!empty($data['pictureFullPath']) && !empty($data['pictureThumbPath'])) { |
| 122 | $service->setPicture(new Picture($data['pictureFullPath'], $data['pictureThumbPath'])); |
| 123 | } |
| 124 | |
| 125 | if (!empty($data['position'])) { |
| 126 | $service->setPosition(new PositiveInteger($data['position'])); |
| 127 | } |
| 128 | |
| 129 | if (isset($data['show'])) { |
| 130 | $service->setShow(new BooleanValueObject($data['show'])); |
| 131 | } |
| 132 | |
| 133 | if (!empty($data['settings'])) { |
| 134 | $service->setSettings(new Json($data['settings'])); |
| 135 | } |
| 136 | |
| 137 | if (!empty($data['recurringCycle'])) { |
| 138 | $service->setRecurringCycle(new Cycle($data['recurringCycle'])); |
| 139 | } |
| 140 | |
| 141 | if (!empty($data['recurringSub'])) { |
| 142 | $service->setRecurringSub(new Name($data['recurringSub'])); |
| 143 | } |
| 144 | |
| 145 | if (isset($data['recurringPayment'])) { |
| 146 | $service->setRecurringPayment(new WholeNumber($data['recurringPayment'])); |
| 147 | } else { |
| 148 | $service->setRecurringPayment(new WholeNumber(0)); |
| 149 | } |
| 150 | |
| 151 | if (isset($data['translations'])) { |
| 152 | $service->setTranslations(new Json($data['translations'])); |
| 153 | } |
| 154 | |
| 155 | if (!empty($data['customPricing'])) { |
| 156 | $service->setCustomPricing(new Json($data['customPricing'])); |
| 157 | } |
| 158 | |
| 159 | if (!empty($data['limitPerCustomer'])) { |
| 160 | $service->setLimitPerCustomer(new Json($data['limitPerCustomer'])); |
| 161 | } |
| 162 | |
| 163 | if (isset($data['deposit'])) { |
| 164 | $service->setDeposit(new Price($data['deposit'])); |
| 165 | } |
| 166 | |
| 167 | if (isset($data['depositPayment'])) { |
| 168 | $service->setDepositPayment(new DepositType($data['depositPayment'])); |
| 169 | } |
| 170 | |
| 171 | if (isset($data['depositPerPerson'])) { |
| 172 | $service->setDepositPerPerson(new BooleanValueObject($data['depositPerPerson'])); |
| 173 | } |
| 174 | |
| 175 | if (isset($data['mandatoryExtra'])) { |
| 176 | $service->setMandatoryExtra(new BooleanValueObject($data['mandatoryExtra'])); |
| 177 | } |
| 178 | |
| 179 | if (!empty($data['minSelectedExtras'])) { |
| 180 | $service->setMinSelectedExtras(new IntegerValue($data['minSelectedExtras'])); |
| 181 | } |
| 182 | |
| 183 | if (isset($data['fullPayment'])) { |
| 184 | $service->setFullPayment(new BooleanValueObject($data['fullPayment'])); |
| 185 | } |
| 186 | |
| 187 | $gallery = new Collection(); |
| 188 | |
| 189 | if (!empty($data['gallery'])) { |
| 190 | foreach ((array)$data['gallery'] as $image) { |
| 191 | $galleryImage = new GalleryImage( |
| 192 | new EntityType(Entities::SERVICE), |
| 193 | new Picture($image['pictureFullPath'], $image['pictureThumbPath']), |
| 194 | new PositiveInteger($image['position']) |
| 195 | ); |
| 196 | |
| 197 | if (!empty($image['id'])) { |
| 198 | $galleryImage->setId(new Id($image['id'])); |
| 199 | } |
| 200 | |
| 201 | if ($service->getId()) { |
| 202 | $galleryImage->setEntityId($service->getId()); |
| 203 | } |
| 204 | |
| 205 | $gallery->addItem($galleryImage); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | $service->setGallery($gallery); |
| 210 | |
| 211 | $extras = new Collection(); |
| 212 | if (!empty($data['extras'])) { |
| 213 | /** @var array $extrasList */ |
| 214 | $extrasList = $data['extras']; |
| 215 | foreach ($extrasList as $extraKey => $extra) { |
| 216 | $extras->addItem(ExtraFactory::create($extra), $extraKey); |
| 217 | } |
| 218 | } |
| 219 | $service->setExtras($extras); |
| 220 | |
| 221 | $coupons = new Collection(); |
| 222 | if (!empty($data['coupons'])) { |
| 223 | /** @var array $couponsList */ |
| 224 | $couponsList = $data['coupons']; |
| 225 | foreach ($couponsList as $couponKey => $coupon) { |
| 226 | $coupons->addItem(CouponFactory::create($coupon), $couponKey); |
| 227 | } |
| 228 | } |
| 229 | $service->setCoupons($coupons); |
| 230 | |
| 231 | if (isset($data['maxExtraPeople'])) { |
| 232 | $service->setMaxExtraPeople(new IntegerValue($data['maxExtraPeople'])); |
| 233 | } |
| 234 | |
| 235 | return $service; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * @param array $rows |
| 240 | * |
| 241 | * @return Collection |
| 242 | * @throws InvalidArgumentException |
| 243 | */ |
| 244 | public static function createCollection($rows) |
| 245 | { |
| 246 | $services = []; |
| 247 | |
| 248 | foreach ($rows as $row) { |
| 249 | $serviceId = $row['service_id']; |
| 250 | $extraId = $row['extra_id']; |
| 251 | $galleryId = isset($row['gallery_id']) ? $row['gallery_id'] : null; |
| 252 | |
| 253 | $services[$serviceId]['id'] = $row['service_id']; |
| 254 | $services[$serviceId]['name'] = $row['service_name']; |
| 255 | $services[$serviceId]['description'] = $row['service_description']; |
| 256 | $services[$serviceId]['color'] = $row['service_color']; |
| 257 | $services[$serviceId]['price'] = $row['service_price']; |
| 258 | $services[$serviceId]['status'] = $row['service_status']; |
| 259 | $services[$serviceId]['categoryId'] = $row['service_categoryId']; |
| 260 | $services[$serviceId]['minCapacity'] = $row['service_minCapacity']; |
| 261 | $services[$serviceId]['maxCapacity'] = $row['service_maxCapacity']; |
| 262 | $services[$serviceId]['maxExtraPeople'] = isset($row['service_maxExtraPeople']) |
| 263 | ? $row['service_maxExtraPeople'] : null; |
| 264 | $services[$serviceId]['duration'] = $row['service_duration']; |
| 265 | $services[$serviceId]['timeAfter'] = $row['service_timeAfter']; |
| 266 | $services[$serviceId]['timeBefore'] = $row['service_timeBefore']; |
| 267 | $services[$serviceId]['bringingAnyone'] = $row['service_bringingAnyone']; |
| 268 | $services[$serviceId]['pictureFullPath'] = $row['service_picture_full']; |
| 269 | $services[$serviceId]['pictureThumbPath'] = $row['service_picture_thumb']; |
| 270 | $services[$serviceId]['position'] = isset($row['service_position']) ? $row['service_position'] : 0; |
| 271 | $services[$serviceId]['show'] = isset($row['service_show']) ? $row['service_show'] : 0; |
| 272 | $services[$serviceId]['aggregatedPrice'] = isset($row['service_aggregatedPrice']) ? |
| 273 | $row['service_aggregatedPrice'] : 0; |
| 274 | $services[$serviceId]['settings'] = isset($row['service_settings']) ? |
| 275 | $row['service_settings'] : null; |
| 276 | $services[$serviceId]['recurringCycle'] = isset($row['service_recurringCycle']) ? |
| 277 | $row['service_recurringCycle'] : null; |
| 278 | $services[$serviceId]['recurringSub'] = isset($row['service_recurringSub']) ? |
| 279 | $row['service_recurringSub'] : null; |
| 280 | $services[$serviceId]['recurringPayment'] = isset($row['service_recurringPayment']) ? |
| 281 | $row['service_recurringPayment'] : null; |
| 282 | $services[$serviceId]['translations'] = isset($row['service_translations']) ? |
| 283 | $row['service_translations'] : null; |
| 284 | $services[$serviceId]['customPricing'] = isset($row['service_customPricing']) ? |
| 285 | $row['service_customPricing'] : null; |
| 286 | $services[$serviceId]['limitPerCustomer'] = isset($row['service_limitPerCustomer']) ? |
| 287 | $row['service_limitPerCustomer'] : null; |
| 288 | $services[$serviceId]['deposit'] = isset($row['service_deposit']) ? $row['service_deposit'] : 0; |
| 289 | $services[$serviceId]['depositPayment'] = isset($row['service_depositPayment']) ? |
| 290 | $row['service_depositPayment'] : 'disabled'; |
| 291 | $services[$serviceId]['depositPerPerson'] = isset($row['service_depositPerPerson']) ? |
| 292 | $row['service_depositPerPerson'] : 1; |
| 293 | $services[$serviceId]['mandatoryExtra'] = isset($row['service_mandatoryExtra']) ? |
| 294 | $row['service_mandatoryExtra'] : null; |
| 295 | $services[$serviceId]['minSelectedExtras'] = isset($row['service_minSelectedExtras']) ? |
| 296 | $row['service_minSelectedExtras'] : null; |
| 297 | $services[$serviceId]['fullPayment'] = isset($row['service_fullPayment']) ? |
| 298 | $row['service_fullPayment'] : null; |
| 299 | |
| 300 | if ($extraId) { |
| 301 | $services[$serviceId]['extras'][$extraId]['id'] = $row['extra_id']; |
| 302 | $services[$serviceId]['extras'][$extraId]['name'] = $row['extra_name']; |
| 303 | $services[$serviceId]['extras'][$extraId]['description'] = isset($row['extra_description']) ? |
| 304 | $row['extra_description'] : null; |
| 305 | $services[$serviceId]['extras'][$extraId]['price'] = $row['extra_price']; |
| 306 | $services[$serviceId]['extras'][$extraId]['maxQuantity'] = $row['extra_maxQuantity']; |
| 307 | $services[$serviceId]['extras'][$extraId]['duration'] = $row['extra_duration']; |
| 308 | $services[$serviceId]['extras'][$extraId]['position'] = $row['extra_position']; |
| 309 | $services[$serviceId]['extras'][$extraId]['aggregatedPrice'] = $row['extra_aggregatedPrice']; |
| 310 | $services[$serviceId]['extras'][$extraId]['translations'] = $row['extra_translations']; |
| 311 | } |
| 312 | |
| 313 | if ($galleryId) { |
| 314 | $services[$serviceId]['gallery'][$galleryId]['id'] = $row['gallery_id']; |
| 315 | $services[$serviceId]['gallery'][$galleryId]['pictureFullPath'] = $row['gallery_picture_full']; |
| 316 | $services[$serviceId]['gallery'][$galleryId]['pictureThumbPath'] = $row['gallery_picture_thumb']; |
| 317 | $services[$serviceId]['gallery'][$galleryId]['position'] = $row['gallery_position']; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | $servicesCollection = new Collection(); |
| 322 | |
| 323 | foreach ($services as $serviceKey => $serviceArray) { |
| 324 | if (!array_key_exists('extras', $serviceArray)) { |
| 325 | $serviceArray['extras'] = []; |
| 326 | } |
| 327 | |
| 328 | if (!array_key_exists('gallery', $serviceArray)) { |
| 329 | $serviceArray['gallery'] = []; |
| 330 | } |
| 331 | |
| 332 | $servicesCollection->addItem( |
| 333 | self::create($serviceArray), |
| 334 | $serviceKey |
| 335 | ); |
| 336 | } |
| 337 | |
| 338 | return $servicesCollection; |
| 339 | } |
| 340 | } |
| 341 |