diff --git a/Model/GetOrCreateEntity.php b/Model/GetOrCreateEntity.php new file mode 100644 index 0000000..be471f6 --- /dev/null +++ b/Model/GetOrCreateEntity.php @@ -0,0 +1,65 @@ +entityRepository = $entityRepository; + $this->entityModelFactory = $entityModelFactory; + } + + /** + * @param int $activityId + * @param string $identifier + * @param string $collectorType + * @return EntityInterface + * @throws Exception + */ + public function execute(int $activityId, string $identifier, string $collectorType): EntityInterface + { + $entity = $this->entityRepository->getByActivityIdAndIdentifierAndType( + $activityId, + $identifier, + $collectorType + ); + + if ($entity->getId()) { + return $entity; + } + + $entity = $this->entityModelFactory->create(); + $entity->setActivityId($activityId); + $entity->setType($collectorType); + $entity->setIdentifier($identifier); + return $entity; + } +}