Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 137 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| ConstantContactController | |
0.00% |
0 / 137 |
|
0.00% |
0 / 9 |
812 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| jsonResponse | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| displaySetup | |
0.00% |
0 / 50 |
|
0.00% |
0 / 1 |
56 | |||
| getStatus | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| getLists | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| selectList | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |||
| createList | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
| disconnect | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| testConnection | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\ConstantContact\Controllers; |
| 4 | |
| 5 | |
| 6 | use BuyerKiosk\ConstantContact\Controllers\ConstantContactService; |
| 7 | /** |
| 8 | * Constant Contact Controller |
| 9 | * |
| 10 | * Handles all Constant Contact integration endpoints including OAuth setup, |
| 11 | * list selection, and integration management. |
| 12 | */ |
| 13 | class ConstantContactController extends \BuyerKiosk\Core\Controllers\BaseController |
| 14 | { |
| 15 | /** @var \Store */ |
| 16 | private $store; |
| 17 | |
| 18 | /** @var \Slim\Slim */ |
| 19 | private $app; |
| 20 | |
| 21 | /** @var ConstantContactService */ |
| 22 | private $ccService; |
| 23 | |
| 24 | /** |
| 25 | * Constructor |
| 26 | * |
| 27 | * @param \Slim\Slim $app |
| 28 | * @param \Store $store |
| 29 | */ |
| 30 | public function __construct($app, \Store $store) |
| 31 | { |
| 32 | $this->store = $store; |
| 33 | $this->app = $app; |
| 34 | $this->ccService = new ConstantContactService($store); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Output JSON response with proper content type |
| 39 | * |
| 40 | * @param array $data |
| 41 | */ |
| 42 | private function jsonResponse(array $data): void |
| 43 | { |
| 44 | $this->app->response->headers->set('Content-Type', 'application/json'); |
| 45 | echo json_encode($data); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Display the Constant Contact setup/configuration page |
| 50 | */ |
| 51 | public function displaySetup(): void |
| 52 | { |
| 53 | // Check if OAuth credentials are configured |
| 54 | if (!$this->ccService->isConfigured()) { |
| 55 | $this->app->render('constantcontact/setup.html', [ |
| 56 | 'page' => [ |
| 57 | 'author' => $this->app->site->author, |
| 58 | 'title' => "Constant Contact | Setup", |
| 59 | 'description' => "Connect your Constant Contact account", |
| 60 | 'alerts' => $this->app->alerts->getAndClearMessages(), |
| 61 | ], |
| 62 | 'groups' => $this->app->user->getGroups(), |
| 63 | 'box_title' => "Constant Contact Setup", |
| 64 | 'store' => getStoreInfo($this->store), |
| 65 | 'configError' => true, |
| 66 | 'errorMessage' => 'Constant Contact integration is not configured. Please contact support.' |
| 67 | ]); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $authUrl = $this->ccService->getAuthorizationUrl(); |
| 72 | $isConnected = $this->ccService->isConnected(); |
| 73 | $accountInfo = null; |
| 74 | $lists = []; |
| 75 | $selectedList = null; |
| 76 | |
| 77 | if ($isConnected) { |
| 78 | try { |
| 79 | // Get lists (account info requires account_read scope which we don't request) |
| 80 | $lists = $this->ccService->getLists(); |
| 81 | |
| 82 | // Get selected list details |
| 83 | $selectedListId = $this->store->getCcList(); |
| 84 | if ($selectedListId) { |
| 85 | foreach ($lists as $list) { |
| 86 | if ($list['list_id'] === $selectedListId) { |
| 87 | $selectedList = $list; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } catch (\Exception $e) { |
| 93 | // API error - token may be expired or invalid |
| 94 | error_log("Constant Contact API error for store " . $this->store->getTypeNum() . ": " . $e->getMessage()); |
| 95 | $accountInfo = null; |
| 96 | $lists = []; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | $this->app->render('constantcontact/setup.html', [ |
| 101 | 'page' => [ |
| 102 | 'author' => $this->app->site->author, |
| 103 | 'title' => "Constant Contact | Setup", |
| 104 | 'description' => "Connect your Constant Contact account", |
| 105 | 'alerts' => $this->app->alerts->getAndClearMessages(), |
| 106 | ], |
| 107 | 'groups' => $this->app->user->getGroups(), |
| 108 | 'box_title' => "Constant Contact Setup", |
| 109 | 'store' => getStoreInfo($this->store), |
| 110 | 'authUrl' => $authUrl, |
| 111 | 'isConnected' => $isConnected, |
| 112 | 'accountInfo' => $accountInfo, |
| 113 | 'lists' => $lists, |
| 114 | 'selectedList' => $selectedList, |
| 115 | 'selectedListId' => $this->store->getCcList(), |
| 116 | 'appDomain' => ConstantContactService::getAppDomain() |
| 117 | ]); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * API: Get connection status |
| 122 | */ |
| 123 | public function getStatus(): void |
| 124 | { |
| 125 | $response = [ |
| 126 | 'connected' => $this->ccService->isConnected(), |
| 127 | 'enabled' => (bool)$this->store->getCcEnabled(), |
| 128 | 'selectedListId' => $this->store->getCcList() |
| 129 | ]; |
| 130 | |
| 131 | if ($response['connected']) { |
| 132 | $accountInfo = $this->ccService->getAccountInfo(); |
| 133 | if ($accountInfo) { |
| 134 | $response['accountName'] = $accountInfo['organization_name'] ?? null; |
| 135 | $response['email'] = $accountInfo['contact_email'] ?? null; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | $this->jsonResponse($response); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * API: Get all contact lists |
| 144 | */ |
| 145 | public function getLists(): void |
| 146 | { |
| 147 | if (!$this->ccService->isConnected()) { |
| 148 | $this->app->response->setStatus(401); |
| 149 | $this->jsonResponse(['success' => false, 'error' => 'Constant Contact not connected']); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | $lists = $this->ccService->getLists(); |
| 154 | |
| 155 | $this->jsonResponse([ |
| 156 | 'success' => true, |
| 157 | 'lists' => $lists |
| 158 | ]); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * API: Select a list for this store |
| 163 | */ |
| 164 | public function selectList(): void |
| 165 | { |
| 166 | if (!$this->ccService->isConnected()) { |
| 167 | $this->app->response->setStatus(401); |
| 168 | $this->jsonResponse(['success' => false, 'error' => 'Constant Contact not connected']); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | $listId = $this->app->request->post('listId'); |
| 173 | |
| 174 | if (empty($listId)) { |
| 175 | $this->app->response->setStatus(400); |
| 176 | $this->jsonResponse(['success' => false, 'error' => 'List ID is required']); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // Verify the list exists |
| 181 | $list = $this->ccService->getList($listId); |
| 182 | if (!$list) { |
| 183 | $this->app->response->setStatus(400); |
| 184 | $this->jsonResponse(['success' => false, 'error' => 'List not found']); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // Update the store's selected list |
| 189 | $this->store->setCcList($listId); |
| 190 | if ($this->store->updateCcList()) { |
| 191 | $this->jsonResponse([ |
| 192 | 'success' => true, |
| 193 | 'message' => 'List selected successfully', |
| 194 | 'list' => $list |
| 195 | ]); |
| 196 | } else { |
| 197 | $this->app->response->setStatus(500); |
| 198 | $this->jsonResponse(['success' => false, 'error' => 'Failed to save list selection']); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * API: Create a new list |
| 204 | */ |
| 205 | public function createList(): void |
| 206 | { |
| 207 | if (!$this->ccService->isConnected()) { |
| 208 | $this->app->response->setStatus(401); |
| 209 | $this->jsonResponse(['success' => false, 'error' => 'Constant Contact not connected']); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | $name = $this->app->request->post('name'); |
| 214 | $description = $this->app->request->post('description'); |
| 215 | |
| 216 | if (empty($name)) { |
| 217 | $this->app->response->setStatus(400); |
| 218 | $this->jsonResponse(['success' => false, 'error' => 'List name is required']); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | $list = $this->ccService->createList($name, $description); |
| 223 | |
| 224 | if ($list) { |
| 225 | $this->jsonResponse([ |
| 226 | 'success' => true, |
| 227 | 'message' => 'List created successfully', |
| 228 | 'list' => $list |
| 229 | ]); |
| 230 | } else { |
| 231 | $this->app->response->setStatus(500); |
| 232 | $this->jsonResponse(['success' => false, 'error' => 'Failed to create list']); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * API: Disconnect Constant Contact |
| 238 | */ |
| 239 | public function disconnect(): void |
| 240 | { |
| 241 | if ($this->ccService->disconnect()) { |
| 242 | $this->jsonResponse([ |
| 243 | 'success' => true, |
| 244 | 'message' => 'Constant Contact has been disconnected' |
| 245 | ]); |
| 246 | } else { |
| 247 | $this->app->response->setStatus(500); |
| 248 | $this->jsonResponse(['success' => false, 'error' => 'Failed to disconnect']); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * API: Test the connection by fetching account info |
| 254 | */ |
| 255 | public function testConnection(): void |
| 256 | { |
| 257 | if (!$this->ccService->isConnected()) { |
| 258 | $this->app->response->setStatus(401); |
| 259 | $this->jsonResponse(['success' => false, 'error' => 'Constant Contact not connected']); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | $accountInfo = $this->ccService->getAccountInfo(); |
| 264 | |
| 265 | if ($accountInfo) { |
| 266 | $this->jsonResponse([ |
| 267 | 'success' => true, |
| 268 | 'message' => 'Connection is working', |
| 269 | 'account' => $accountInfo |
| 270 | ]); |
| 271 | } else { |
| 272 | $this->app->response->setStatus(500); |
| 273 | $this->jsonResponse(['success' => false, 'error' => 'Failed to connect to Constant Contact']); |
| 274 | } |
| 275 | } |
| 276 | } |