-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from experius/feature/added_comando_and_cronjob
Feature/added comando and cronjob
- Loading branch information
Showing
9 changed files
with
264 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Experius\PageNotFound\Console\Command; | ||
|
||
use Experius\PageNotFound\Helper\UrlCleanUp; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Magento\Framework\Console\Cli; | ||
|
||
class Clean extends Command | ||
{ | ||
|
||
const DAYS = "days"; | ||
|
||
/** | ||
* @param UrlCleanUp $cleanUp | ||
*/ | ||
public function __construct( | ||
protected UrlCleanUp $cleanUp, | ||
) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$options = [ | ||
new InputOption( | ||
self::DAYS, | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'Days you want to keep the reports' | ||
) | ||
]; | ||
|
||
$this->setName("experius_pagenotfound:clean") | ||
->setDescription("Cleanup old reports.") | ||
->setDefinition($options); | ||
|
||
parent::configure(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
if ($days = $input->getOption(self::DAYS)) { | ||
if(!is_numeric($days)) { | ||
$output->writeln($days . " is not a number"); | ||
return Cli::RETURN_FAILURE; | ||
} | ||
$output->writeln("Deleting everything older than " . $days . " days."); | ||
$deletionCount = $this->cleanUp->execute($days); | ||
} else { | ||
$output->writeln("No days given, using days from admin if available."); | ||
$deletionCount = $this->cleanUp->execute(); | ||
} | ||
$output->writeln('Removed ' . $deletionCount . ' from 404 reports.'); | ||
return Cli::RETURN_SUCCESS; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Copyright © Experius B.V. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Experius\PageNotFound\Cron; | ||
|
||
use Experius\PageNotFound\Helper\UrlCleanUp; | ||
use Experius\PageNotFound\Helper\Settings; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Clean | ||
{ | ||
/** | ||
* @param LoggerInterface $logger | ||
* @param UrlCleanUp $cleanHelper | ||
* @param Settings $settings | ||
*/ | ||
public function __construct( | ||
protected LoggerInterface $logger, | ||
protected UrlCleanUp $cleanHelper, | ||
protected Settings $settings | ||
) {} | ||
|
||
/** | ||
* Execute the cron | ||
* @return void | ||
*/ | ||
public function execute(): void | ||
{ | ||
if(!$this->settings->getIsCronEnabled()){ | ||
$this->logger->info(__("Cron is disabled for '404 reports'")); | ||
return; | ||
} | ||
$this->cleanHelper->execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
/** | ||
* Copyright © Experius B.V. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Experius\PageNotFound\Helper; | ||
|
||
use Magento\Framework\App\Helper\Context; | ||
use Magento\Framework\App\Helper\AbstractHelper; | ||
|
||
class Settings extends AbstractHelper | ||
{ | ||
|
||
const IS_CRON_ENABLED = 'pagenotfound/cron_config/is_cron_enabled'; | ||
const CONFIG_DAYS_TO_CLEAN = 'pagenotfound/cron_config/days_to_clean'; | ||
const DELETE_NOT_EMPTY_REDIRECT = 'pagenotfound/cron_config/delete_not_empty_redirect'; | ||
|
||
/** | ||
* @param Context $context | ||
*/ | ||
public function __construct( | ||
Context $context | ||
) { | ||
parent::__construct($context); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getIsCronEnabled() | ||
{ | ||
return $this->scopeConfig->getValue(self::IS_CRON_ENABLED); | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getConfigDaysToClean() | ||
{ | ||
return $this->scopeConfig->getValue(self::CONFIG_DAYS_TO_CLEAN); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDeleteNotEmpyRedirect() | ||
{ | ||
return $this->scopeConfig->getValue(self::DELETE_NOT_EMPTY_REDIRECT); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Copyright © Experius B.V. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Experius\PageNotFound\Helper; | ||
|
||
use Experius\PageNotFound\Helper\Settings; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class UrlCleanUp | ||
{ | ||
/** | ||
* @var \Magento\Framework\DB\Adapter\AdapterInterface | ||
*/ | ||
protected $connection; | ||
|
||
const TABLE = 'experius_page_not_found'; | ||
|
||
/** | ||
* @param LoggerInterface $logger | ||
* @param ResourceConnection $resourceConnection | ||
* @param Settings $settings | ||
*/ | ||
public function __construct( | ||
protected LoggerInterface $logger, | ||
protected ResourceConnection $resourceConnection, | ||
private Settings $settings | ||
) | ||
{ | ||
$this->connection = $this->resourceConnection->getConnection(); | ||
} | ||
|
||
/** | ||
* Get days to clean | ||
* @return int | ||
*/ | ||
public function getDaysToClean($days = null): int | ||
{ | ||
if ($days) { | ||
return (int)$days; | ||
} | ||
|
||
return (int)$this->settings->getConfigDaysToClean(); | ||
} | ||
|
||
/** | ||
* execute cleanup | ||
* @return int | ||
*/ | ||
public function execute($days = null): int | ||
{ | ||
$where = ("last_visited < '" . date('c', time() - ($this->getDaysToClean($days) * (3600 * 24))) . "'"); | ||
|
||
if(!$this->settings->getDeleteNotEmpyRedirect()) | ||
$where .= (' AND to_url IS NULL'); | ||
|
||
$deletionCount = $this->connection->delete(self::TABLE, $where); | ||
|
||
$this->logger->info(__('Experius 404 url Cleanup: Removed %1 records.', $deletionCount)); | ||
|
||
return $deletionCount; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" ?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> | ||
<group id="default"> | ||
<job name="experius_pagenotfound_clean" instance="Experius\PageNotFound\Cron\Clean" method="execute"> | ||
<schedule>0 3 * * *</schedule> | ||
</job> | ||
</group> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters