<?php
namespace App\AppBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use FOS\RestBundle\Context\Context;
use App\AppBundle\Entity\EstateFlow;
use Symfony\Component\HttpFoundation\StreamedResponse;
class EstateFlowController extends AppBaseEntityController
{
public function getEstateFlow(Request $request)
{
set_time_limit(3600);
ini_set('memory_limit', '512M');
$estateFlow = null;
try{
list($modelEntityId, $sequenceId, $timelineId) = $this->checkRequestAndResolveParameters($request);
$response = $this->getEntityManager()->resolveEstateFlow($modelEntityId, $sequenceId, $timelineId, true);
} catch(\Throwable $e){
// Notify fail
$household = $this->get('app.app_bundle.household_connection_manager')
->getCurrentlySelectedHousehold();
$message = "{$e->getFile()}:{$e->getLine()}:{$e->getMessage()}";
$this->getEntityManager()->sendFlowFailEmail($message, $household->getHashId(), $modelEntityId, $sequenceId, $timelineId);
$values = [
'modelEntity'=>$modelEntityId,
'sequence'=>$sequenceId,
'timeline'=>$timelineId,
'result'=>$message,
'status'=>EstateFlow::STATUS_FAIL
];
$this->getEntityManager()->saveEstateFlow($values);
return $this->createBadRequestResponse();
}
return JsonResponse::fromJsonString("{\"estateFlow\":{$response->estateFlow},\"fromCache\":{$response->fromCache}}");
}
public function estateFlowRenderFail(Request $request)
{
list($modelEntityId, $sequenceId, $timelineId) = $this->checkRequestAndResolveParameters($request);
// Notify fail
$household = $this->get('app.app_bundle.household_connection_manager')
->getCurrentlySelectedHousehold();
$message = "Render fail";
$this->getEntityManager()->sendFlowFailEmail($message, $household->getHashId(), $modelEntityId, $sequenceId, $timelineId);
$values = [
'modelEntity'=>$modelEntityId,
'sequence'=>$sequenceId,
'timeline'=>$timelineId,
'result'=>$message,
'status'=>EstateFlow::STATUS_FAIL
];
$this->getEntityManager()->saveEstateFlow($values);
}
public function resolveAndCacheEstateFlow(Request $request)
{
try{
list($modelEntityId, $sequenceId, $timelineId) = $this->checkRequestAndResolveParameters($request);
$response = $this->getEntityManager()->resolveEstateFlow($modelEntityId, $sequenceId, $timelineId, true);
} catch(Exception $e){
return $this->createBadRequestResponse();
}
return $this->createResponse('resolved');
}
public function clearAndGetEstateFlow(Request $request)
{
try{
list($modelEntityId, $sequenceId, $timelineId) = $this->checkRequestAndResolveParameters($request);
$this->getEntityManager()->clearEstateFlow($modelEntityId, $sequenceId, $timelineId);
$response = $this->get('app.app_bundle.estate_flow_service')->resolveEstateFlow($modelEntityId, $sequenceId, $timelineId);
} catch(Exception $e){
return $this->createBadRequestResponse();
}
return JsonResponse::fromJsonString("{\"estateFlow\":{$response->estateFlow},\"fromCache\":{$response->fromCache}}");
}
private function checkRequestAndResolveParameters(Request $request)
{
$modelEntityId = $request->get('modelEntityId');
$sequenceId = $request->get('sequenceId');
$timelineId = $request->get('timelineId');
$estateFlow = null;
if(!isset($modelEntityId) ||
!isset($sequenceId)
){
throw new \Exception();
}
if (isset($timelineId) && intval($timelineId) == 0){
$timelineId = null;
}elseif(isset($timelineId)){
$timelineId = intval($timelineId);
}
return [intval($modelEntityId), intval($sequenceId), $timelineId];
}
public function getEstateFlowSummary(Request $request)
{
$estateFlow = null;
try{
list($modelEntityId, $sequenceId, $timelineId) = $this->checkRequestAndResolveParameters($request);
$response = $this->get('app.app_bundle.estate_flow_service')->getEstateFlowSummary($modelEntityId, $sequenceId, $timelineId);
} catch(Exception $e){
return $this->createBadRequestResponse();
}
return $this->view(['summary'=>$response]);
}
public function getEstateFlowAIContext(Request $request)
{
try{
list($modelEntityId, $sequenceId, $timelineId) = $this->checkRequestAndResolveParameters($request);
$response = $this->get('app.app_bundle.estate_flow_service')->generateEstatePrompt($modelEntityId, $sequenceId, $timelineId);
} catch(Exception $e){
return $this->createBadRequestResponse();
}
return $this->view(['aiContext'=>$response]);
}
public function getEstateFlowsForSequence(Request $request)
{
set_time_limit(3600);
ini_set('memory_limit', '512M');
list($modelEntityId, $sequenceId) = $this->checkRequestAndResolveParameters($request);
$response = new StreamedResponse(function() use ($modelEntityId, $sequenceId) {
$estateFlows = $this->getEntityManager()->resolveSequenceEstateFlows($modelEntityId, $sequenceId);
echo '[';
$first = true;
foreach ($estateFlows as $flow) {
if (!$first) {
echo ',';
}
echo $flow;
$first = false;
flush();
}
echo ']';
});
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}