Skip to content

Commit

Permalink
feat: symfony 7 (#131)
Browse files Browse the repository at this point in the history
* Fix the type related issues, replace the doc types with PHP type wherever safe

* feat: run on symfony 7

* fix: $scriptName must not be acces   sed before initialization

* feat: replace annotations with attributes

* fix: add strict types

* chore: use only needed libraries

---------

Co-authored-by: Musa Haidari <[email protected]>
  • Loading branch information
k0d3r1s and musahcoding authored Jan 3, 2024
1 parent a59afb8 commit 2c0341f
Show file tree
Hide file tree
Showing 28 changed files with 363 additions and 528 deletions.
55 changes: 18 additions & 37 deletions Command/CronCreateCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* This file is part of the SymfonyCronBundle package.
*
Expand All @@ -11,12 +11,13 @@

use Cron\CronBundle\Cron\CronCommand;
use Cron\CronBundle\Entity\CronJob;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Input\InputArgument;
use InvalidArgumentException;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

/**
* @author Dries De Peuter <[email protected]>
Expand All @@ -26,7 +27,7 @@ class CronCreateCommand extends CronCommand
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this->setName('cron:create')
->setDescription('Create a cron job')
Expand All @@ -37,14 +38,7 @@ protected function configure()
->addOption('enabled', null, InputOption::VALUE_REQUIRED, 'Is the job enabled');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$job = new CronJob();

Expand Down Expand Up @@ -108,18 +102,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
/**
* Validate the job name.
*
* @param string $name
* @return string
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
protected function validateJobName($name)
protected function validateJobName(?string $name): string
{
if (!$name || strlen($name) == 0) {
throw new \InvalidArgumentException('Please set a name.');
throw new InvalidArgumentException('Please set a name.');
}

if ($this->queryJob($name)) {
throw new \InvalidArgumentException('Name already in use.');
throw new InvalidArgumentException('Name already in use.');
}

return $name;
Expand All @@ -128,47 +120,36 @@ protected function validateJobName($name)
/**
* Validate the command.
*
* @param string $command
* @return string
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
protected function validateCommand($command)
protected function validateCommand(string $command): string
{
$parts = explode(' ', $command);
$this->getApplication()->get((string) $parts[0]);
$this->getApplication()->get($parts[0]);

return $command;
}

/**
* Validate the schedule.
*
* @param string $schedule
* @return string
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
protected function validateSchedule($schedule)
protected function validateSchedule(string $schedule): string
{
$this->getContainer()->get('cron.validator')
->validate($schedule);

return $schedule;
}

/**
* @param string $jobName
* @return CronJob
*/
protected function queryJob($jobName)
protected function queryJob(string $jobName): ?CronJob
{
return $this->getContainer()->get('cron.manager')
->getJobByName($jobName);
}

/**
* @return QuestionHelper
*/
private function getQuestionHelper()
private function getQuestionHelper(): HelperInterface
{
return $this->getHelperSet()->get('question');
}
Expand Down
34 changes: 11 additions & 23 deletions Command/CronDeleteCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* This file is part of the SymfonyCronBundle package.
*
Expand All @@ -11,10 +11,12 @@

use Cron\CronBundle\Cron\CronCommand;
use Cron\CronBundle\Entity\CronJob;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use InvalidArgumentException;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

/**
* @author Dries De Peuter <[email protected]>
Expand All @@ -24,30 +26,23 @@ class CronDeleteCommand extends CronCommand
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this->setName('cron:delete')
->setDescription('Delete a cron job')
->addArgument('job', InputArgument::REQUIRED, 'The job to delete');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$job = $this->queryJob($input->getArgument('job'));

if (!$job) {
throw new \InvalidArgumentException('Unknown job.');
throw new InvalidArgumentException('Unknown job.');
}

if ($job->getEnabled()) {
throw new \InvalidArgumentException('The job should be disabled first.');
throw new InvalidArgumentException('The job should be disabled first.');
}

$output->writeln(sprintf('<info>You are about to delete "%s".</info>', $job->getName()));
Expand All @@ -61,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
);

if (!$this->getQuestionHelper()->ask($input, $output, $question)) {
return;
return 0;
}

$this->getContainer()->get('cron.manager')
Expand All @@ -72,20 +67,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

/**
* @param string $jobName
* @return CronJob
*/
protected function queryJob($jobName)
protected function queryJob(string $jobName): ?CronJob
{
return $this->getContainer()->get('cron.manager')
->getJobByName($jobName);
}

/**
* @return QuestionHelper
*/
private function getQuestionHelper()
private function getQuestionHelper(): HelperInterface
{
return $this->getHelperSet()->get('question');
}
Expand Down
21 changes: 6 additions & 15 deletions Command/CronDisableCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* This file is part of the SymfonyCronBundle package.
*
Expand All @@ -11,6 +11,7 @@

use Cron\CronBundle\Cron\CronCommand;
use Cron\CronBundle\Entity\CronJob;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -23,26 +24,20 @@ class CronDisableCommand extends CronCommand
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this->setName('cron:disable')
->setDescription('Disable a cron job')
->addArgument('job', InputArgument::REQUIRED, 'The job to disable');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$job = $this->queryJob($input->getArgument('job'));

if (!$job) {
throw new \InvalidArgumentException('Unknown job.');
throw new InvalidArgumentException('Unknown job.');
}

$job->setEnabled(false);
Expand All @@ -55,11 +50,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

/**
* @param string $jobName
* @return CronJob
*/
protected function queryJob($jobName)
protected function queryJob(string $jobName): ?CronJob
{
return $this->getContainer()->get('cron.manager')
->getJobByName($jobName);
Expand Down
22 changes: 6 additions & 16 deletions Command/CronEnableCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* This file is part of the SymfonyCronBundle package.
*
Expand All @@ -11,6 +11,7 @@

use Cron\CronBundle\Cron\CronCommand;
use Cron\CronBundle\Entity\CronJob;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -23,26 +24,19 @@ class CronEnableCommand extends CronCommand
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this->setName('cron:enable')
->setDescription('Enable a cron job')
->addArgument('job', InputArgument::REQUIRED, 'The job to enable');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$job = $this->queryJob($input->getArgument('job'));

if (!$job) {
throw new \InvalidArgumentException('Unknown job.');
throw new InvalidArgumentException('Unknown job.');
}

$job->setEnabled(true);
Expand All @@ -55,11 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 0;
}

/**
* @param string $jobName
* @return CronJob
*/
protected function queryJob($jobName)
protected function queryJob(string $jobName): ?CronJob
{
return $this->getContainer()->get('cron.manager')
->getJobByName($jobName);
Expand Down
17 changes: 4 additions & 13 deletions Command/CronListCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);
/**
* This file is part of the SymfonyCronBundle package.
*
Expand All @@ -11,10 +11,8 @@

use Cron\CronBundle\Cron\CronCommand;
use Cron\CronBundle\Entity\CronJob;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* @author Dries De Peuter <[email protected]>
Expand All @@ -24,20 +22,13 @@ class CronListCommand extends CronCommand
/**
* {@inheritdoc}
*/
protected function configure()
protected function configure(): void
{
$this->setName('cron:list')
->setDescription('List all available crons');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$jobs = $this->queryJobs();

Expand All @@ -52,7 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
/**
* @return CronJob[]
*/
protected function queryJobs()
protected function queryJobs(): array
{
return $this->getContainer()->get('cron.manager')->listJobs();
}
Expand Down
Loading

0 comments on commit 2c0341f

Please sign in to comment.