# ivyforms/0.3/backend/src/Services/Notification/NotificationService.php

The Innovative Form Builder – IvyForms, version 0.3. 195 lines.

- Page: https://pluginprobe.com/plugins/ivyforms/0.3/code/backend/src/Services/Notification/NotificationService.php
- Raw: https://pluginprobe.com/plugins/ivyforms/0.3/raw/backend/src/Services/Notification/NotificationService.php
- Modified: 2025-08-20T10:20:12+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.3/code/backend/src/Services/Notification/NotificationService.php#L10-L20`.

```php
<?php

namespace IvyForms\Services\Notification;

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

use IvyForms\Common\Exceptions\InvalidArgumentException;
use IvyForms\Common\Exceptions\NotFoundException;
use IvyForms\Entity\Notification\Notification;
use IvyForms\Factory\Notification\NotificationFactory;
use IvyForms\Repository\Notification\NotificationRepositoryInterface;
use IvyForms\Services\Translations\BackendStrings;

class NotificationService
{
    private NotificationRepositoryInterface $notificationRepository;

    // Constructor injection for the NotificationRepository via PHP-DI
    public function __construct(NotificationRepositoryInterface $notificationRepository)
    {
        $this->notificationRepository = $notificationRepository;
    }

    /**
     * Get all notifications from the repository.
     *
     * @return array<mixed>|null
     */
    public function getAllNotifications(): ?array
    {
        return $this->notificationRepository->getAll();
    }

    /**
     * Create a new notification.
     *
     * @param Notification $notificationData
     *
     * @return int Notification ID
     */
    public function createNotification(Notification $notificationData): int
    {
        // Create notification via repository and return new notification ID
        return $this->notificationRepository->add($notificationData);
    }

    /**
     * Get a specific notification by its ID.
     *
     * @param int $notificationId
     *
     * @return object NotificationEntity
     *
     * @throws NotFoundException If the notification is not found
     */
    public function getNotificationById(int $notificationId): object
    {
        $notification = $this->notificationRepository->getById($notificationId);

        if (!$notification) {
            throw new NotFoundException(
                BackendStrings::getExceptionStrings()['notification_not_found']
            );
        }

        return $notification;
    }

    /**
     * Update an existing notification.
     *
     * @param int $notificationId
     * @param Notification $notificationData
     *
     * @return bool
     */
    public function updateNotification(int $notificationId, Notification $notificationData): bool
    {
        // Validate notification data before updating

        return $this->notificationRepository->update($notificationId, $notificationData);
    }

    /**
     * Delete a notification by its ID.
     *
     * @param int $notificationId
     *
     * @return int
     */
    public function deleteNotification(int $notificationId): int
    {
        return $this->notificationRepository->delete($notificationId);
    }
    /**
     * Get notifications by form ID.
     *
     * @param int $formId
     *
     * @return array<Notification>
     *
     * @throws NotFoundException If no notifications are found
     */
    public function getNotificationsByFormId(int $formId): array
    {
        $notification = $this->notificationRepository->getAllById($formId);

        if (!$notification) {
            throw new NotFoundException(
                BackendStrings::getExceptionStrings()['notification_not_found']
            );
        }

        return $notification;
    }

    /**
     * Get active notifications by form ID.
     *
     * @param int $formId
     *
     * @return array<Notification>
     *
     * @throws NotFoundException If no active notifications are found
     */
    public function getActiveNotificationsByFormId(int $formId): array
    {
        $notification = $this->notificationRepository->getAllActiveById($formId);

        if (!$notification) {
            throw new NotFoundException(
                BackendStrings::getExceptionStrings()['active_notif_not_found']
            );
        }

        return $notification;
    }

    /**
     *
     * Delete notifications by form ID.
     *
     * @throws NotFoundException
     */
    public function deleteNotificationsByFormId(int $formId): void
    {
        $notifications = $this->getNotificationsByFormId($formId);

        if (!$notifications) {
            return;
        }

        foreach ($notifications as $notification) {
            $this->deleteNotification($notification->getId());
        }
    }

    /**
     * Duplicate notifications from one form to another.
     *
     * @param int $originalFormId
     * @param int $newFormId
     *
     * @return void
     * @throws NotFoundException
     * @throws InvalidArgumentException
     */
    public function duplicateNotifications(int $originalFormId, int $newFormId): void
    {
        $notifications = $this->getNotificationsByFormId($originalFormId);
        foreach ($notifications as $notification) {
            $notificationData = $notification->toArray();
            unset($notificationData['id']);
            $notificationData['formId'] = $newFormId;

            $this->createNotification(NotificationFactory::create($notificationData));
        }
    }

    /**
     * Search notifications with pagination and sorting
     *
     * @param array<string, mixed> $params
     *
     * @return array<string, mixed>
     */
    public function searchNotifications(array $params): array
    {
        return $this->notificationRepository->search($params);
    }
}

```
