| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\GoogleSheets; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 6 |
use IvyForms\Entity\GoogleSheets\GoogleSheetIntegration; |
| 7 |
use IvyForms\Services\Translations\BackendStrings; |
| 8 |
use IvyForms\Repository\GoogleSheets\GoogleSheetsRepositoryInterface; |
| 9 |
use IvyForms\ValueObjects\GoogleSheets\GoogleSheetIntegration as GoogleSheetIntegrationValueObject; |
| 10 |
|
| 11 |
/** |
| 12 |
* Google Sheets integration CRUD manager. |
| 13 |
*/ |
| 14 |
class GoogleSheetsManagerService |
| 15 |
{ |
| 16 |
private GoogleSheetsRepositoryInterface $repository; |
| 17 |
|
| 18 |
public function __construct(GoogleSheetsRepositoryInterface $repository) |
| 19 |
{ |
| 20 |
$this->repository = $repository; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @param list<array{column: string, value: string}> $fieldMapping |
| 25 |
* @param array<string, mixed> $smartLogic |
| 26 |
* |
| 27 |
* @SuppressWarnings("BooleanArgumentFlag") |
| 28 |
*/ |
| 29 |
public function createIntegration( |
| 30 |
int $formId, |
| 31 |
string $spreadsheetId, |
| 32 |
string $spreadsheetName, |
| 33 |
string $worksheetName, |
| 34 |
array $fieldMapping = [], |
| 35 |
array $smartLogic = [], |
| 36 |
bool $enabled = true |
| 37 |
): GoogleSheetIntegration { |
| 38 |
$this->validateIntegrationLimit($formId); |
| 39 |
|
| 40 |
$valueObject = new GoogleSheetIntegrationValueObject( |
| 41 |
null, |
| 42 |
$formId, |
| 43 |
$spreadsheetId, |
| 44 |
$spreadsheetName, |
| 45 |
$worksheetName, |
| 46 |
$fieldMapping, |
| 47 |
$smartLogic, |
| 48 |
$enabled, |
| 49 |
null, |
| 50 |
null |
| 51 |
); |
| 52 |
|
| 53 |
$integration = new GoogleSheetIntegration($valueObject); |
| 54 |
$integrationId = $this->repository->add($integration); |
| 55 |
$integration->setId($integrationId); |
| 56 |
|
| 57 |
return $integration; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @param array<string, mixed> $data |
| 62 |
*/ |
| 63 |
public function updateIntegration(int $id, array $data): GoogleSheetIntegration |
| 64 |
{ |
| 65 |
$integration = $this->repository->findById($id); |
| 66 |
|
| 67 |
if (!$integration) { |
| 68 |
throw new InvalidArgumentException('Google Sheets integration not found'); |
| 69 |
} |
| 70 |
|
| 71 |
if (isset($data['spreadsheet_id'])) { |
| 72 |
$integration->setSpreadsheetId((string) $data['spreadsheet_id']); |
| 73 |
} |
| 74 |
|
| 75 |
if (isset($data['spreadsheet_name'])) { |
| 76 |
$integration->setSpreadsheetName((string) $data['spreadsheet_name']); |
| 77 |
} |
| 78 |
|
| 79 |
if (isset($data['worksheet_name'])) { |
| 80 |
$integration->setWorksheetName((string) $data['worksheet_name']); |
| 81 |
} |
| 82 |
|
| 83 |
if (isset($data['field_mapping'])) { |
| 84 |
$integration->setFieldMapping((array) $data['field_mapping']); |
| 85 |
} |
| 86 |
|
| 87 |
if (isset($data['smart_logic'])) { |
| 88 |
$integration->setSmartLogic((array) $data['smart_logic']); |
| 89 |
} |
| 90 |
|
| 91 |
if (isset($data['enabled'])) { |
| 92 |
$integration->setEnabled((bool) $data['enabled']); |
| 93 |
} |
| 94 |
|
| 95 |
$this->repository->update($integration->getId(), $integration); |
| 96 |
|
| 97 |
return $integration; |
| 98 |
} |
| 99 |
|
| 100 |
public function deleteIntegration(int $id): bool |
| 101 |
{ |
| 102 |
return $this->repository->deleteById($id); |
| 103 |
} |
| 104 |
|
| 105 |
public function updateStatus(int $id): GoogleSheetIntegration |
| 106 |
{ |
| 107 |
$integration = $this->repository->findById($id); |
| 108 |
|
| 109 |
if (!$integration) { |
| 110 |
throw new InvalidArgumentException('Google Sheets integration not found'); |
| 111 |
} |
| 112 |
|
| 113 |
$integration->setEnabled(!$integration->isEnabled()); |
| 114 |
$this->repository->update($integration->getId(), $integration); |
| 115 |
|
| 116 |
return $integration; |
| 117 |
} |
| 118 |
|
| 119 |
public function getIntegration(int $id): ?GoogleSheetIntegration |
| 120 |
{ |
| 121 |
return $this->repository->findById($id); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @return array<GoogleSheetIntegration> |
| 126 |
*/ |
| 127 |
public function getIntegrationsByFormId(int $formId): array |
| 128 |
{ |
| 129 |
return $this->repository->findByFormId($formId); |
| 130 |
} |
| 131 |
|
| 132 |
public function getMaxIntegrationsPerForm(): int |
| 133 |
{ |
| 134 |
return (int) apply_filters('ivyforms/google_sheets/max_per_form', 1); |
| 135 |
} |
| 136 |
|
| 137 |
protected function validateIntegrationLimit(int $formId, ?int $excludeId = null): void |
| 138 |
{ |
| 139 |
$maxPerForm = $this->getMaxIntegrationsPerForm(); |
| 140 |
$count = $this->repository->countByFormId($formId); |
| 141 |
|
| 142 |
if ($excludeId !== null) { |
| 143 |
$count--; |
| 144 |
} |
| 145 |
|
| 146 |
if ($count >= $maxPerForm) { |
| 147 |
throw new InvalidArgumentException( |
| 148 |
str_replace( |
| 149 |
'{limit}', |
| 150 |
(string) $maxPerForm, |
| 151 |
BackendStrings::getIntegrationsStrings()['google_sheets_limit_reached'] |
| 152 |
) |
| 153 |
); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|