File size: 4,377 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
<?php

namespace MauticPlugin\MauticSocialBundle\Command;

use MauticPlugin\MauticSocialBundle\Entity\MonitoringRepository;
use MauticPlugin\MauticSocialBundle\Model\MonitoringModel;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MauticSocialMonitoringCommand extends Command
{
    public function __construct(
        private MonitoringModel $monitoringModel
    ) {
        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('mautic:social:monitoring')
            ->addOption('mid', 'i', InputOption::VALUE_OPTIONAL, 'The id of a specific monitor record to process')
            ->addOption(
                'batch-size',
                null,
                InputOption::VALUE_REQUIRED,
                'The maximum number of iterations the cron runs per cycle. This value gets distributed by the number of monitor records published'
            )
            ->addOption('query-count', null, InputOption::VALUE_OPTIONAL, 'The number of records to search for per iteration. Default is 100.', 100);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        // get the mid from the cli
        $batchSize = $input->getOption('batch-size');

        // monitor record
        $monitorId   = $input->getOption('mid');
        $monitorList = $this->getMonitors($monitorId);

        // no mid found, quit now
        if (!$monitorList->count()) {
            $output->writeln('No published monitors found. Make sure the id you supplied is published');

            return Command::SUCCESS;
        }

        // max iterations
        $maxPerIterations = ceil($batchSize / count($monitorList));

        foreach ($monitorList as $monitor) {
            $output->writeln('Executing Monitor Item '.$monitor->getId());
            $resultCode = $this->processMonitorListItem($monitor, $maxPerIterations, $input, $output);
            $output->writeln('Result Code: '.$resultCode);
        }

        return Command::SUCCESS;
    }

    /**
     * @return \Doctrine\ORM\Tools\Pagination\Paginator
     */
    protected function getMonitors($id = null)
    {
        $filter = [
            'start' => 0,
            'limit' => 100,
        ];

        /** @var MonitoringRepository $repository */
        $repository = $this->monitoringModel->getRepository();

        if (null !== $id) {
            $filter['filter'] = [
                'force' => [
                    [
                        'column' => $repository->getTableAlias().'.id',
                        'expr'   => 'eq',
                        'value'  => (int) $id,
                    ],
                ],
            ];
        }

        return $repository->getPublishedEntities($filter);
    }

    /**
     * @return bool|int
     *
     * @throws \Exception
     */
    protected function processMonitorListItem($listItem, float $maxPerIterations, InputInterface $input, OutputInterface $output)
    {
        // @todo set this up to use the command type per-monitor record.
        $networkType = $listItem->getNetworkType();

        $commandName = '';

        // hashtag command
        if ('twitter_hashtag' == $networkType) {
            $commandName = 'social:monitor:twitter:hashtags';
        }

        // mention command
        if ('twitter_handle' == $networkType) {
            $commandName = 'social:monitor:twitter:mentions';
        }

        if ('' == $commandName) {
            $output->writeln('Matching command not found.');

            return 1;
        }

        // monitor hash command
        $command = $this->getApplication()->find($commandName);

        // create command options
        $cliArgs = [
            'command'       => $commandName,
            '--mid'         => $listItem->getId(),
            '--max-runs'    => $maxPerIterations,
            '--query-count' => $input->getOption('query-count'),
        ];

        // execute the command
        $returnCode = $command->run(new ArrayInput($cliArgs), $output);

        return $returnCode;
    }

    protected static $defaultDescription = 'Looks at the records of monitors and iterates through them. ';
}