GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CronTaskRunCommand::executeCommands()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 8.8571
cc 5
eloc 9
nc 5
nop 3
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace TMSolution\CronBundle\Command;
10
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Input\StringInput;
15
16
class CronTaskRunCommand extends ContainerAwareCommand
17
{
18
19
    const EXEC_CLI = 1;
20
    const EXEC_SYMFONYCLI = 2;
21
    const EXEC_REST = 3;
22
    const EXEC_SYMFONYSERVICE = 4;
23
24
    private $output;
25
    private $em;
26
27
    protected function configure()
28
    {
29
        $this
30
                ->setName('crontasks:run')
31
                ->setDescription('Runs Cron Tasks if needed')
32
        ;
33
    }
34
35
    private function getEntityManager()
36
    {
37
        return $this->getContainer()->get('doctrine.orm.entity_manager');
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        //$output->writeln('<comment>Running Cron Tasks...</comment>');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43
44
        $this->output = $output;
45
        $this->em = $this->getEntityManager();
46
        // $i = 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
        while (true) {
48
49
50
51
52
53
            try {
54
                $crontasks = $this->em->getRepository('TMSolutionCronBundle:CronTask')->getNewTasks(20);
55
56
                foreach($crontasks as $crontask) {
57
                    if ($crontask) {
58
59
60
                        $this->runCronTask($crontask, $output);
61
                        $this->em->flush();
62
                    }
63
                }
64
                //$i++;
65
            } catch (\Exception $e) {
66
                echo "Błąd crona.";
67
            }
68
            sleep(10);
69
        }
70
        $output->writeln('<comment>Done!</comment>');
71
    }
72
73
    private function runCronTask($crontask, OutputInterface $output)
74
    {
75
        $output->writeln(sprintf('Running Cron Task <info>%s</info>', $crontask->getName()));
76
        $crontask->setRunDate(new \DateTime());
77
        $commands = $crontask->getCommands();
78
        $crontask->setUsed(1);
79
        $this->em->persist($crontask);
80
        try {
81
            $this->executeCommands($commands, $crontask->getExecType(), $output);
82
            $output->writeln('<info>SUCCESS</info>');
83
            $crontask->setSuccess(1);
84
        } catch (\Exception $e) {
85
            $output->writeln(sprintf('<error>ERROR</error><comment>%s</comment>', $e->getMessage()));
86
            $crontask->setSuccess(0);
87
        }
88
        $this->em->persist($crontask);
89
    }
90
91
    private function executeCommands($commands, $execType, OutputInterface $output)
92
    {
93
        foreach ($commands as $command) {
94
            $output->writeln(sprintf('Executing command <comment>%s</comment>...', $command));
95
            if ($execType == self::EXEC_SYMFONYCLI) {
96
                return $this->runSymfonyCliCommand($command);
97
            } elseif ($execType == self::EXEC_CLI) {
98
                return $this->runCliCommand($command);
99
            } elseif ($execType == self::EXEC_REST) {
100
                return $this->runRestCommand($command);
101
            }
102
        }
103
    }
104
105
    private function runSymfonyCliCommand($string)
106
    {
107
        $namespace = explode(' ', $string)[0];
108
        $command = $this->getApplication()->find($namespace);
109
        $input = new StringInput($string);
110
        $returnCode = $command->run($input, $this->output);
111
112
        return $returnCode != 0;
113
    }
114
115
    private function runCliCommand($command)
116
    {
117
        $command = 'nohup ' . $command . ' > /dev/null 2>&1 & echo $!';
118
        $op = [];
119
        exec($command, $op);
120
121
        if (!isset($op[0])) {
122
            throw new \Exception;
123
        } else {
124
            return true;
125
        }
126
    }
127
128
    private function runRestCommand($command)
129
    {
130
        //next example will recieve all messages for specific conversation
131
        $service_url = 'http://localhost/owca/web/app_dev.php/dummy';
132
        $curl = curl_init($service_url);
133
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
134
        $curl_response = curl_exec($curl);
135
        if ($curl_response === false) {
136
            $info = curl_getinfo($curl);
137
            curl_close($curl);
138
            throw new \Exception('error occured during curl exec. Additinal info: ' . var_export($info));
139
        }
140
        curl_close($curl);
141
        $decoded = json_decode($curl_response);
142
143
        var_dump($decoded);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($decoded); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
144
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method runRestCommand() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
145
146
        if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
0 ignored issues
show
Unused Code introduced by
if (isset($decoded->resp...ponse->errormessage); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Bug introduced by
The variable $decoded seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
147
            throw new \Exception('error occured: ' . $decoded->response->errormessage);
148
        }
149
150
151
        return true;
152
        echo 'response ok!';
0 ignored issues
show
Unused Code introduced by
echo 'response ok!'; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
153
        var_export($decoded->response);
154
    }
155
156
}
157