Completed
Push — master ( ea15e4...6b748c )
by Michał
04:07
created

Worker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 58
loc 58
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 14 14 1
A execute() 20 20 3
A getNamespace() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Phppm\Console;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Style\SymfonyStyle;
10
11 View Code Duplication
class Worker extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    protected function configure()
14
    {
15
        $this->setName('worker')
16
            ->setDescription('Execute worker with given process script.')
17
            ->setHelp('');
18
19
        $this->addArgument(
20
            'script',
21
            InputArgument::REQUIRED,
22
            'Script that will be demonized'
23
        );
24
25
        $this->addOption('interval', 'i', InputArgument::OPTIONAL, 'Worker interval time.');
26
    }
27
28
    /**
29
     * @param InputInterface $input
30
     * @param OutputInterface $output
31
     * @throws \InvalidArgumentException
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $style = new SymfonyStyle($input, $output);
36
        $script = $input->getArgument('script');
37
38
        $style->title('Script worker.');
39
40
        $time = $input->getOption('interval') ?: Run::DEFAULT_TIME;
41
42
        $style->comment('Execute with interval: ' . $time);
43
44
        if (file_exists($script)) {
45
            require_once $script;
46
            $namespace = $this->getNamespace($script);
47
48
            $worker = new \Phppm\ProcessWorker('\\' . $namespace, $output, $time);
49
            $worker->runProcess();
50
51
        }
52
    }
53
54
    /**
55
     * @param string $file
56
     * @return string
57
     */
58
    protected function getNamespace(string $file) : string
59
    {
60
        $source = file_get_contents($file);
61
62
        if (preg_match('#^namespace\s+(.+?);.*class\s+(\w+).+;$#sm', $source, $matches)) {
63
            return $matches[1].'\\'.$matches[2];
64
        }
65
66
        return '';
67
    }
68
}
69