You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
1.9 KiB

<?php
namespace App\Service;
use App\Entity\Project;
use App\Entity\Task;
use GeoJson\GeoJson;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Workflow\WorkflowInterface;
class GeoJsonManager
{
public function __construct(
private UrlGeneratorInterface $router,
private WorkflowInterface $taskLifecycleStateMachine,
) {
}
private function getFullGeoJson(Task $task)
{
if (!$task->hasGeojson()) {
return null;
}
try {
$data = json_decode($task->getGeojson(), true);
} catch (\Exception $exception) {
return null;
}
if (!isset($data['features'][0]['properties'])) {
return null;
}
$data['features'][0]['properties'] = array_merge($data['features'][0]['properties'], [
'name' => $task->getName(),
'url' => $this->router->generate('app_task_show', ['slug' => $task->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL),
'color' => $this->taskLifecycleStateMachine->getMetadataStore()->getPlaceMetadata($task->getStatus())['color'],
]);
return $data;
}
public function generateGeoJson($entity)
{
$geoJsons = [];
if ($entity instanceof Task) {
$task = $entity;
$geoJson = $this->getFullGeojson($task);
if (!is_null($geoJson)) {
$geoJsons[] = GeoJson::jsonUnserialize($geoJson);
}
} elseif ($entity instanceof Project) {
$project = $entity;
foreach ($project->getTasks() as $task) {
$geoJson = $this->getFullGeojson($task);
if (!is_null($geoJson)) {
$geoJsons[] = GeoJson::jsonUnserialize($geoJson);
}
}
}
return $geoJsons;
}
}