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