/
SourceCreatedMigrationCommand.php
89 lines (76 loc) · 2.74 KB
/
SourceCreatedMigrationCommand.php
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
<?php
/*
* @copyright 2019 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace MauticPlugin\MauticCustomReportBundle\Command;
use Doctrine\ORM\EntityManager;
use Mautic\CoreBundle\Command\ModeratedCommand;
use MauticPlugin\MauticCustomReportBundle\Entity\CustomCreatedContactLog;
use phpDocumentor\Reflection\Types\Parent_;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class SourceCreatedMigrationCommand extends ModeratedCommand
{
/**
* @var EntityManager
*/
private $entityManager;
/**
* SourceCreatedMigrationCommand constructor.
*
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('mautic:source:created:contacts:migration')
->setDescription('Created contacts source migration')
->setHelp('Created contacts source migration');
parent::configure();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$key = __CLASS__;
if (!$this->checkRunStatus($input, $output, $key)) {
return 0;
}
$maxIdQuery = 'SELECT MAX(ccl.log_id) FROM '.MAUTIC_TABLE_PREFIX.CustomCreatedContactLog::TABLE.' ccl';
$maxLogId = (int) $this->entityManager->getConnection()->query($maxIdQuery)->fetchColumn();
$countQuery = 'SELECT COUNT(lel.id) FROM '.$this->getLeadEventLogQueryPart(
).' AND lel.id > '.$maxLogId;
$numberOfImportedRows = $this->entityManager->getConnection()->query($countQuery)->fetchColumn();
$query = 'INSERT INTO '.MAUTIC_TABLE_PREFIX.CustomCreatedContactLog::TABLE.' (lead_id, log_id, url, date_added)
SELECT lel.lead_id, lel.id,ph.url,lel.date_added FROM '.$this->getLeadEventLogQueryPart();
if ($maxLogId) {
$query .= ' AND lel.id > '.$maxLogId;
}
$this->entityManager->getConnection()->query($query);
$output->writeln(sprintf("Migrated %s lines", $numberOfImportedRows));
}
/**
* @return string
*/
private function getLeadEventLogQueryPart()
{
return MAUTIC_TABLE_PREFIX.'lead_event_log lel
LEFT JOIN '.MAUTIC_TABLE_PREFIX.'page_hits ph ON ph.id = lel.object_id
WHERE lel.bundle = \'page\' and lel.object = \'hit\' AND lel.action = \'created_contact\'';
}
}