Skip to content

Commit

Permalink
Merge pull request #36 from experius/feature/added_comando_and_cronjob
Browse files Browse the repository at this point in the history
Feature/added comando and cronjob
  • Loading branch information
DRdevil27 authored Mar 20, 2024
2 parents 3e65c1e + 9a4fbe3 commit 0aa7e05
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 4 deletions.
70 changes: 70 additions & 0 deletions Console/Command/Clean.php
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;
}

}

39 changes: 39 additions & 0 deletions Cron/Clean.php
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();
}
}
53 changes: 53 additions & 0 deletions Helper/Settings.php
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);
}

}
68 changes: 68 additions & 0 deletions Helper/UrlCleanUp.php
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;
}

}
18 changes: 17 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@
<comment>comma separated. For example language,p,id</comment>
</field>
</group>
<group id="cron_config" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
<label>config</label>
<field id="is_cron_enabled" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="select">
<label>Enable cronjob</label>
<comment>cronjob runs once per day</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="days_to_clean" type="text" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Days you want to save the 404 url's</label>
</field>
<field id="delete_not_empty_redirect" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="select">
<label>delete not empty redirect</label>
<comment>delete rows with filled 'redirect to'</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>
</config>
7 changes: 6 additions & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<included_params_redirect/>
<included_params_from_url/>
</general>
<cron_config>
<is_cron_enabled>0</is_cron_enabled>
<days_to_clean>90</days_to_clean>
<delete_not_empty_redirect>0</delete_not_empty_redirect>
</cron_config>
</pagenotfound>
</default>
</config>
</config>
8 changes: 8 additions & 0 deletions etc/crontab.xml
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>
3 changes: 2 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<arguments>
<argument name="commands" xsi:type="array">
<item name="experius_pagenotfound_import" xsi:type="object">Experius\PageNotFound\Console\Command\Import</item>
</argument>
<item name="clean" xsi:type="object">Experius\PageNotFound\Console\Command\Clean</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
<filter>dateRange</filter>
<dataType>date</dataType>
<dateFormat>yyyy-MM-dd</dateFormat>
<label translate="true">last visited</label>
<label translate="true">Last Visited</label>
</settings>
</column>
<actionsColumn class="Experius\PageNotFound\Ui\Component\Listing\Column\PagenotfoundActions" name="actions">
Expand Down

0 comments on commit 0aa7e05

Please sign in to comment.