src/Aqarmap/Bundle/NeighborhoodBundle/Controller/Api/NeighborhoodController.php line 195

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\NeighborhoodBundle\Controller\Api;
  3. use App\Message\LocationStatisticsMessage;
  4. use Aqarmap\Bundle\ListingBundle\Entity\Location;
  5. use Aqarmap\Bundle\MainBundle\Controller\Api\BaseController;
  6. use Aqarmap\Bundle\NeighborhoodBundle\Service\NeighborhoodManager;
  7. use FOS\RestBundle\Controller\Annotations as Rest;
  8. use FOS\RestBundle\View\View;
  9. use Nelmio\ApiDocBundle\Annotation\Operation;
  10. use OpenApi\Annotations as OA;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class NeighborhoodController extends BaseController
  15. {
  16.     /**
  17.      * @var NeighborhoodManager
  18.      */
  19.     private $neighborhoodManager;
  20.     /** @var LoggerInterface */
  21.     private $logger;
  22.     public function __construct(NeighborhoodManager $neighborhoodManagerLoggerInterface $logger)
  23.     {
  24.         $this->neighborhoodManager $neighborhoodManager;
  25.         $this->logger $logger;
  26.     }
  27.     /**
  28.      * Update Neighborhood Data from Estimates.
  29.      *
  30.      * @Operation(
  31.      *     tags={"Neighborhood"},
  32.      *     summary="Update Neighborhood Data from Estimates.",
  33.      *
  34.      *     @OA\Response(
  35.      *         response="201",
  36.      *         description="Returned when successfully created"
  37.      *     ),
  38.      *     @OA\Response(
  39.      *         response="400",
  40.      *         description="Returned when validation error"
  41.      *     )
  42.      * )
  43.      *
  44.      * @Rest\Post("/api/v2/neighborhood/estimates",
  45.      * options={"i18n" = false}, name="aqarmap_api_neighborhood_update_data")
  46.      *
  47.      * @return View|Response
  48.      */
  49.     public function updateNeighborhoodsData(Request $request)
  50.     {
  51.         ini_set('memory_limit''1024M');
  52.         $this->logger->debug('entered with request');
  53.         /** @var $em \Doctrine\ORM\EntityManager */
  54.         $em $this->getDoctrine()->getManager();
  55.         $sections json_decode($request->request->get('data'), 1);
  56.         $em->getConnection()->getConfiguration()->setSQLLogger(null);
  57.         gc_collect_cycles();
  58.         set_time_limit(0);
  59.         $errors = [];
  60.         $this->logger->debug('entered');
  61.         if (is_array($sections) || is_object($sections)) {
  62.             foreach ($sections as $sectionId => $section) {
  63.                 $this->logger->debug('entered loop');
  64.                 foreach ($section as $propertyTypeId => $propertyType) {
  65.                     foreach ($propertyType as $locationId => $data) {
  66.                         try {
  67.                             $this->dispatchMessage(new LocationStatisticsMessage(
  68.                                 (int) $locationId,
  69.                                 (int) $sectionId,
  70.                                 (int) $propertyTypeId,
  71.                                 $data,
  72.                             ));
  73.                             $this->logger->error('modular 0 ');
  74.                         } catch (\Exception $e) {
  75.                             $errors[] = $e->getMessage();
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.             if (!empty($errors)) {
  81.                 $this->logger->error('valuationerrors'$errors);
  82.                 return $this->respond(['errors' => $errors], Response::HTTP_INTERNAL_SERVER_ERROR);
  83.             }
  84.         }
  85.         return $this->respond(true);
  86.     }
  87.     /**
  88.      * Get Neighborhood First Level Locations  Without Children.
  89.      *
  90.      * **Response Format**
  91.      *
  92.      * ```
  93.      * {
  94.      *   "location":
  95.      *     {
  96.      *       "id": 1,
  97.      *       "title": "Cairo",
  98.      *       "description": "Cairo City",
  99.      *       "center_lat": 30.04841,
  100.      *       "center_lng": 31.23619,
  101.      *       "zoom_level": 10,
  102.      *       "searchable": true,
  103.      *        "home_filter": true,
  104.      *        "statistics_filter": true,
  105.      *         "neighborhood_filter": true,
  106.      *         }
  107.      *         {...
  108.      *         }
  109.      *       ]
  110.      *     },
  111.      *     {...
  112.      *     }
  113.      * }
  114.      * ```
  115.      *
  116.      * @Operation(
  117.      *     tags={"Neighborhood"},
  118.      *     summary="Get Neighborhood First Level Locations Without Children.",
  119.      *
  120.      * )
  121.      *
  122.      * @Rest\Get("/api/v2/neighborhood", options={"i18n" = false}, name="aqarmap_api_neighborhood_location")
  123.      *
  124.      * @Rest\View(serializerGroups={"Default", "Neighborhood"})
  125.      *
  126.      * @return array
  127.      */
  128.     public function getNeighborhoodLocation()
  129.     {
  130.         /** @var $em \Doctrine\ORM\EntityManager */
  131.         $em $this->getDoctrine()->getManager();
  132.         $locations $em->getRepository(Location::class)->getNeighbourhoodLocations(['level' => 0]);
  133.         return [
  134.             'locations' => $locations,
  135.         ];
  136.     }
  137.     /**
  138.      * Get Neighborhood Location With Children.
  139.      *
  140.      * @Operation(
  141.      *     tags={"Neighborhood"},
  142.      *     summary="Get Neighborhood Location With Children.",
  143.      *
  144.      * )
  145.      *
  146.      * @Rest\Get("/api/v2/neighborhood/{location}",
  147.      * options={"i18n" = false}, name="aqarmap_api_neighborhood_location_details")
  148.      *
  149.      * @Rest\View(serializerGroups={"Default", "Neighborhood"})
  150.      *
  151.      * @return array
  152.      */
  153.     public function getNeighborhoodDetails(Location $location)
  154.     {
  155.         return [
  156.             'location' => $location,
  157.             'children' => $location->getNeighborhoodChildren(),
  158.         ];
  159.     }
  160.     /**
  161.      * Get Neighborhood History For Crm App.
  162.      *
  163.      * @Operation(
  164.      *     tags={"Neighborhood"},
  165.      *     summary="Get Neighborhood History For Crm App.",
  166.      *
  167.      * )
  168.      *
  169.      * @Rest\Get("/api/v2/neighborhood/{location}/history",
  170.      * options={"i18n" = false}, name="aqarmap_api_neighborhood_history_data")
  171.      *
  172.      * @Rest\View(serializerGroups={"Default", "NeighborhoodStatistics"})
  173.      *
  174.      * @return array
  175.      */
  176.     public function getNeighborhoodHistory(Location $location)
  177.     {
  178.         $historyData $this->neighborhoodManager->generateStatisticsHistoryData($location);
  179.         foreach ($historyData['historyData'] as &$value) {
  180.             $value['data']['average_price'] = array_values($value['data']['average_price']);
  181.             $value['data']['demand_heat'] = array_values($value['data']['demand_heat']);
  182.             $value['data']['growth'] = array_values($value['data']['growth']);
  183.         }
  184.         return [
  185.             'location' => $location,
  186.             'data' => $historyData['historyData'],
  187.         ];
  188.     }
  189. }