ExecuteBatchJobAction.php (4839B)
1 <?php 2 3 namespace PartKeepr\BatchJobBundle\Action; 4 5 use Doctrine\ORM\EntityManager; 6 use Doctrine\ORM\QueryBuilder; 7 use Dunglas\ApiBundle\Action\ActionUtilTrait; 8 use Dunglas\ApiBundle\Api\IriConverter; 9 use Dunglas\ApiBundle\Exception\RuntimeException; 10 use Dunglas\ApiBundle\Model\DataProviderInterface; 11 use PartKeepr\BatchJobBundle\Entity\BatchJob; 12 use PartKeepr\CategoryBundle\Exception\RootNodeNotFoundException; 13 use PartKeepr\DoctrineReflectionBundle\Filter\AdvancedSearchFilter; 14 use PartKeepr\DoctrineReflectionBundle\Services\ReflectionService; 15 use Symfony\Component\HttpFoundation\Request; 16 use Symfony\Component\PropertyAccess\PropertyAccess; 17 18 class ExecuteBatchJobAction 19 { 20 use ActionUtilTrait; 21 22 /** 23 * @var DataProviderInterface 24 */ 25 private $dataProvider; 26 27 /** 28 * @var EntityManager 29 */ 30 private $em; 31 32 /** 33 * @var IriConverter 34 */ 35 private $iriConverter; 36 37 /** 38 * @var AdvancedSearchFilter 39 */ 40 private $advancedSearchFilter; 41 42 /** 43 * @var ReflectionService 44 */ 45 private $reflectionService; 46 47 public function __construct( 48 DataProviderInterface $dataProvider, 49 IriConverter $iriConverter, 50 EntityManager $em, 51 AdvancedSearchFilter $advancedSearchFilter, 52 ReflectionService $reflectionService 53 ) { 54 $this->dataProvider = $dataProvider; 55 $this->iriConverter = $iriConverter; 56 $this->em = $em; 57 $this->advancedSearchFilter = $advancedSearchFilter; 58 $this->reflectionService = $reflectionService; 59 } 60 61 /** 62 * Executes a batch action. 63 * 64 * @param Request $request 65 * @param string $id 66 * 67 * @throws RuntimeException|RootNodeNotFoundException 68 * 69 * @return array|\Dunglas\ApiBundle\Model\PaginatorInterface|\Traversable 70 */ 71 public function __invoke(Request $request, $id) 72 { 73 list($resourceType) = $this->extractAttributes($request); 74 75 /** 76 * @var BatchJob 77 */ 78 $batchJob = $this->getItem($this->dataProvider, $resourceType, $id); 79 80 $queryFields = []; 81 $updateFields = []; 82 83 if ($request->request->has("queryFields")) { 84 $queryFields = json_decode($request->request->get("queryFields"), true); 85 } 86 87 if ($request->request->has("updateFields")) { 88 $updateFields = json_decode($request->request->get("updateFields"), true); 89 } 90 91 $queryFilters = []; 92 93 foreach ($batchJob->getBatchJobQueryFields() as $batchJobQueryField) { 94 $queryFilter = new \stdClass(); 95 $queryFilter->property = $batchJobQueryField->getProperty(); 96 $queryFilter->operator = $batchJobQueryField->getOperator(); 97 $queryFilter->value = $batchJobQueryField->getValue(); 98 99 if ($batchJobQueryField->getDynamic()) { 100 foreach ($queryFields as $queryField) { 101 if ($queryField["property"] == $batchJobQueryField->getProperty()) { 102 $queryFilter->value = $queryField["value"]; 103 } 104 } 105 } 106 107 $queryFilters[] = $queryFilter; 108 } 109 110 $updateFieldConfigs = []; 111 112 foreach ($batchJob->getBatchJobUpdateFields() as $batchJobUpdateField) { 113 $updateFieldConfig = new \stdClass(); 114 $updateFieldConfig->property = $batchJobUpdateField->getProperty(); 115 $updateFieldConfig->value = $batchJobUpdateField->getValue(); 116 117 if ($batchJobUpdateField->getDynamic()) { 118 foreach ($updateFields as $updateField) { 119 if ($updateField["property"] == $batchJobUpdateField->getProperty()) { 120 $updateFieldConfig->value = $updateField["value"]; 121 } 122 } 123 } 124 125 $updateFieldConfigs[] = $updateFieldConfig; 126 } 127 128 $configuration = $this->advancedSearchFilter->extractConfiguration($queryFilters, []); 129 130 $qb = new QueryBuilder($this->em); 131 $qb->select("o")->from($this->reflectionService->convertExtJSToPHPClassName($batchJob->getBaseEntity()), "o"); 132 133 $this->advancedSearchFilter->filter($qb, $configuration['filters'], $configuration['sorters']); 134 135 $data = $qb->getQuery()->getResult(); 136 137 $accessor = PropertyAccess::createPropertyAccessor(); 138 139 foreach ($data as $item) { 140 foreach ($updateFieldConfigs as $updateField) { 141 try { 142 $value = $this->iriConverter->getItemFromIri($updateField->value); 143 } catch (\Exception $e) { 144 $value = $updateField->value; 145 } 146 147 $accessor->setValue($item, $updateField->property, $value); 148 } 149 } 150 151 $this->em->flush(); 152 153 return []; 154 } 155 }