HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcode/app/Console/Commands/ActiveCampaignSender.php
<?php

namespace App\Console\Commands;

use App\Helpers\ActiveCampaignHelper;
use App\Models\AbstractQueue;
use App\Models\ActiveCampaignQueue;
use App\Repository\ActiveCampaignQueueRepository;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as CommandAlias;
use Throwable;

class ActiveCampaignSender extends Command
{

    protected $signature = 'active-campaign:send';

    protected $description = 'Process ActiveCampaign queue';

    private int $errorsCount = 0;

    private function setError(AbstractQueue $queueItem, string $error): void
    {
        $this->warn($error);
        $queueItem->error($error);
        $this->errorsCount++;
    }

    public function handle(ActiveCampaignQueueRepository $activeCampaignQueueRepository, ActiveCampaignHelper $activeCampaignHelper): int
    {
        $listId = env('ACTIVE_CAMPAIGN_LIST_ID');
        $popupListId = env('ACTIVE_CAMPAIGN_POPUP_LIST_ID');
        if (!$activeCampaignHelper->check() || (empty($listId) && empty($popupListId))) {
            $this->error('Need to fill ACTIVE_CAMPAIGN_HOST, ACTIVE_CAMPAIGN_TOKEN, ACTIVE_CAMPAIGN_LIST or ACTIVE_CAMPAIGN_POPUP_LIST_ID in .env');
        }
        $this->info('Send leads to ActiveCampaign');

        $queue = $activeCampaignQueueRepository->getNeedToSend();
        $this->line('Found items: ' . $queue->count());
        $needSendDefaultListError = true;
        $needSendPopupListError = true;
        foreach ($queue as $queueItem) {
            /** @var ActiveCampaignQueue $queueItem */
            try {
                $queueItem->inProgress();
                $currentListId = $listId;

                $data = json_decode($queueItem->data, true);
                if (empty($data['listType'])) {
                    if (empty($listId)) {
                        $errorMsg = 'Need to fill ACTIVE_CAMPAIGN_LIST_ID in .env';
                        if ($needSendDefaultListError) {
                            $this->error($errorMsg);
                        }
                        $needSendDefaultListError = false;
                        $queueItem->error($errorMsg);
                        continue;
                    }
                } else {
                    switch ($data['listType']) {
                        case 'popupList':
                            if (empty($popupListId)) {
                                $errorMsg = 'Need to fill ACTIVE_CAMPAIGN_POPUP_LIST_ID in .env';
                                if ($needSendPopupListError) {
                                    $this->error($errorMsg);
                                }
                                $needSendPopupListError = false;
                                $queueItem->error($errorMsg);
                                continue 2;
                            }
                            $currentListId = $popupListId;
                            break;
                        default:
                            $this->error('Unexpected list type: ' . $data['listType'] . '(' . $queueItem->id . ')');
                            break;
                    }
                }
                unset($data['listType']);
                unset($data['tg']);

                $data['email'] = strtolower($data['email']);
                $data['phone'] = empty($data['phone']) ? null : $data['phone'];
                if (empty($data['landing_url'])) {
                    $url = explode('://', env('APP_URL'));
                    $landingUrl = $url[1] ?? $url[0] . $data['referrer'];
                    $data['landing_url'] = $landingUrl;
                }

                $response = $activeCampaignHelper->searchContact(['email' => $data['email']]);
                if (!$response->successful()) {
                    $this->setError($queueItem, 'Response error: ' . $response->status());

                    continue;
                }
                $responseData = $response->json();

                $contact = $responseData['contacts'][0] ?? null;

                if (empty($contact)) {
                    $this->line('Create person ' . $data['email']);
                    $response = $activeCampaignHelper->createContact($data);
                    if (!$response->successful()) {
                        $this->setError($queueItem, 'Response error: ' . $response->status());

                        continue;
                    }
                    $responseData = $response->json();
                    $contact = $responseData['contact'] ?? null;
                }
                if (empty($contact)) {
                    $this->setError($queueItem, 'Contact not found or create');

                    continue;
                }

                $activeCampaignHelper->subscribeContact($contact['id'], $currentListId);
                if (!$response->successful()) {
                    $this->setError($queueItem, 'Response error: ' . $response->status());

                    continue;
                }

                $queueItem->done();
            } catch (Throwable $exception) {
                $this->setError($queueItem, $exception->getMessage());
            }
        }

        if (!empty($this->errorsCount)) {
            $this->warn('Errors count: ' . $this->errorsCount);
        }

        $this->info(PHP_EOL . 'Done');

        return CommandAlias::SUCCESS;
    }

}