| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Confirmation; |
| 4 |
|
| 5 |
// phpcs:disable PSR1.Files.SideEffects |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; // Exit if accessed directly |
| 8 |
} |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\NotFoundException; |
| 11 |
use IvyForms\Entity\Confirmation\Confirmation; |
| 12 |
use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface; |
| 13 |
|
| 14 |
class ConfirmationService |
| 15 |
{ |
| 16 |
private ConfirmationRepositoryInterface $confirmationRepository; |
| 17 |
|
| 18 |
// Constructor injection for the ConfirmationRepository via PHP-DI |
| 19 |
public function __construct(ConfirmationRepositoryInterface $confirmationRepository) |
| 20 |
{ |
| 21 |
$this->confirmationRepository = $confirmationRepository; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Create a new confirmation. |
| 26 |
* |
| 27 |
* @param Confirmation $confirmationData |
| 28 |
* |
| 29 |
* @return int Confirmation ID |
| 30 |
*/ |
| 31 |
public function createConfirmation(Confirmation $confirmationData): int |
| 32 |
{ |
| 33 |
// Create confirmation via repository and return new confirmation ID |
| 34 |
return $this->confirmationRepository->add($confirmationData); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Update an existing confirmation. |
| 39 |
* |
| 40 |
* @param int $confirmationId |
| 41 |
* @param Confirmation $confirmationData |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
*/ |
| 45 |
public function updateConfirmation(int $confirmationId, Confirmation $confirmationData): bool |
| 46 |
{ |
| 47 |
return $this->confirmationRepository->update($confirmationId, $confirmationData); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get confirmations by form ID. |
| 52 |
* |
| 53 |
* @param int $id |
| 54 |
* |
| 55 |
* @return mixed[]|null |
| 56 |
* |
| 57 |
* @throws NotFoundException If no confirmations are found |
| 58 |
*/ |
| 59 |
public function getConfirmationsById(int $id): ?array |
| 60 |
{ |
| 61 |
$confirmation = $this->confirmationRepository->getAllById($id); |
| 62 |
|
| 63 |
if (!$confirmation) { |
| 64 |
throw new NotFoundException('Confirmation not found.'); |
| 65 |
} |
| 66 |
|
| 67 |
return $confirmation; |
| 68 |
} |
| 69 |
/** |
| 70 |
* Get a specific form by its ID. |
| 71 |
* |
| 72 |
* @param int $id |
| 73 |
* |
| 74 |
* @return object ConfirmationEntity |
| 75 |
* |
| 76 |
* @throws NotFoundException If the confirmation is not found |
| 77 |
*/ |
| 78 |
public function getConfirmationById(int $id): object |
| 79 |
{ |
| 80 |
$confirmation = $this->confirmationRepository->getById($id); |
| 81 |
|
| 82 |
if (!$confirmation) { |
| 83 |
throw new NotFoundException('Confirmation not found.'); |
| 84 |
} |
| 85 |
|
| 86 |
return $confirmation; |
| 87 |
} |
| 88 |
} |
| 89 |
|