Skip to content

Commit

Permalink
improve: add GetOrCreateEntity.php
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloargentiero committed Feb 21, 2023
1 parent ec6c511 commit 712d648
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Model/GetOrCreateEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/*
* Copyright © Ghost Unicorns snc. All rights reserved.
* See LICENSE for license details.
*/

declare(strict_types=1);

namespace GhostUnicorns\CrtEntity\Model;

use Exception;
use GhostUnicorns\CrtEntity\Api\Data\EntityInterface;
use GhostUnicorns\CrtEntity\Api\EntityRepositoryInterface;
use GhostUnicorns\CrtEntity\Model\EntityModelFactory;

class GetOrCreateEntity
{
/**
* @var EntityRepositoryInterface
*/
private $entityRepository;

/**
* @var EntityModelFactory
*/
private $entityModelFactory;

/**
* @param EntityRepositoryInterface $entityRepository
* @param EntityModelFactory $entityModelFactory
*/
public function __construct(
EntityRepositoryInterface $entityRepository,
EntityModelFactory $entityModelFactory
) {
$this->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;
}
}

0 comments on commit 712d648

Please sign in to comment.