<?php


namespace ModulesGarden\HostingQuotaNotifications\App\Cron\WithoutThread;


use ModulesGarden\HostingQuotaNotifications\App\Helper\Logger\Log;
use ModulesGarden\HostingQuotaNotifications\App\Libs\Cron\Logs;
use ModulesGarden\HostingQuotaNotifications\Core\CommandLine\AbstractCronControllerWithoutThread;

/**
 * Class DeleteLogs
 */
class DeleteLogs extends AbstractCronControllerWithoutThread
{
    const OLDER_THAN = '--older-than';

    public function run()
    {
        try
        {
            Log::cronLog('start');

            $argv = $_SERVER['argv'];

            if ($argv[2] && $argv[2] !== self::OLDER_THAN)
            {
                throw new \Exception('Unavailable command: ' . $argv[2]);
            }

            $days = $argv[3] && is_numeric($argv[3]) && $argv[3] >= 0 ? $argv[3] : 1;

            $logs = new Logs();
            $logs->deleteOlderThan($days);

            Log::cronLog('finish');
        }
        catch (\Exception $exception)
        {
            \logModuleCall('hosting_quota_notification', 'cron error', [], var_export($exception, true));
        }
    }
}