| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace IvyForms\Services\Permissions; |
| 8 |
|
| 9 |
// phpcs:disable PSR1.Files.SideEffects |
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface; |
| 15 |
use IvyForms\Repository\Entry\EntryRepositoryInterface; |
| 16 |
use IvyForms\Repository\Notification\NotificationRepositoryInterface; |
| 17 |
|
| 18 |
/** |
| 19 |
* Resolves owning form IDs for REST resources keyed by entity id (entry, notification, confirmation). |
| 20 |
*/ |
| 21 |
final class PermissionResourceResolver |
| 22 |
{ |
| 23 |
private NotificationRepositoryInterface $notificationRepository; |
| 24 |
|
| 25 |
private EntryRepositoryInterface $entryRepository; |
| 26 |
|
| 27 |
private ConfirmationRepositoryInterface $confirmationRepository; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
NotificationRepositoryInterface $notificationRepository, |
| 31 |
EntryRepositoryInterface $entryRepository, |
| 32 |
ConfirmationRepositoryInterface $confirmationRepository |
| 33 |
) { |
| 34 |
$this->notificationRepository = $notificationRepository; |
| 35 |
$this->entryRepository = $entryRepository; |
| 36 |
$this->confirmationRepository = $confirmationRepository; |
| 37 |
} |
| 38 |
|
| 39 |
public function formIdFromNotificationId(int $notificationId): ?int |
| 40 |
{ |
| 41 |
if ($notificationId <= 0) { |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
$notification = $this->notificationRepository->getById($notificationId); |
| 46 |
if ($notification === null || !method_exists($notification, 'getFormId')) { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
$formId = (int) $notification->getFormId(); |
| 51 |
|
| 52 |
return $formId > 0 ? $formId : null; |
| 53 |
} |
| 54 |
|
| 55 |
public function formIdFromEntryId(int $entryId): ?int |
| 56 |
{ |
| 57 |
if ($entryId <= 0) { |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
$entry = $this->entryRepository->getById($entryId); |
| 62 |
if ($entry === null || !method_exists($entry, 'getFormId')) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$formId = (int) $entry->getFormId(); |
| 67 |
|
| 68 |
return $formId > 0 ? $formId : null; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Resolve owning form IDs for many entries in a single query. |
| 73 |
* |
| 74 |
* @param list<int|string> $entryIds |
| 75 |
* @return array<int, int> entryId => formId (only found entries with a positive formId) |
| 76 |
*/ |
| 77 |
public function formIdsFromEntryIds(array $entryIds): array |
| 78 |
{ |
| 79 |
$ids = []; |
| 80 |
foreach ($entryIds as $entryId) { |
| 81 |
$id = (int) $entryId; |
| 82 |
if ($id > 0) { |
| 83 |
$ids[] = $id; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if ($ids === []) { |
| 88 |
return []; |
| 89 |
} |
| 90 |
|
| 91 |
return $this->entryRepository->getFormIdsByEntryIds($ids); |
| 92 |
} |
| 93 |
|
| 94 |
public function formIdFromConfirmationId(int $confirmationId): ?int |
| 95 |
{ |
| 96 |
if ($confirmationId <= 0) { |
| 97 |
return null; |
| 98 |
} |
| 99 |
|
| 100 |
$confirmation = $this->confirmationRepository->getById($confirmationId); |
| 101 |
if ($confirmation === null || !method_exists($confirmation, 'getFormId')) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
$formId = (int) $confirmation->getFormId(); |
| 106 |
|
| 107 |
return $formId > 0 ? $formId : null; |
| 108 |
} |
| 109 |
} |
| 110 |
|