<?php
namespace Aqarmap\Bundle\NeighborhoodBundle\Controller\Api;
use App\Message\LocationStatisticsMessage;
use Aqarmap\Bundle\ListingBundle\Entity\Location;
use Aqarmap\Bundle\MainBundle\Controller\Api\BaseController;
use Aqarmap\Bundle\NeighborhoodBundle\Service\NeighborhoodManager;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use Nelmio\ApiDocBundle\Annotation\Operation;
use OpenApi\Annotations as OA;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class NeighborhoodController extends BaseController
{
/**
* @var NeighborhoodManager
*/
private $neighborhoodManager;
/** @var LoggerInterface */
private $logger;
public function __construct(NeighborhoodManager $neighborhoodManager, LoggerInterface $logger)
{
$this->neighborhoodManager = $neighborhoodManager;
$this->logger = $logger;
}
/**
* Update Neighborhood Data from Estimates.
*
* @Operation(
* tags={"Neighborhood"},
* summary="Update Neighborhood Data from Estimates.",
*
* @OA\Response(
* response="201",
* description="Returned when successfully created"
* ),
* @OA\Response(
* response="400",
* description="Returned when validation error"
* )
* )
*
* @Rest\Post("/api/v2/neighborhood/estimates",
* options={"i18n" = false}, name="aqarmap_api_neighborhood_update_data")
*
* @return View|Response
*/
public function updateNeighborhoodsData(Request $request)
{
ini_set('memory_limit', '1024M');
$this->logger->debug('entered with request');
/** @var $em \Doctrine\ORM\EntityManager */
$em = $this->getDoctrine()->getManager();
$sections = json_decode($request->request->get('data'), 1);
$em->getConnection()->getConfiguration()->setSQLLogger(null);
gc_collect_cycles();
set_time_limit(0);
$errors = [];
$this->logger->debug('entered');
if (is_array($sections) || is_object($sections)) {
foreach ($sections as $sectionId => $section) {
$this->logger->debug('entered loop');
foreach ($section as $propertyTypeId => $propertyType) {
foreach ($propertyType as $locationId => $data) {
try {
$this->dispatchMessage(new LocationStatisticsMessage(
(int) $locationId,
(int) $sectionId,
(int) $propertyTypeId,
$data,
));
$this->logger->error('modular 0 ');
} catch (\Exception $e) {
$errors[] = $e->getMessage();
}
}
}
}
if (!empty($errors)) {
$this->logger->error('valuationerrors', $errors);
return $this->respond(['errors' => $errors], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
return $this->respond(true);
}
/**
* Get Neighborhood First Level Locations Without Children.
*
* **Response Format**
*
* ```
* {
* "location":
* {
* "id": 1,
* "title": "Cairo",
* "description": "Cairo City",
* "center_lat": 30.04841,
* "center_lng": 31.23619,
* "zoom_level": 10,
* "searchable": true,
* "home_filter": true,
* "statistics_filter": true,
* "neighborhood_filter": true,
* }
* {...
* }
* ]
* },
* {...
* }
* }
* ```
*
* @Operation(
* tags={"Neighborhood"},
* summary="Get Neighborhood First Level Locations Without Children.",
*
* )
*
* @Rest\Get("/api/v2/neighborhood", options={"i18n" = false}, name="aqarmap_api_neighborhood_location")
*
* @Rest\View(serializerGroups={"Default", "Neighborhood"})
*
* @return array
*/
public function getNeighborhoodLocation()
{
/** @var $em \Doctrine\ORM\EntityManager */
$em = $this->getDoctrine()->getManager();
$locations = $em->getRepository(Location::class)->getNeighbourhoodLocations(['level' => 0]);
return [
'locations' => $locations,
];
}
/**
* Get Neighborhood Location With Children.
*
* @Operation(
* tags={"Neighborhood"},
* summary="Get Neighborhood Location With Children.",
*
* )
*
* @Rest\Get("/api/v2/neighborhood/{location}",
* options={"i18n" = false}, name="aqarmap_api_neighborhood_location_details")
*
* @Rest\View(serializerGroups={"Default", "Neighborhood"})
*
* @return array
*/
public function getNeighborhoodDetails(Location $location)
{
return [
'location' => $location,
'children' => $location->getNeighborhoodChildren(),
];
}
/**
* Get Neighborhood History For Crm App.
*
* @Operation(
* tags={"Neighborhood"},
* summary="Get Neighborhood History For Crm App.",
*
* )
*
* @Rest\Get("/api/v2/neighborhood/{location}/history",
* options={"i18n" = false}, name="aqarmap_api_neighborhood_history_data")
*
* @Rest\View(serializerGroups={"Default", "NeighborhoodStatistics"})
*
* @return array
*/
public function getNeighborhoodHistory(Location $location)
{
$historyData = $this->neighborhoodManager->generateStatisticsHistoryData($location);
foreach ($historyData['historyData'] as &$value) {
$value['data']['average_price'] = array_values($value['data']['average_price']);
$value['data']['demand_heat'] = array_values($value['data']['demand_heat']);
$value['data']['growth'] = array_values($value['data']['growth']);
}
return [
'location' => $location,
'data' => $historyData['historyData'],
];
}
}