/**
  * {@inheritdoc}
  */
 public function setStepExecution(StepExecution $stepExecution)
 {
     $this->stepExecution = $stepExecution;
     if (!$this->contextRegistry) {
         throw new \InvalidArgumentException('Missing ContextRegistry');
     }
     $this->setImportExportContext($this->contextRegistry->getByStepExecution($this->stepExecution));
 }
 /**
  * @param StepExecution $stepExecution
  */
 public function setStepExecution(StepExecution $stepExecution)
 {
     $this->stepExecution = $stepExecution;
     if ($this->strategy instanceof StepExecutionAwareInterface) {
         $this->strategy->setStepExecution($stepExecution);
     }
     $this->setImportExportContext($this->contextRegistry->getByStepExecution($this->stepExecution));
 }
Ejemplo n.º 3
0
 public function testGetByStepExecution()
 {
     $fooStepExecution = $this->createStepExecution();
     $fooContext = $this->registry->getByStepExecution($fooStepExecution);
     $this->assertInstanceOf('Oro\\Bundle\\ImportExportBundle\\Context\\StepExecutionProxyContext', $fooContext);
     $this->assertAttributeEquals($fooStepExecution, 'stepExecution', $fooContext);
     $this->assertSame($fooContext, $this->registry->getByStepExecution($fooStepExecution));
     $barStepExecution = $this->createStepExecution();
     $barContext = $this->registry->getByStepExecution($barStepExecution);
     $this->assertNotSame($barContext, $fooContext);
 }
 /**
  * {@inheritdoc}
  */
 public function setStepExecution(StepExecution $stepExecution)
 {
     $this->stepExecution = $stepExecution;
     if (!$this->contextRegistry) {
         throw new \InvalidArgumentException('Missing ContextRegistry');
     }
     if ($this->strategy instanceof StepExecutionAwareInterface) {
         $this->strategy->setStepExecution($stepExecution);
     }
     $this->setImportExportContext($this->contextRegistry->getByStepExecution($this->stepExecution));
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function write(array $items)
 {
     foreach ($items as $item) {
         $this->entityManager->persist($item);
         $this->detachFixer->fixEntityAssociationFields($item, 1);
     }
     $this->entityManager->flush();
     $configuration = $this->contextRegistry->getByStepExecution($this->stepExecution)->getConfiguration();
     if (empty($configuration[self::SKIP_CLEAR])) {
         $this->entityManager->clear();
     }
 }
Ejemplo n.º 6
0
 /**
  * @return array
  */
 public function getConfig()
 {
     if (null === $this->config) {
         $this->config = $this->contextRegistry->getByStepExecution($this->stepExecution)->getConfiguration();
     }
     return $this->config;
 }
Ejemplo n.º 7
0
 public function testGetByStepExecution()
 {
     $fooStepExecution = $this->createStepExecution();
     $fooContext = $this->registry->getByStepExecution($fooStepExecution);
     $this->assertInstanceOf('Oro\\Bundle\\ImportExportBundle\\Context\\StepExecutionProxyContext', $fooContext);
     $this->assertAttributeEquals($fooStepExecution, 'stepExecution', $fooContext);
     $this->assertSame($fooContext, $this->registry->getByStepExecution($fooStepExecution));
     $barStepExecution = $this->createStepExecution('job2');
     $barContext = $this->registry->getByStepExecution($barStepExecution);
     $this->assertNotSame($barContext, $fooContext);
     /** @var \PHPUnit_Framework_MockObject_MockObject|JobInstance $jobInstance */
     $jobInstance = $this->getMock('Akeneo\\Bundle\\BatchBundle\\Entity\\JobInstance');
     $jobInstance->expects($this->any())->method('getAlias')->will($this->returnValue('job2'));
     $this->registry->clear($jobInstance);
     $barContext2 = $this->registry->getByStepExecution($barStepExecution);
     $this->assertNotSame($barContext, $barContext2);
 }
Ejemplo n.º 8
0
 /**
  * @param JobExecution $jobExecution
  * @return array
  */
 protected function collectErrors(JobExecution $jobExecution)
 {
     $errors = array();
     foreach ($jobExecution->getStepExecutions() as $stepExecution) {
         $errors = array_merge($errors, $this->contextRegistry->getByStepExecution($stepExecution)->getErrors());
     }
     return $errors;
 }
 /**
  * @return ProcessorInterface
  * @throws InvalidConfigurationException
  * @throws LogicException
  */
 protected function getDelegateProcessor()
 {
     if (!$this->stepExecution) {
         throw new LogicException('Step execution entity must be injected to processor.');
     }
     $context = $this->contextRegistry->getByStepExecution($this->stepExecution);
     if (!$context->getOption('processorAlias')) {
         throw new InvalidConfigurationException('Configuration of processor must contain "processorAlias" options.');
     }
     $result = $this->processorRegistry->getProcessor($this->delegateType, $context->getOption('processorAlias'));
     if ($result instanceof ContextAwareInterface) {
         $result->setImportExportContext($context);
     }
     if ($result instanceof StepExecutionAwareInterface) {
         $result->setStepExecution($this->stepExecution);
     }
     return $result;
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function write(array $items)
 {
     /** @var EntityManager $em */
     $em = $this->registry->getManager();
     try {
         $em->beginTransaction();
         foreach ($items as $item) {
             $em->persist($item);
         }
         $em->flush();
         $em->commit();
         $configuration = $this->contextRegistry->getByStepExecution($this->stepExecution)->getConfiguration();
         $contextSkipClear = $this->stepExecution->getJobExecution()->getExecutionContext()->get(EntityWriter::SKIP_CLEAR);
         if (empty($configuration[EntityWriter::SKIP_CLEAR]) && !$contextSkipClear) {
             $em->clear();
         }
     } catch (\Exception $exception) {
         $em->rollback();
         if (!$em->isOpen()) {
             $this->registry->resetManager();
         }
         $jobName = $this->stepExecution->getJobExecution()->getJobInstance()->getAlias();
         $event = new WriterErrorEvent($items, $jobName, $exception);
         $this->eventDispatcher->dispatch(WriterErrorEvent::NAME, $event);
         if ($event->getCouldBeSkipped()) {
             $importContext = $this->contextRegistry->getByStepExecution($this->stepExecution);
             $importContext->setValue('error_entries_count', (int) $importContext->getValue('error_entries_count') + count($items));
             $this->logger->warning($event->getWarning());
             if ($event->getException() === $exception) {
                 // exception are already handled and job can move forward
                 throw new InvalidItemException($event->getWarning(), []);
             } else {
                 // exception are already created and ready to be rethrown
                 throw $event->getException();
             }
         } else {
             throw $exception;
         }
     }
     $this->eventDispatcher->dispatch(WriterAfterFlushEvent::NAME, new WriterAfterFlushEvent($em));
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function setStepExecution(StepExecution $stepExecution)
 {
     $context = $this->contextRegistry->getByStepExecution($stepExecution);
     if (!$context->hasOption('filePath')) {
         throw new InvalidConfigurationException('Configuration of CSV writer must contain "filePath".');
     } else {
         $this->setFilePath($context->getOption('filePath'));
     }
     if ($context->hasOption('delimiter')) {
         $this->delimiter = $context->getOption('delimiter');
     }
     if ($context->hasOption('enclosure')) {
         $this->enclosure = $context->getOption('enclosure');
     }
     if ($context->hasOption('firstLineIsHeader')) {
         $this->firstLineIsHeader = (bool) $context->getOption('firstLineIsHeader');
     }
     if ($context->hasOption('header')) {
         $this->header = $context->getOption('header');
     }
 }
Ejemplo n.º 12
0
 /**
  * @dataProvider initializationDataProvider
  *
  * @param \PHPUnit_Framework_MockObject_MockObject|mixed $transport
  * @param null                                           $source
  * @param bool|string                                    $expectedException
  */
 public function testInitialization($transport, $source = null, $expectedException = false)
 {
     $logger = new LoggerStrategy(new NullLogger());
     $contextRegistry = new ContextRegistry();
     $contextMediatorMock = $this->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\Provider\\ConnectorContextMediator')->disableOriginalConstructor()->getMock();
     $integration = new Integration();
     $integration->setTransport($this->transportSettings);
     $context = $contextRegistry->getByStepExecution($this->stepExecutionMock);
     $contextMediatorMock->expects($this->at(0))->method('getTransport')->with($this->equalTo($context))->will($this->returnValue($transport));
     $contextMediatorMock->expects($this->at(1))->method('getChannel')->with($this->equalTo($context))->will($this->returnValue($integration));
     $connector = $this->getMockBuilder('Oro\\Bundle\\IntegrationBundle\\Provider\\AbstractConnector')->setMethods(['getConnectorSource'])->setConstructorArgs([$contextRegistry, $logger, $contextMediatorMock])->getMockForAbstractClass();
     if (false !== $expectedException) {
         $this->setExpectedException($expectedException);
     } else {
         $transport->expects($this->once())->method('init')->with($this->equalTo($this->transportSettings));
         $connector->expects($this->once())->method('getConnectorSource')->will($this->returnValue($source));
     }
     $connector->setStepExecution($this->stepExecutionMock);
     $this->assertAttributeSame($transport, 'transport', $connector);
     $this->assertAttributeSame($integration, 'channel', $connector);
 }
Ejemplo n.º 13
0
 /**
  * Set data to JobResult
  * TODO: Find a way to work with multiple amount of job and step executions
  * TODO https://magecore.atlassian.net/browse/BAP-2600
  *
  * @param JobResult $jobResult
  * @param JobInstance $jobInstance
  */
 protected function setJobResultData(JobResult $jobResult, JobInstance $jobInstance)
 {
     $jobResult->setJobId($jobInstance->getId());
     $jobResult->setJobCode($jobInstance->getCode());
     /** @var JobExecution $jobExecution */
     $jobExecution = $jobInstance->getJobExecutions()->first();
     if ($jobExecution) {
         $stepExecutions = $jobExecution->getStepExecutions();
         /** @var StepExecution $firstStepExecution */
         $firstStepExecution = $stepExecutions->first();
         if ($firstStepExecution) {
             $context = $this->contextRegistry->getByStepExecution($firstStepExecution);
             if ($stepExecutions->count() > 1) {
                 /** @var StepExecution $stepExecution */
                 foreach ($stepExecutions->slice(1) as $stepExecution) {
                     ContextHelper::mergeContextCounters($context, $this->contextRegistry->getByStepExecution($stepExecution));
                 }
             }
             $jobResult->setContext($context);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function setStepExecution(StepExecution $stepExecution)
 {
     $this->importExportContext = $this->contextRegistry->getByStepExecution($stepExecution);
     $this->setImportExportContext($this->importExportContext);
 }
 /**
  * @return ContextInterface
  */
 protected function getContext()
 {
     return $this->contextRegistry->getByStepExecution($this->getStepExecution());
 }
 /**
  * @param array $item
  */
 protected function updateContext(array $item)
 {
     $context = $this->contextRegistry->getByStepExecution($this->stepExecution);
     $toDelete = (int) $context->getDeleteCount() + $this->getItemsToRemoveCount($item);
     $context->setValue('delete_count', $toDelete);
 }