TaskSchedulerCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 50
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 0
cts 33
cp 0
rs 8.6315
c 0
b 0
f 0
cc 6
eloc 26
nc 8
nop 2
crap 42
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AppBundle\Command;
10
11
use Doctrine\ORM\EntityManager;
12
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use AnimeDb\Bundle\AppBundle\Entity\Task;
16
use AnimeDb\Bundle\AppBundle\Repository\Task as TaskRepository;
17
use Symfony\Component\Process\PhpExecutableFinder;
18
19
class TaskSchedulerCommand extends ContainerAwareCommand
20
{
21
    protected function configure()
22
    {
23
        $this->setName('animedb:task-scheduler')
24
            ->setDescription('Task Scheduler');
25
    }
26
27
    /**
28
     * @param InputInterface $input
29
     * @param OutputInterface $output
30
     *
31
     * @return bool
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        // exit if disabled
36
        if (!$this->getContainer()->getParameter('task_scheduler.enabled')) {
37
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method Symfony\Component\Console\Command\Command::execute of type null|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
38
        }
39
40
        // path to php executable
41
        $finder = new PhpExecutableFinder();
42
        $console = $finder->find().' '.$this->getContainer()->getParameter('kernel.root_dir').'/console';
43
44
        /* @var $em EntityManager */
45
        $em = $this->getContainer()->get('doctrine')->getManager();
46
        /* @var $repository TaskRepository */
47
        $repository = $em->getRepository('AnimeDbAppBundle:Task');
48
49
        $output->writeln('Task Scheduler');
50
51
        while (true) {
52
            $task = $repository->getNextTask();
53
54
            // task is exists
55
            if ($task instanceof Task) {
56
                $output->writeln(sprintf('Run <info>%s</info>', $task->getCommand()));
57
58
                if (defined('PHP_WINDOWS_VERSION_BUILD')) {
59
                    pclose(popen('start /b '.$console.' '.$task->getCommand().' >nul 2>&1', 'r'));
60
                } else {
61
                    exec($console.' '.$task->getCommand().' >/dev/null 2>&1 &');
62
                }
63
64
                // update information on starting
65
                $task->executed();
66
                $em->persist($task);
67
                $em->flush($task);
68
            }
69
70
            // standby for the next task
71
            $time = $repository->getWaitingTime();
72
            if ($time) {
73
                $output->writeln(sprintf('Wait <comment>%s</comment> s.', $time));
74
                sleep($time);
75
            }
76
77
            unset($task);
78
            gc_collect_cycles();
79
        }
80
81
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method Symfony\Component\Console\Command\Command::execute of type null|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
82
    }
83
}
84