SettingsFactory.php
122 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @copyright © TMS-Plugins. All rights reserved. |
| 4 | * @licence See LICENCE.md for license details. |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Domain\Factory\Settings; |
| 8 | |
| 9 | use AmeliaBooking\Domain\Entity\Settings\GeneralSettings; |
| 10 | use AmeliaBooking\Domain\Entity\Settings\GoogleMeetSettings; |
| 11 | use AmeliaBooking\Domain\Entity\Settings\LessonSpaceSettings; |
| 12 | use AmeliaBooking\Domain\Entity\Settings\PaymentSettings; |
| 13 | use AmeliaBooking\Domain\Entity\Settings\Settings; |
| 14 | use AmeliaBooking\Domain\Entity\Settings\ZoomSettings; |
| 15 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 16 | |
| 17 | /** |
| 18 | * Class SettingsFactory |
| 19 | * |
| 20 | * @package AmeliaBooking\Domain\Factory\Settings |
| 21 | */ |
| 22 | class SettingsFactory |
| 23 | { |
| 24 | /** |
| 25 | * @param Json $entityJsonData |
| 26 | * @param array $globalSettings |
| 27 | * |
| 28 | * @return Settings |
| 29 | */ |
| 30 | public static function create($entityJsonData, $globalSettings) |
| 31 | { |
| 32 | $entitySettings = new Settings(); |
| 33 | $generalSettings = new GeneralSettings(); |
| 34 | $zoomSettings = new ZoomSettings(); |
| 35 | $paymentSettings = new PaymentSettings(); |
| 36 | $lessonSpaceSetings = new LessonSpaceSettings(); |
| 37 | $googleMeetSettings = new GoogleMeetSettings(); |
| 38 | |
| 39 | $data = $entityJsonData ? json_decode($entityJsonData->getValue(), true) : []; |
| 40 | |
| 41 | $isOldEntitySettings = !isset($data['activation']['version']); |
| 42 | |
| 43 | if (isset($data['general']['defaultAppointmentStatus'])) { |
| 44 | $generalSettings->setDefaultAppointmentStatus($data['general']['defaultAppointmentStatus']); |
| 45 | } else { |
| 46 | $generalSettings->setDefaultAppointmentStatus($globalSettings['general']['defaultAppointmentStatus']); |
| 47 | } |
| 48 | |
| 49 | if (isset($data['general']['minimumTimeRequirementPriorToBooking'])) { |
| 50 | $generalSettings->setMinimumTimeRequirementPriorToBooking( |
| 51 | $data['general']['minimumTimeRequirementPriorToBooking'] |
| 52 | ); |
| 53 | } else { |
| 54 | $generalSettings->setMinimumTimeRequirementPriorToBooking( |
| 55 | $globalSettings['general']['minimumTimeRequirementPriorToBooking'] |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | if (isset($data['general']['minimumTimeRequirementPriorToCanceling'])) { |
| 60 | $generalSettings->setMinimumTimeRequirementPriorToCanceling( |
| 61 | $data['general']['minimumTimeRequirementPriorToCanceling'] |
| 62 | ); |
| 63 | } else { |
| 64 | $generalSettings->setMinimumTimeRequirementPriorToCanceling( |
| 65 | $globalSettings['general']['minimumTimeRequirementPriorToCanceling'] |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | if (isset($data['general']['minimumTimeRequirementPriorToRescheduling'])) { |
| 70 | $generalSettings->setMinimumTimeRequirementPriorToRescheduling( |
| 71 | $data['general']['minimumTimeRequirementPriorToRescheduling'] |
| 72 | ); |
| 73 | } else { |
| 74 | $generalSettings->setMinimumTimeRequirementPriorToRescheduling( |
| 75 | $globalSettings['general']['minimumTimeRequirementPriorToRescheduling'] |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | if ($isOldEntitySettings && !isset($globalSettings['general']['minimumTimeRequirementPriorToCanceling'])) { |
| 80 | $generalSettings->setMinimumTimeRequirementPriorToRescheduling( |
| 81 | $generalSettings->getMinimumTimeRequirementPriorToCanceling() |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | if (!empty($data['general']['numberOfDaysAvailableForBooking'])) { |
| 86 | $generalSettings->setNumberOfDaysAvailableForBooking( |
| 87 | $data['general']['numberOfDaysAvailableForBooking'] |
| 88 | ); |
| 89 | } else { |
| 90 | $generalSettings->setNumberOfDaysAvailableForBooking( |
| 91 | $globalSettings['general']['numberOfDaysAvailableForBooking'] |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | if (isset($data['zoom']['enabled'])) { |
| 96 | $zoomSettings->setEnabled($data['zoom']['enabled']); |
| 97 | } else { |
| 98 | $zoomSettings->setEnabled($globalSettings['zoom']['enabled']); |
| 99 | } |
| 100 | |
| 101 | if (isset($data['lessonSpace']['enabled'])) { |
| 102 | $lessonSpaceSetings->setEnabled($data['lessonSpace']['enabled']); |
| 103 | } else { |
| 104 | $lessonSpaceSetings->setEnabled($globalSettings['lessonSpace']['enabled']); |
| 105 | } |
| 106 | |
| 107 | if (isset($data['googleMeet']['enabled'])) { |
| 108 | $googleMeetSettings->setEnabled($data['googleMeet']['enabled']); |
| 109 | } else { |
| 110 | $googleMeetSettings->setEnabled($globalSettings['googleCalendar']['enableGoogleMeet']); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | $entitySettings->setGeneralSettings($generalSettings); |
| 115 | $entitySettings->setZoomSettings($zoomSettings); |
| 116 | $entitySettings->setLessonSpaceSettings($lessonSpaceSetings); |
| 117 | $entitySettings->setGoogleMeetSettings($googleMeetSettings); |
| 118 | |
| 119 | return $entitySettings; |
| 120 | } |
| 121 | } |
| 122 |