Issues (3627)

LeadBundle/Command/UpdateLeadListsCommand.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\LeadBundle\Command;
13
14
use Mautic\CoreBundle\Command\ModeratedCommand;
15
use Mautic\LeadBundle\Segment\Query\QueryException;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class UpdateLeadListsCommand extends ModeratedCommand
21
{
22
    protected function configure()
23
    {
24
        $this
25
            ->setName('mautic:segments:update')
26
            ->setAliases(['mautic:segments:rebuild'])
27
            ->setDescription('Update contacts in smart segments based on new contact data.')
28
            ->addOption('--batch-limit', '-b', InputOption::VALUE_OPTIONAL, 'Set batch size of contacts to process per round. Defaults to 300.', 300)
29
            ->addOption(
30
                '--max-contacts',
31
                '-m',
32
                InputOption::VALUE_OPTIONAL,
33
                'Set max number of contacts to process per segment for this script execution. Defaults to all.',
34
                false
35
            )
36
            ->addOption('--list-id', '-i', InputOption::VALUE_OPTIONAL, 'Specific ID to rebuild. Defaults to all.', false);
37
38
        parent::configure();
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $container  = $this->getContainer();
44
        $translator = $container->get('translator');
45
46
        /** @var \Mautic\LeadBundle\Model\ListModel $listModel */
47
        $listModel = $container->get('mautic.lead.model.list');
48
49
        $id    = $input->getOption('list-id');
50
        $batch = $input->getOption('batch-limit');
51
        $max   = $input->getOption('max-contacts');
52
53
        if (!$this->checkRunStatus($input, $output, $id)) {
54
            return 0;
55
        }
56
57
        if ($id) {
58
            $list = $listModel->getEntity($id);
59
60
            if (null !== $list) {
61
                if ($list->isPublished()) {
62
                    $output->writeln('<info>'.$translator->trans('mautic.lead.list.rebuild.rebuilding', ['%id%' => $id]).'</info>');
63
                    $processed = 0;
0 ignored issues
show
The assignment to $processed is dead and can be removed.
Loading history...
64
                    try {
65
                        $processed = $listModel->rebuildListLeads($list, $batch, $max, $output);
66
                    } catch (QueryException $e) {
67
                        $this->getContainer()->get('monolog.logger.mautic')->error('Query Builder Exception: '.$e->getMessage());
68
                    }
69
70
                    $output->writeln(
71
                        '<comment>'.$translator->trans('mautic.lead.list.rebuild.leads_affected', ['%leads%' => $processed]).'</comment>'
72
                    );
73
                }
74
            } else {
75
                $output->writeln('<error>'.$translator->trans('mautic.lead.list.rebuild.not_found', ['%id%' => $id]).'</error>');
76
            }
77
        } else {
78
            $lists = $listModel->getEntities(
79
                [
80
                    'iterator_mode' => true,
81
                ]
82
            );
83
84
            while (false !== ($l = $lists->next())) {
85
                // Get first item; using reset as the key will be the ID and not 0
86
                $l = reset($l);
87
88
                if ($l->isPublished()) {
89
                    $output->writeln('<info>'.$translator->trans('mautic.lead.list.rebuild.rebuilding', ['%id%' => $l->getId()]).'</info>');
90
91
                    $processed = $listModel->rebuildListLeads($l, $batch, $max, $output);
92
                    $output->writeln(
93
                        '<comment>'.$translator->trans('mautic.lead.list.rebuild.leads_affected', ['%leads%' => $processed]).'</comment>'."\n"
94
                    );
95
                }
96
97
                unset($l);
98
            }
99
100
            unset($lists);
101
        }
102
103
        $this->completeRun();
104
105
        return 0;
106
    }
107
}
108