File size: 1,943 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
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Command;

use Doctrine\DBAL\Exception as DBALException;
use Mautic\CoreBundle\Entity\AuditLogRepository;
use Mautic\CoreBundle\Entity\IpAddressRepository;
use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class AnonymizeIpCommand extends Command
{
    /**
     * @var string
     */
    public const COMMAND_NAME = 'mautic:anonymize:ip';

    public function __construct(private IpAddressRepository $ipAddressRepository, private CoreParametersHelper $coreParametersHelper, private AuditLogRepository $auditLogRepository)
    {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this->setName(self::COMMAND_NAME)
            ->setDescription('Delete all stored ip addresses.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        if (!$this->coreParametersHelper->get('anonymize_ip')) {
            return $this->exitWithError('Anonymization could not be done because anonymize Ip feature is disabled for this instance.', $output);
        }
        try {
            $anonymizedRows = $this->ipAddressRepository->anonymizeAllIpAddress();
            $anonymizedRows += $this->auditLogRepository->anonymizeAllIpAddress();
            $output->writeln(sprintf('<info>%s IP addresses have been anonymized</info>', $anonymizedRows));
        } catch (DBALException $e) {
            return $this->exitWithError(sprintf('Anonymization of IP addresses failed because of database error: %s', $e->getMessage()), $output);
        }

        return 0;
    }

    private function exitWithError(string $message, OutputInterface $output): int
    {
        $output->writeln(sprintf('<error>%s</error>', $message));

        return 1;
    }
}