public function getResultCollectionByData($data) { $processCollection = new ProcessCollection(); foreach ($data as $input) { $processEntity = new Process($input['class'], $input['data']); $processCollection->add($processEntity); } $dispatcher = new Dispatcher($processCollection); return $dispatcher->getResultCollection(); }
/** * * @return ResultCollection */ public function getResultCollection() { $pool = new WorkerPool(); $pool->setWorkerPoolSize($this->processCollection->count())->create(new ClosureWorker(function (Process $process) { $result = new Result(); $result->setProcess($process); try { if (!class_exists($process->getClass())) { throw new \Exception(sprintf('Invalid class provided: "%s"', $process->getClass())); } $class = $process->getClass(); $class = new $class(); $result->setData($class->process($process->getData())); $result->setSuccessful(true); } catch (\Exception $e) { $result->setErrorMessage($e->getMessage()); } return $result; })); foreach ($this->processCollection as $process) { $pool->run($process); } $pool->waitForAllWorkers(); $resultCollection = new ResultCollection(); foreach ($pool as $result) { $resultEntity = new Result(); if (!empty($result['data'])) { $resultEntity = $result['data']; } elseif (!empty($result['workerException']['message'])) { $resultEntity->setErrorMessage($result['workerException']['message']); } elseif (!empty($result['poolException']['message'])) { $resultEntity->setErrorMessage($result['poolException']['message']); } else { $resultEntity->setErrorMessage(self::INTERNAL_ERROR); } $resultCollection->add($resultEntity); } return $resultCollection; }