PluginProbe
The Innovative Form Builder – IvyForms / 0.2
The Innovative Form Builder – IvyForms v0.2
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Confirmation / ConfirmationService.php

ConfirmationService.php in The Innovative Form Builder – IvyForms 0.2, at backend/src/Services/Confirmation/ConfirmationService.php

132 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Factory\Confirmation\ConfirmationFactory;
13 use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface;
14
15 class ConfirmationService
16 {
17 private ConfirmationRepositoryInterface $confirmationRepository;
18
19 // Constructor injection for the ConfirmationRepository via PHP-DI
20 public function __construct(ConfirmationRepositoryInterface $confirmationRepository)
21 {
22 $this->confirmationRepository = $confirmationRepository;
23 }
24
25 /**
26 * Create a new confirmation.
27 *
28 * @param Confirmation $confirmationData
29 *
30 * @return int Confirmation ID
31 */
32 public function createConfirmation(Confirmation $confirmationData): int
33 {
34 // Create confirmation via repository and return new confirmation ID
35 return $this->confirmationRepository->add($confirmationData);
36 }
37
38 /**
39 * Update an existing confirmation.
40 *
41 * @param int $confirmationId
42 * @param Confirmation $confirmationData
43 *
44 * @return bool
45 */
46 public function updateConfirmation(int $confirmationId, Confirmation $confirmationData): bool
47 {
48 return $this->confirmationRepository->update($confirmationId, $confirmationData);
49 }
50
51 /**
52 * Get confirmations by form ID.
53 *
54 * @param int $id
55 *
56 * @return mixed[]|null
57 *
58 * @throws NotFoundException If no confirmations are found
59 */
60 public function getConfirmationsById(int $id): ?array
61 {
62 $confirmation = $this->confirmationRepository->getAllById($id);
63
64 if (!$confirmation) {
65 throw new NotFoundException('Confirmation not found.');
66 }
67
68 return $confirmation;
69 }
70 /**
71 * Get a specific form by its ID.
72 *
73 * @param int $id
74 *
75 * @return object ConfirmationEntity
76 *
77 * @throws NotFoundException If the confirmation is not found
78 */
79 public function getConfirmationById(int $id): object
80 {
81 $confirmation = $this->confirmationRepository->getById($id);
82
83 if (!$confirmation) {
84 throw new NotFoundException('Confirmation not found.');
85 }
86
87 return $confirmation;
88 }
89
90 /**
91 * Delete confirmations by form ID.
92 *
93 * @param int $id
94 *
95 * @return void
96 * @throws NotFoundException
97 */
98 public function deleteConfirmationsByFormId(int $id): void
99 {
100 $confirmations = $this->getConfirmationsById($id);
101
102 if (!$confirmations) {
103 return;
104 }
105
106 foreach ($confirmations as $confirmation) {
107 $this->confirmationRepository->delete($confirmation->getId());
108 }
109 }
110
111 /**
112 * Duplicate confirmations from one form to another.
113 *
114 * @param int $originalFormId
115 * @param int $newFormId
116 *
117 * @return void
118 * @throws NotFoundException
119 */
120 public function duplicateConfirmations(int $originalFormId, int $newFormId): void
121 {
122 $confirmations = $this->getConfirmationsById($originalFormId);
123 foreach ($confirmations as $confirmation) {
124 $confirmationData = $confirmation->toArray();
125 unset($confirmationData['id']);
126 $confirmationData['formId'] = $newFormId;
127
128 $this->createConfirmation(ConfirmationFactory::create($confirmationData));
129 }
130 }
131 }
132