PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
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.3, at backend/src/Services/Confirmation/ConfirmationService.php

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