src/AppBundle/Controller/EstateFlowController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\AppBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use FOS\RestBundle\Context\Context;
  7. use App\AppBundle\Entity\EstateFlow;
  8. use Symfony\Component\HttpFoundation\StreamedResponse;
  9. class EstateFlowController extends AppBaseEntityController
  10. {
  11.     public function getEstateFlow(Request $request)
  12.     {
  13.         set_time_limit(3600);
  14.         ini_set('memory_limit''512M');
  15.         
  16.         $estateFlow null;
  17.         
  18.         try{
  19.             list($modelEntityId$sequenceId$timelineId) = $this->checkRequestAndResolveParameters($request);
  20.             
  21.             $response $this->getEntityManager()->resolveEstateFlow($modelEntityId$sequenceId$timelineIdtrue);
  22.         } catch(\Throwable $e){
  23.             // Notify fail
  24.             $household $this->get('app.app_bundle.household_connection_manager')
  25.             ->getCurrentlySelectedHousehold();
  26.             $message "{$e->getFile()}:{$e->getLine()}:{$e->getMessage()}";
  27.             $this->getEntityManager()->sendFlowFailEmail($message$household->getHashId(), $modelEntityId$sequenceId$timelineId);
  28.             $values = [
  29.                     'modelEntity'=>$modelEntityId,
  30.                     'sequence'=>$sequenceId,
  31.                     'timeline'=>$timelineId,
  32.                     'result'=>$message,
  33.                     'status'=>EstateFlow::STATUS_FAIL
  34.                 ];
  35.             $this->getEntityManager()->saveEstateFlow($values);
  36.             
  37.             return $this->createBadRequestResponse();
  38.         }
  39.         return JsonResponse::fromJsonString("{\"estateFlow\":{$response->estateFlow},\"fromCache\":{$response->fromCache}}");
  40.     }
  41.     public function estateFlowRenderFail(Request $request)
  42.     {
  43.         list($modelEntityId$sequenceId$timelineId) = $this->checkRequestAndResolveParameters($request);
  44.             
  45.         // Notify fail
  46.         $household $this->get('app.app_bundle.household_connection_manager')
  47.         ->getCurrentlySelectedHousehold();
  48.         $message "Render fail";
  49.         $this->getEntityManager()->sendFlowFailEmail($message$household->getHashId(), $modelEntityId$sequenceId$timelineId);
  50.         $values = [
  51.                 'modelEntity'=>$modelEntityId,
  52.                 'sequence'=>$sequenceId,
  53.                 'timeline'=>$timelineId,
  54.                 'result'=>$message,
  55.                 'status'=>EstateFlow::STATUS_FAIL
  56.             ];
  57.         $this->getEntityManager()->saveEstateFlow($values);
  58.             
  59.     }
  60.     public function resolveAndCacheEstateFlow(Request $request)
  61.     {
  62.         try{
  63.             list($modelEntityId$sequenceId$timelineId) = $this->checkRequestAndResolveParameters($request);
  64.             $response $this->getEntityManager()->resolveEstateFlow($modelEntityId$sequenceId$timelineIdtrue);
  65.         } catch(Exception $e){
  66.             return $this->createBadRequestResponse();
  67.         }
  68.             
  69.         return $this->createResponse('resolved');
  70.     }
  71.     public function clearAndGetEstateFlow(Request $request)
  72.     {
  73.         try{
  74.             list($modelEntityId$sequenceId$timelineId) = $this->checkRequestAndResolveParameters($request);
  75.             $this->getEntityManager()->clearEstateFlow($modelEntityId$sequenceId$timelineId);
  76.             $response $this->get('app.app_bundle.estate_flow_service')->resolveEstateFlow($modelEntityId$sequenceId$timelineId);
  77.         } catch(Exception $e){
  78.             return $this->createBadRequestResponse();
  79.         }            
  80.         
  81.         return JsonResponse::fromJsonString("{\"estateFlow\":{$response->estateFlow},\"fromCache\":{$response->fromCache}}");
  82.     }
  83.     private function checkRequestAndResolveParameters(Request $request)
  84.     {
  85.         $modelEntityId $request->get('modelEntityId');
  86.         $sequenceId $request->get('sequenceId');
  87.         $timelineId $request->get('timelineId');
  88.         $estateFlow null;
  89.         
  90.         if(!isset($modelEntityId) ||
  91.             !isset($sequenceId)
  92.         ){
  93.             throw new \Exception();
  94.         }
  95.         if (isset($timelineId) && intval($timelineId) == 0){
  96.             $timelineId null;
  97.         }elseif(isset($timelineId)){
  98.             $timelineId intval($timelineId);
  99.         }
  100.         return [intval($modelEntityId), intval($sequenceId), $timelineId];
  101.     }
  102.     public function getEstateFlowSummary(Request $request)
  103.     {
  104.         $estateFlow null;
  105.         
  106.         try{
  107.             list($modelEntityId$sequenceId$timelineId) = $this->checkRequestAndResolveParameters($request);
  108.             $response $this->get('app.app_bundle.estate_flow_service')->getEstateFlowSummary($modelEntityId$sequenceId$timelineId);
  109.         } catch(Exception $e){
  110.             return $this->createBadRequestResponse();
  111.         }
  112.         
  113.         return $this->view(['summary'=>$response]);
  114.     }
  115.     public function getEstateFlowAIContext(Request $request)
  116.     {
  117.         try{
  118.             list($modelEntityId$sequenceId$timelineId) = $this->checkRequestAndResolveParameters($request);
  119.             $response $this->get('app.app_bundle.estate_flow_service')->generateEstatePrompt($modelEntityId$sequenceId$timelineId);
  120.         } catch(Exception $e){
  121.             return $this->createBadRequestResponse();
  122.         }
  123.         return $this->view(['aiContext'=>$response]);
  124.         
  125.         
  126.     }
  127.     public function getEstateFlowsForSequence(Request $request)
  128.     {
  129.         set_time_limit(3600);
  130.         ini_set('memory_limit''512M');
  131.         list($modelEntityId$sequenceId) = $this->checkRequestAndResolveParameters($request);
  132.                 
  133.         $response = new StreamedResponse(function() use ($modelEntityId$sequenceId) {
  134.             $estateFlows $this->getEntityManager()->resolveSequenceEstateFlows($modelEntityId$sequenceId);
  135.             
  136.             echo '[';
  137.             $first true;
  138.             foreach ($estateFlows as $flow) {
  139.                 if (!$first) {
  140.                     echo ',';
  141.                 }
  142.                 echo $flow;
  143.                 $first false;
  144.                 flush();
  145.             }
  146.             echo ']';
  147.         });
  148.         $response->headers->set('Content-Type''application/json');
  149.         return $response;
  150.     }
  151. }