Spaces:
No application file
No application file
File size: 12,929 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
<?php
namespace Mautic\PluginBundle\Command;
use Mautic\PluginBundle\Helper\IntegrationHelper;
use Mautic\PluginBundle\Integration\UnifiedIntegrationInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class FetchLeadsCommand extends Command
{
public function __construct(
private TranslatorInterface $translator,
private IntegrationHelper $integrationHelper
) {
parent::__construct();
}
protected function configure()
{
$this
->setName('mautic:integration:fetchleads')
->setAliases(
[
'mautic:integration:synccontacts',
]
)
->addOption(
'--integration',
'-i',
InputOption::VALUE_REQUIRED,
'Fetch leads from integration. Integration must be enabled and authorised.',
null
)
->addOption('--start-date', '-d', InputOption::VALUE_REQUIRED, 'Set start date for updated values.')
->addOption(
'--end-date',
'-t',
InputOption::VALUE_REQUIRED,
'Set end date for updated values.'
)
->addOption(
'--fetch-all',
null,
InputOption::VALUE_NONE,
'Get all CRM contacts whatever the date is. Should be used at instance initialization only'
)
->addOption(
'--time-interval',
'-a',
InputOption::VALUE_OPTIONAL,
'Send time interval to check updates on Salesforce, it should be a correct php formatted time interval in the past eg:(10 minutes)'
)
->addOption(
'--limit',
'-l',
InputOption::VALUE_OPTIONAL,
'Number of records to process when syncing objects',
100
)
->addOption('--force', '-f', InputOption::VALUE_NONE, 'Force execution even if another process is assumed running.');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$integration = $input->getOption('integration');
$startDate = $input->getOption('start-date');
$endDate = $input->getOption('end-date');
$interval = $input->getOption('time-interval');
$limit = $input->getOption('limit');
$fetchAll = $input->getOption('fetch-all');
$leadsExecuted = $contactsExecuted = null;
// @TODO Since integration is mandatory it should really be turned into an agument, but that would not be B.C.
if (!$integration) {
throw new \RuntimeException('An integration must be specified');
}
$integrationObject = $this->integrationHelper->getIntegrationObject($integration);
if (!$integrationObject instanceof UnifiedIntegrationInterface) {
$availableIntegrations = array_filter($this->integrationHelper->getIntegrationObjects(),
fn (UnifiedIntegrationInterface $availableIntegration) => $availableIntegration->isConfigured());
throw new \RuntimeException(sprintf('The Integration "%s" is not one of the available integrations (%s)', $integration, implode(', ', array_keys($availableIntegrations))));
}
if (!$interval) {
$interval = '15 minutes';
}
$startDate = !$startDate ? date('c', strtotime('-'.$interval)) : date('c', strtotime($startDate));
$endDate = !$endDate ? date('c') : date('c', strtotime($endDate));
if (!$startDate || !$endDate) {
$output->writeln(sprintf('<info>Invalid date rage given %s -> %s</info>', $startDate, $endDate));
return 255;
}
$integrationObject = $this->integrationHelper->getIntegrationObject($integration);
if (!$integrationObject->isAuthorized()) {
$output->writeln(sprintf('<error>ERROR:</error> <info>'.$this->translator->trans('mautic.plugin.command.notauthorized').'</info>', $integration));
return 255;
}
// Tell audit log to use integration name
define('MAUTIC_AUDITLOG_USER', $integration);
$config = $integrationObject->mergeConfigToFeatureSettings();
$supportedFeatures = $integrationObject->getIntegrationSettings()->getSupportedFeatures();
defined('MAUTIC_CONSOLE_VERBOSITY') or define('MAUTIC_CONSOLE_VERBOSITY', $output->getVerbosity());
if (!isset($config['objects'])) {
$config['objects'] = [];
}
$params['start'] = $startDate;
$params['end'] = $endDate;
$params['limit'] = $limit;
$params['fetchAll'] = $fetchAll;
$params['output'] = $output;
$integrationObject->setCommandParameters($params);
// set this constant to ensure that all contacts have the same date modified time and date synced time to prevent a pull/push loop
define('MAUTIC_DATE_MODIFIED_OVERRIDE', time());
if (isset($supportedFeatures) && in_array('get_leads', $supportedFeatures)) {
if (null !== $integrationObject && method_exists($integrationObject, 'getLeads') && isset($config['objects'])) {
$output->writeln('<info>'.$this->translator->trans('mautic.plugin.command.fetch.leads', ['%integration%' => $integration]).'</info>');
$output->writeln('<comment>'.$this->translator->trans('mautic.plugin.command.fetch.leads.starting').'</comment>');
// Handle case when integration object are named "Contacts" and "Leads"
$leadObjectName = 'Lead';
if (in_array('Leads', $config['objects'])) {
$leadObjectName = 'Leads';
}
$contactObjectName = 'Contact';
if (in_array(strtolower('Contacts'), array_map(fn ($i): string => strtolower($i), $config['objects']), true)) {
$contactObjectName = 'Contacts';
}
$updated = $created = $processed = 0;
if (in_array($leadObjectName, $config['objects'])) {
$leadList = [];
$results = $integrationObject->getLeads($params, null, $leadsExecuted, $leadList, $leadObjectName);
if (is_array($results)) {
[$justUpdated, $justCreated] = $results;
$updated += (int) $justUpdated;
$created += (int) $justCreated;
} else {
$processed += (int) $results;
}
}
if (in_array(strtolower($contactObjectName), array_map(fn ($i): string => strtolower($i), $config['objects']), true)) {
$output->writeln('');
$output->writeln('<comment>'.$this->translator->trans('mautic.plugin.command.fetch.contacts.starting').'</comment>');
$contactList = [];
$results = $integrationObject->getLeads($params, null, $contactsExecuted, $contactList, $contactObjectName);
if (is_array($results)) {
[$justUpdated, $justCreated] = $results;
$updated += (int) $justUpdated;
$created += (int) $justCreated;
} else {
$processed += (int) $results;
}
}
$output->writeln('');
if ($processed) {
$output->writeln(
'<comment>'.$this->translator->trans('mautic.plugin.command.fetch.leads.events_executed', ['%events%' => $processed])
.'</comment>'."\n"
);
} else {
$output->writeln(
'<comment>'.$this->translator->trans(
'mautic.plugin.command.fetch.leads.events_executed_breakout',
['%updated%' => $updated, '%created%' => $created]
)
.'</comment>'."\n"
);
}
}
if (null !== $integrationObject && method_exists($integrationObject, 'getCompanies') && isset($config['objects'])
&& in_array(
'company',
$config['objects']
)
) {
$updated = $created = $processed = 0;
$output->writeln('<info>'.$this->translator->trans('mautic.plugin.command.fetch.companies', ['%integration%' => $integration]).'</info>');
$output->writeln('<comment>'.$this->translator->trans('mautic.plugin.command.fetch.companies.starting').'</comment>');
$results = $integrationObject->getCompanies($params);
if (is_array($results)) {
[$justUpdated, $justCreated] = $results;
$updated += (int) $justUpdated;
$created += (int) $justCreated;
} else {
$processed += (int) $results;
}
$output->writeln('');
if ($processed) {
$output->writeln(
'<comment>'.$this->translator->trans('mautic.plugin.command.fetch.companies.events_executed', ['%events%' => $processed])
.'</comment>'."\n"
);
} else {
$output->writeln(
'<comment>'.$this->translator->trans(
'mautic.plugin.command.fetch.companies.events_executed_breakout',
['%updated%' => $updated, '%created%' => $created]
)
.'</comment>'."\n"
);
}
}
}
if (isset($supportedFeatures) && in_array('push_leads', $supportedFeatures) && method_exists($integrationObject, 'pushLeads')) {
$output->writeln('<info>'.$this->translator->trans('mautic.plugin.command.pushing.leads', ['%integration%' => $integration]).'</info>');
$result = $integrationObject->pushLeads($params);
$ignored = 0;
if (4 === count($result)) {
[$updated, $created, $errored, $ignored] = $result;
} elseif (3 === count($result)) {
[$updated, $created, $errored] = $result;
} else {
$errored = '?';
[$updated, $created] = $result;
}
$output->writeln(
'<comment>'.$this->translator->trans(
'mautic.plugin.command.fetch.pushing.leads.events_executed',
[
'%updated%' => $updated,
'%created%' => $created,
'%errored%' => $errored,
'%ignored%' => $ignored,
]
)
.'</comment>'."\n"
);
if (in_array('push_companies', $supportedFeatures) && method_exists($integrationObject, 'pushCompanies')) {
$output->writeln('<info>'.$this->translator->trans('mautic.plugin.command.pushing.companies', ['%integration%' => $integration]).'</info>');
$result = $integrationObject->pushCompanies($params);
$ignored = 0;
if (4 === count($result)) {
[$updated, $created, $errored, $ignored] = $result;
} elseif (3 === count($result)) {
[$updated, $created, $errored] = $result;
} else {
$errored = '?';
[$updated, $created] = $result;
}
$output->writeln(
'<comment>'.$this->translator->trans(
'mautic.plugin.command.fetch.pushing.companies.events_executed',
[
'%updated%' => $updated,
'%created%' => $created,
'%errored%' => $errored,
'%ignored%' => $ignored,
]
)
.'</comment>'."\n"
);
}
}
return Command::SUCCESS;
}
protected static $defaultDescription = 'Fetch leads from integration.';
}
|