LocationFactory.php
219 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Domain\Factory\Location; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\Location\Location; |
| 8 | use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory; |
| 9 | use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory; |
| 10 | use AmeliaBooking\Domain\Factory\User\UserFactory; |
| 11 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 12 | use AmeliaBooking\Domain\ValueObjects\Json; |
| 13 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 14 | use AmeliaBooking\Domain\ValueObjects\Picture; |
| 15 | use AmeliaBooking\Domain\ValueObjects\String\Address; |
| 16 | use AmeliaBooking\Domain\ValueObjects\String\Description; |
| 17 | use AmeliaBooking\Domain\ValueObjects\GeoTag; |
| 18 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 19 | use AmeliaBooking\Domain\ValueObjects\String\Name; |
| 20 | use AmeliaBooking\Domain\ValueObjects\String\Phone; |
| 21 | use AmeliaBooking\Domain\ValueObjects\String\Url; |
| 22 | |
| 23 | /** |
| 24 | * Class LocationFactory |
| 25 | * |
| 26 | * @package AmeliaBooking\Domain\Factory\Location |
| 27 | */ |
| 28 | class LocationFactory |
| 29 | { |
| 30 | /** |
| 31 | * @param $data |
| 32 | * |
| 33 | * @return Location |
| 34 | * @throws InvalidArgumentException |
| 35 | */ |
| 36 | public static function create($data) |
| 37 | { |
| 38 | $location = new Location(); |
| 39 | |
| 40 | if (isset($data['id'])) { |
| 41 | $location->setId(new Id($data['id'])); |
| 42 | } |
| 43 | |
| 44 | if (isset($data['name'])) { |
| 45 | $location->setName(new Name($data['name'])); |
| 46 | } |
| 47 | |
| 48 | if (isset($data['countryPhoneIso'])) { |
| 49 | $location->setCountryPhoneIso(new Name($data['countryPhoneIso'])); |
| 50 | } |
| 51 | |
| 52 | if (isset($data['address'])) { |
| 53 | $location->setAddress(new Address($data['address'])); |
| 54 | } |
| 55 | |
| 56 | $location->setPhone(new Phone($data['phone'] ?? '')); |
| 57 | |
| 58 | if (isset($data['latitude'], $data['longitude'])) { |
| 59 | $location->setCoordinates(new GeoTag($data['latitude'], $data['longitude'])); |
| 60 | } |
| 61 | |
| 62 | if (isset($data['description'])) { |
| 63 | $location->setDescription(new Description($data['description'])); |
| 64 | } |
| 65 | |
| 66 | if (isset($data['status'])) { |
| 67 | $location->setStatus(new Status($data['status'])); |
| 68 | } |
| 69 | |
| 70 | if (!empty($data['pictureFullPath']) && !empty($data['pictureThumbPath'])) { |
| 71 | $location->setPicture(new Picture($data['pictureFullPath'], $data['pictureThumbPath'])); |
| 72 | } |
| 73 | |
| 74 | if (isset($data['pin'])) { |
| 75 | $location->setPin(new Url($data['pin'])); |
| 76 | } |
| 77 | |
| 78 | if (!empty($data['translations'])) { |
| 79 | $location->setTranslations(new Json($data['translations'])); |
| 80 | } |
| 81 | |
| 82 | if (isset($data['serviceList'])) { |
| 83 | $serviceList = []; |
| 84 | foreach ((array)$data['serviceList'] as $service) { |
| 85 | $serviceList[$service['id']] = ServiceFactory::create($service); |
| 86 | } |
| 87 | |
| 88 | $location->setServiceList(new Collection($serviceList)); |
| 89 | } |
| 90 | |
| 91 | if (isset($data['eventList'])) { |
| 92 | $eventsList = []; |
| 93 | foreach ((array)$data['eventList'] as $event) { |
| 94 | $eventsList[$event['id']] = EventFactory::create($event); |
| 95 | } |
| 96 | |
| 97 | $location->setEventList(new Collection($eventsList)); |
| 98 | } |
| 99 | |
| 100 | if (isset($data['providerList'])) { |
| 101 | $providerList = []; |
| 102 | foreach ((array)$data['providerList'] as $provider) { |
| 103 | $providerList[$provider['id']] = UserFactory::create($provider); |
| 104 | } |
| 105 | |
| 106 | $location->setProviderList(new Collection($providerList)); |
| 107 | } |
| 108 | |
| 109 | return $location; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * @param array $rows |
| 115 | * |
| 116 | * @return Collection |
| 117 | * @throws InvalidArgumentException |
| 118 | */ |
| 119 | public static function createCollection($rows) |
| 120 | { |
| 121 | $locations = []; |
| 122 | |
| 123 | foreach ($rows as $row) { |
| 124 | $locationId = $row['location_id']; |
| 125 | |
| 126 | $providerId = $row['provider_id']; |
| 127 | |
| 128 | $serviceId = $row['service_id']; |
| 129 | |
| 130 | $eventId = $row['event_id']; |
| 131 | |
| 132 | $eventPeriodId = $row['event_periodId']; |
| 133 | |
| 134 | if (!array_key_exists($locationId, $locations)) { |
| 135 | $locations[$locationId] = [ |
| 136 | 'id' => $row['location_id'], |
| 137 | 'name' => $row['location_name'], |
| 138 | 'address' => $row['location_address'], |
| 139 | 'phone' => $row['location_phone'], |
| 140 | 'latitude' => $row['location_latitude'], |
| 141 | 'longitude' => $row['location_longitude'], |
| 142 | 'description' => $row['location_description'], |
| 143 | 'status' => $row['location_status'], |
| 144 | 'pictureFullPath' => $row['location_pictureFullPath'], |
| 145 | 'pictureThumbPath' => $row['location_pictureThumbPath'], |
| 146 | 'pin' => $row['location_pin'], |
| 147 | 'translations' => $row['location_translations'] |
| 148 | ]; |
| 149 | } |
| 150 | |
| 151 | if ($serviceId && empty($locations[$locationId]['serviceList'][$serviceId])) { |
| 152 | $serviceId = $row['service_id']; |
| 153 | |
| 154 | $locations[$locationId]['serviceList'][$serviceId] = [ |
| 155 | 'id' => $serviceId, |
| 156 | 'name' => $row['service_name'], |
| 157 | 'category' => [ |
| 158 | 'name' => $row['category_name'] |
| 159 | ], |
| 160 | 'color' => $row['service_color'] |
| 161 | ]; |
| 162 | } |
| 163 | |
| 164 | if (!empty($row['provider_id']) && empty($locations[$locationId]['providerList'][$providerId])) { |
| 165 | $locations[$locationId]['providerList'][$providerId] = [ |
| 166 | 'id' => $providerId, |
| 167 | 'firstName' => $row['provider_firstName'], |
| 168 | 'lastName' => $row['provider_lastName'], |
| 169 | 'phone' => $row['provider_phone'], |
| 170 | 'email' => $row['provider_email'], |
| 171 | 'type' => 'provider', |
| 172 | 'pictureThumbPath' => $row['provider_pictureThumbPath'], |
| 173 | 'pictureFullPath' => $row['provider_pictureFullPath'] |
| 174 | ]; |
| 175 | } |
| 176 | |
| 177 | if ($eventId && empty($locations[$locationId]['eventList'][$eventId])) { |
| 178 | $locations[$locationId]['eventList'][$eventId] = [ |
| 179 | 'id' => $eventId, |
| 180 | 'name' => $row['event_name'], |
| 181 | 'color' => $row['event_color'], |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | if ($eventPeriodId && !isset($locations[$locationId]['eventList'][$eventId]['periods'][$eventPeriodId])) { |
| 186 | $zoomMeetingJson = !empty($row['event_periodZoomMeeting']) ? |
| 187 | json_decode($row['event_periodZoomMeeting'], true) : null; |
| 188 | |
| 189 | $locations[$locationId]['eventList'][$eventId]['periods'][$eventPeriodId] = [ |
| 190 | 'id' => $eventPeriodId, |
| 191 | 'periodStart' => DateTimeService::getCustomDateTimeFromUtc($row['event_periodStart']), |
| 192 | 'periodEnd' => DateTimeService::getCustomDateTimeFromUtc($row['event_periodEnd']), |
| 193 | 'zoomMeeting' => [ |
| 194 | 'id' => $zoomMeetingJson ? $zoomMeetingJson['id'] : null, |
| 195 | 'startUrl' => $zoomMeetingJson ? $zoomMeetingJson['startUrl'] : null, |
| 196 | 'joinUrl' => $zoomMeetingJson ? $zoomMeetingJson['joinUrl'] : null, |
| 197 | ], |
| 198 | 'lessonSpace' => !empty($row['event_periodLessonSpace']) ? |
| 199 | $row['event_periodLessonSpace'] : null, |
| 200 | 'googleMeetUrl' => !empty($row['event_googleMeetUrl']) ? |
| 201 | $row['event_googleMeetUrl'] : null |
| 202 | ]; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** @var Collection $collection */ |
| 207 | $collection = new Collection(); |
| 208 | |
| 209 | foreach ($locations as $key => $value) { |
| 210 | $collection->addItem( |
| 211 | self::create($value), |
| 212 | $key |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | return $collection; |
| 217 | } |
| 218 | } |
| 219 |