# ivyforms/0.2/backend/src/Services/Confirmation/ConfirmationService.php

The Innovative Form Builder – IvyForms, version 0.2. 132 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.2/code/backend/src/Services/Confirmation/ConfirmationService.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.2/raw/backend/src/Services/Confirmation/ConfirmationService.php
- Modified: 2025-07-24T13:31:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/ivyforms/0.2/code/backend/src/Services/Confirmation/ConfirmationService.php#L10-L20`.

```php
<?php

namespace IvyForms\Services\Confirmation;

// phpcs:disable PSR1.Files.SideEffects
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

use IvyForms\Common\Exceptions\NotFoundException;
use IvyForms\Entity\Confirmation\Confirmation;
use IvyForms\Factory\Confirmation\ConfirmationFactory;
use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface;

class ConfirmationService
{
    private ConfirmationRepositoryInterface $confirmationRepository;

    // Constructor injection for the ConfirmationRepository via PHP-DI
    public function __construct(ConfirmationRepositoryInterface $confirmationRepository)
    {
        $this->confirmationRepository = $confirmationRepository;
    }

    /**
     * Create a new confirmation.
     *
     * @param Confirmation $confirmationData
     *
     * @return int Confirmation ID
     */
    public function createConfirmation(Confirmation $confirmationData): int
    {
        // Create confirmation via repository and return new confirmation ID
        return $this->confirmationRepository->add($confirmationData);
    }

    /**
     * Update an existing confirmation.
     *
     * @param int $confirmationId
     * @param Confirmation $confirmationData
     *
     * @return bool
     */
    public function updateConfirmation(int $confirmationId, Confirmation $confirmationData): bool
    {
        return $this->confirmationRepository->update($confirmationId, $confirmationData);
    }

    /**
     * Get confirmations by form ID.
     *
     * @param int $id
     *
     * @return mixed[]|null
     *
     * @throws NotFoundException If no confirmations are found
     */
    public function getConfirmationsById(int $id): ?array
    {
        $confirmation = $this->confirmationRepository->getAllById($id);

        if (!$confirmation) {
            throw new NotFoundException('Confirmation not found.');
        }

        return $confirmation;
    }
    /**
     * Get a specific form by its ID.
     *
     * @param int $id
     *
     * @return object ConfirmationEntity
     *
     * @throws NotFoundException If the confirmation is not found
     */
    public function getConfirmationById(int $id): object
    {
        $confirmation = $this->confirmationRepository->getById($id);

        if (!$confirmation) {
            throw new NotFoundException('Confirmation not found.');
        }

        return $confirmation;
    }

    /**
     * Delete confirmations by form ID.
     *
     * @param int $id
     *
     * @return void
     * @throws NotFoundException
     */
    public function deleteConfirmationsByFormId(int $id): void
    {
        $confirmations = $this->getConfirmationsById($id);

        if (!$confirmations) {
            return;
        }

        foreach ($confirmations as $confirmation) {
            $this->confirmationRepository->delete($confirmation->getId());
        }
    }

    /**
     * Duplicate confirmations from one form to another.
     *
     * @param int $originalFormId
     * @param int $newFormId
     *
     * @return void
     * @throws NotFoundException
     */
    public function duplicateConfirmations(int $originalFormId, int $newFormId): void
    {
        $confirmations = $this->getConfirmationsById($originalFormId);
        foreach ($confirmations as $confirmation) {
            $confirmationData = $confirmation->toArray();
            unset($confirmationData['id']);
            $confirmationData['formId'] = $newFormId;

            $this->createConfirmation(ConfirmationFactory::create($confirmationData));
        }
    }
}

```
