Création du module PurgeFakeCustomer
This commit is contained in:
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Laurent LE CORRE <laurent@thecoredev.fr>
|
||||||
|
* Date: 12/01/2021
|
||||||
|
*/
|
||||||
|
namespace PurgeFakeCustomer\Command;
|
||||||
|
|
||||||
|
use PurgeFakeCustomer\Event\FakeCustomerEvent;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Thelia\Command\ContainerAwareCommand;
|
||||||
|
use Thelia\Model\ConfigQuery;
|
||||||
|
|
||||||
|
class FakeCustomerPurge extends ContainerAwareCommand
|
||||||
|
{
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
{
|
||||||
|
$output->writeln(
|
||||||
|
sprintf("<info>Deleting fake customers</info>")
|
||||||
|
);
|
||||||
|
|
||||||
|
$critere = ConfigQuery::read('purgefakecustomer_critere', '');
|
||||||
|
$event = new FakeCustomerEvent($critere, $input->getOption('verbose'));
|
||||||
|
|
||||||
|
$this->getDispatcher()->dispatch(FakeCustomerEvent::PURGE, $event);
|
||||||
|
|
||||||
|
foreach ($event->getStatus() as $status => $level) {
|
||||||
|
$output->writeln("<$level>$status</$level>");
|
||||||
|
}
|
||||||
|
|
||||||
|
$output->writeln(sprintf("<info>%d fake customers deleteddàçp</info>", $event->getDeletedCount()));
|
||||||
|
}
|
||||||
|
}
|
||||||
1
local/modules/PurgeFakeCustomer/Config/secret-key.txt
Normal file
1
local/modules/PurgeFakeCustomer/Config/secret-key.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
sterivein-2021
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PurgeFakeCustomer\Controller;
|
||||||
|
|
||||||
|
use PurgeFakeCustomer\Event\FakeCustomerEvent;
|
||||||
|
use Thelia\Controller\Front\BaseFrontController;
|
||||||
|
use Thelia\Core\HttpFoundation\Response;
|
||||||
|
use Thelia\Model\ConfigQuery;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Laurent LE CORRE <laurent@thecoredev.fr>
|
||||||
|
* Date: 12/01/2021
|
||||||
|
*/
|
||||||
|
class PurgeController extends BaseFrontController
|
||||||
|
{
|
||||||
|
public function purge($secretKey)
|
||||||
|
{
|
||||||
|
$responseText = '';
|
||||||
|
$storedSecretKey = trim(@file_get_contents(__DIR__ .'/../Config/secret-key.txt'));
|
||||||
|
|
||||||
|
if ($storedSecretKey != $secretKey) {
|
||||||
|
$responseText .= sprintf("ERROR: key verification failed.<br>");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
$critere = ConfigQuery::read('purgefakecustomer_critere', '');
|
||||||
|
$responseText .= sprintf("INFO: Deleting fake customers<br>");
|
||||||
|
$verbose = $this->getRequest()->query->get('verbose', false);
|
||||||
|
|
||||||
|
$event = new FakeCustomerEvent($critere, !empty($verbose));
|
||||||
|
$this->getDispatcher()->dispatch(FakeCustomerEvent::PURGE, $event);
|
||||||
|
|
||||||
|
foreach ($event->getStatus() as $status => $level) {
|
||||||
|
$responseText .= strtoupper($level) . ": $status<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
$responseText .= sprintf("INFO: %d fake customers deleted<br>", $event->getDeletedCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response($responseText);
|
||||||
|
}
|
||||||
|
}
|
||||||
83
local/modules/PurgeFakeCustomer/Event/FakeCustomerEvent.php
Normal file
83
local/modules/PurgeFakeCustomer/Event/FakeCustomerEvent.php
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace PurgeFakeCustomer\Event;
|
||||||
|
|
||||||
|
use Thelia\Core\Event\ActionEvent;
|
||||||
|
|
||||||
|
class FakeCustomerEvent extends ActionEvent
|
||||||
|
{
|
||||||
|
const PURGE = 'purgefakecustomer.purge';
|
||||||
|
|
||||||
|
/* Critère de purge des faux clients */
|
||||||
|
protected $critere;
|
||||||
|
|
||||||
|
/** @var string[] */
|
||||||
|
protected $status = [];
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
protected $verbose;
|
||||||
|
|
||||||
|
/** @var int */
|
||||||
|
protected $deletedCount = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FakeCustomerEvent constructor.
|
||||||
|
*/
|
||||||
|
public function __construct($critere, $verbose = false)
|
||||||
|
{
|
||||||
|
$this->critere = $critere;
|
||||||
|
$this->verbose = $verbose;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCritere()
|
||||||
|
{
|
||||||
|
return $this->critere;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public function getStatus()
|
||||||
|
{
|
||||||
|
return $this->status;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $status
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function appendStatus($status, $level = 'info')
|
||||||
|
{
|
||||||
|
$this->status[$status] = $level;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function isVerbose()
|
||||||
|
{
|
||||||
|
return $this->verbose;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getDeletedCount()
|
||||||
|
{
|
||||||
|
return $this->deletedCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $deletedCount
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setDeletedCount($deletedCount)
|
||||||
|
{
|
||||||
|
$this->deletedCount = $deletedCount;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Laurent LE CORRE <laurent@thecoredev.fr>
|
||||||
|
* Date: 12/01/2021
|
||||||
|
*/
|
||||||
|
namespace PurgeFakeCustomer\EventListener;
|
||||||
|
|
||||||
|
use Propel\Runtime\Exception\PropelException;
|
||||||
|
use Propel\Runtime\Propel;
|
||||||
|
use PurgeFakeCustomer\Event\FakeCustomerEvent;
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
use Thelia\Model\CustomerQuery;
|
||||||
|
use Thelia\Model\Map\FeatureTableMap;
|
||||||
|
use Thelia\Model\Map\TemplateTableMap;
|
||||||
|
use Thelia\Model\Template as ChildTemplate;
|
||||||
|
|
||||||
|
class EventManager implements EventSubscriberInterface
|
||||||
|
{
|
||||||
|
public function purge(FakeCustomerEvent $event)
|
||||||
|
{
|
||||||
|
$critereComplet = '';
|
||||||
|
$critere = $event->getCritere();
|
||||||
|
|
||||||
|
if ($critere !== '') $critereComplet = $critere . ' AND ';
|
||||||
|
$critereComplet = $critereComplet . '`customer_id` NOT IN (SELECT DISTINCT(`customer_id`) FROM `order`)';
|
||||||
|
|
||||||
|
$verbose = $event->isVerbose();
|
||||||
|
$deleted = 0;
|
||||||
|
|
||||||
|
$sql = 'SELECT `customer_id` FROM `address` WHERE ' . $critereComplet;
|
||||||
|
$con = Propel::getServiceContainer()->getReadConnection(FeatureTableMap::DATABASE_NAME);
|
||||||
|
try {
|
||||||
|
$stmt = $con->prepare($sql);
|
||||||
|
$stmt->execute();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
Propel::log($e->getMessage(), Propel::LOG_ERR);
|
||||||
|
throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), 0, $e);
|
||||||
|
}
|
||||||
|
$obj = null;
|
||||||
|
if ($rows = $stmt->fetchAll(\PDO::FETCH_NUM)) {
|
||||||
|
$obj = new ChildTemplate();
|
||||||
|
$obj->hydrate($row);
|
||||||
|
TemplateTableMap::addInstanceToPool($obj, (string) $key);
|
||||||
|
}
|
||||||
|
$stmt->closeCursor();
|
||||||
|
|
||||||
|
return $obj;
|
||||||
|
|
||||||
|
$event->setDeletedCount($deleted);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
public static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
FakeCustomerEvent::PURGE => ["purge", 128]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user