24 $this->source =
$config->get(
'source');
40 $validateErrors = $this->validate($latitude, $longitude, $zoom,
$width, $height);
41 if (!empty($validateErrors))
46 $point = $this->makePoint($latitude, $longitude);
47 $hash = $this->getHash($point, $zoom,
$width, $height);
52 $resultFromCache = $this->getResultFromCache((
int)$existingFileRow[
'FILE_ID']);
55 return $resultFromCache;
59 $staticMapService = $this->source->makeStaticMapService();
60 if (!$staticMapService)
63 new Error(
'Static map service is not supported by the source')
67 $serviceResult = $staticMapService->getStaticMap($point, $zoom,
$width, $height);
68 if (!$serviceResult->isSuccess())
70 return $serviceResult;
73 $this->saveResultToCache(
$hash, $serviceResult);
75 return $serviceResult;
78 private function validate(
90 $this->makePoint($latitude, $longitude);
92 catch (InvalidArgumentException $exception)
94 $result[] =
new Error($exception->getMessage());
97 if ($zoom < 0 || $zoom > 18)
99 $result[] =
new Error(
'zoom must be a positive number or zero between 0 and 18');
104 $result[] =
new Error(
'width must be a positive number between 0 and 640');
107 if ($height <= 0 || $height > 640)
109 $result[] =
new Error(
'height must be a positive number 0 and 640');
115 private function makePoint(
120 return new Point($latitude, $longitude);
123 private function getHash(
132 $this->source->getCode(),
142 private function getResultFromCache(
int $fileId): ?StaticMapResult
144 $file = \CFile::GetByID($fileId)->fetch();
146 if ($file && isset($file[
'SRC']))
148 return (
new StaticMapResult())
149 ->setMimeType($file[
'CONTENT_TYPE'])
150 ->setPath($file[
'SRC'])
157 private function saveResultToCache(
string $hash, StaticMapResult
$result): void
159 $resultMimeType =
$result->getMimeType();
160 $resultContent =
$result->getContent();
162 $resultFileExtension =
'';
163 foreach (MimeType::getMimeTypeList() as $extension => $mimeType)
165 if ($resultMimeType === $mimeType)
167 $resultFileExtension = $extension;
172 $fileId = (int)\CFile::SaveFile(
174 'name' =>
$hash . ($resultFileExtension ? (
'.' . $resultFileExtension) :
''),
175 'type' => $resultMimeType,
176 'size' => mb_strlen($resultContent),
177 'content' => $resultContent,
178 'MODULE_ID' =>
'location',
180 'location/static_map'
185 $filePath = \CFile::GetPath($fileId);
191 StaticMapFileTable::merge(
194 'FILE_ID' => $fileId,
197 'FILE_ID' => $fileId,
static getRowById($id, array $parameters=[])