/**
  * {@inheritdoc}
  */
 public function process($item)
 {
     $entity = $this->findOrCreateObject($item);
     try {
         $this->updater->update($entity, $item);
     } catch (\InvalidArgumentException $exception) {
         $this->skipItemWithMessage($item, $exception->getMessage(), $exception);
     }
     $violations = $this->validator->validate($entity);
     if ($violations->count() > 0) {
         $this->objectDetacher->detach($entity);
         $this->skipItemWithConstraintViolations($item, $violations);
     }
     $rawParameters = $entity->getRawParameters();
     if (!empty($rawParameters)) {
         $job = $this->jobRegistry->get($entity->getJobName());
         $parameters = $this->jobParamsFactory->create($job, $rawParameters);
         $violations = $this->jobParamsValidator->validate($job, $parameters);
         if ($violations->count() > 0) {
             $this->objectDetacher->detach($entity);
             $this->skipItemWithConstraintViolations($item, $violations);
         }
     }
     return $entity;
 }
 /**
  * @param JobInstance $jobInstance
  * @param string      $field
  * @param mixed       $data
  */
 protected function setData(JobInstance $jobInstance, $field, $data)
 {
     switch ($field) {
         case 'connector':
             $jobInstance->setConnector($data);
             break;
         case 'alias':
             $jobInstance->setJobName($data);
             break;
         case 'label':
             $jobInstance->setLabel($data);
             break;
         case 'type':
             $jobInstance->setType($data);
             break;
         case 'configuration':
             $job = $this->jobRegistry->get($jobInstance->getJobName());
             /** @var JobParameters $jobParameters */
             $jobParameters = $this->jobParametersFactory->create($job, $data);
             $jobInstance->setRawParameters($jobParameters->all());
             break;
         case 'code':
             $jobInstance->setCode($data);
             break;
     }
 }
 /**
  * Check if the job execution is supported
  *
  * @param JobExecution $jobExecution
  *
  * @return bool
  */
 public function supports(JobExecution $jobExecution)
 {
     $job = $this->jobRegistry->get($jobExecution->getJobInstance()->getJobName());
     foreach ($job->getSteps() as $step) {
         if ($step instanceof ItemStep && $this->isReaderUsable($step->getReader())) {
             return true;
         }
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 public function validate($entity, Constraint $constraint)
 {
     if ($entity instanceof JobInstanceModel) {
         try {
             $this->jobRegistry->get($entity->getJobName());
         } catch (UndefinedJobException $e) {
             $this->context->buildViolation($constraint->message, ['%job_type%' => $entity->getType()])->atPath($constraint->property)->addViolation();
         }
     }
 }
 /**
  * Add job configuration form type
  *
  * @param FormBuilderInterface $builder
  *
  * @return JobInstanceFormType
  */
 protected function addJobConfigurationField(FormBuilderInterface $builder)
 {
     $jobName = $builder->getData()->getJobName();
     if (null !== $jobName) {
         $job = $this->jobRegistry->get($jobName);
         $builder->add('parameters', 'pim_import_export_job_parameters', ['required' => true, 'by_reference' => false, 'property_path' => 'rawParameters'])->get('parameters')->addModelTransformer(new ConfigurationToJobParametersTransformer($this->jobParametersFactory, $job));
     }
     return $this;
 }
 /**
  * Create a jobExecution
  *
  * @param JobInstance   $jobInstance
  * @param UserInterface $user
  *
  * @return JobExecution
  */
 protected function createJobExecution(JobInstance $jobInstance, UserInterface $user)
 {
     $job = $this->jobRegistry->get($jobInstance->getJobName());
     $jobParameters = $this->jobParametersFactory->create($job, $jobInstance->getRawParameters());
     $jobExecution = $this->jobRepository->createJobExecution($jobInstance, $jobParameters);
     $jobExecution->setUser($user->getUsername());
     $this->jobRepository->updateJobExecution($jobExecution);
     return $jobExecution;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $builder->setDataMapper($this);
     $factory = $builder->getFormFactory();
     $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($factory) {
         $form = $event->getForm();
         $jobInstance = $form->getRoot()->getData();
         if (null === $jobInstance->getId()) {
             return;
         }
         $job = $this->jobRegistry->get($jobInstance->getJobName());
         $configProvider = $this->configProviderRegistry->get($job);
         $configs = $configProvider->getFormConfiguration();
         $constraintProvider = $this->constraintProviderRegistry->get($job);
         $constraintsCollection = $constraintProvider->getConstraintCollection();
         $fieldConstraints = $constraintsCollection->fields;
         foreach ($configs as $parameter => $config) {
             if (isset($config['system']) && true === $config['system']) {
                 continue;
             }
             $config = array_merge(['type' => 'text', 'options' => []], $config);
             $options = array_merge(['auto_initialize' => false, 'required' => false, 'label' => ucfirst($parameter)], $config['options']);
             if (isset($fieldConstraints[$parameter])) {
                 $options['constraints'] = $fieldConstraints[$parameter]->constraints;
             }
             $form->add($factory->createNamed($parameter, $config['type'], null, $options));
         }
     });
     $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
         $form = $event->getForm();
         $jobInstance = $form->getRoot()->getData();
         if (null === $jobInstance->getId()) {
             return;
         }
         $job = $this->jobRegistry->get($jobInstance->getJobName());
         $configProvider = $this->configProviderRegistry->get($job);
         $configs = $configProvider->getFormConfiguration();
         $data = $event->getData();
         if (isset($configs['filters']) && isset($data['filters'])) {
             $data['filters'] = json_decode($data['filters'], true);
         }
         $event->setData($data);
     });
 }
 /**
  * Get a job instance
  *
  * @param int  $id
  * @param bool $checkStatus
  *
  * @throws NotFoundHttpException
  *
  * @return JobInstance|RedirectResponse
  */
 protected function getJobInstance($id, $checkStatus = true)
 {
     $jobInstance = $this->jobInstanceRepository->find($id);
     if (null === $jobInstance) {
         throw new NotFoundHttpException('Akeneo\\Component\\Batch\\Model\\JobInstance entity not found');
     }
     // Fixme: should look at the job execution to see the status of a job instance execution
     if ($checkStatus && $jobInstance->getStatus() === JobInstance::STATUS_IN_PROGRESS) {
         throw new NotFoundHttpException(sprintf('The %s "%s" is currently in progress', $jobInstance->getType(), $jobInstance->getLabel()));
     }
     $job = $this->jobRegistry->get($jobInstance->getJobName());
     if (!$job) {
         throw new NotFoundHttpException(sprintf('The following %s does not exist anymore. Please check configuration:<br />' . 'Connector: %s<br />' . 'Type: %s<br />' . 'Alias: %s', $this->getJobType(), $jobInstance->getConnector(), $jobInstance->getType(), $jobInstance->getJobName()));
     }
     return $jobInstance;
 }
 /**
  * Get the list of jobs that are declared among all the connectors
  *
  * @return array with job alias => job
  */
 protected function getAllJobs()
 {
     $jobs = $this->jobRegistry->all();
     return $jobs;
 }
 /**
  * Return filter choices for the connector column
  *
  * @param string $type
  *
  * @return array
  */
 protected function getConnectorChoices($type)
 {
     $connectors = $this->registry->getConnectors();
     return empty($connectors) ? [] : array_combine($connectors, $connectors);
 }