Test Failed
Push — master ( 45bc78...882099 )
by Alexey
05:03
created

Daemon::check()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 31
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 6
nop 0
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
class Daemon extends Module {
4
    private $tasksDirResource;
5
    private $serializer;
6
    private $workDir;
7
    public $checked = false;
8
9
    function check() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
10
        $workDir = $this->workDir();
11
        $lock = fopen($workDir . '/daemon.lock', 'w+');
12
        if (flock($lock, LOCK_EX | LOCK_NB)) {
13
            flock($lock, LOCK_UN);
14
            fclose($lock);
15
            if (function_exists('pcntl_fork') && $pid = pcntl_fork() !== -1) {
16
                if ($pid) {
17
                    return $pid;
18
                } else {
19
                    $this->start(true);
20
                }
21
            } elseif (function_exists('fsockopen')) {
22
                $fp = fsockopen($_SERVER['SERVER_NAME'],
23
                    80,
24
                    $errno, $errstr, 30);
25
                $out = "GET /daemon/start HTTP/1.1\r\n";
26
                $out .= "Host: " . $_SERVER['SERVER_NAME'] . "\r\n";
27
                $out .= "Connection: Close\r\n\r\n";
28
                fwrite($fp, $out);
29
                fclose($fp);
30
            } elseif (function_exists('curl_init')) {
31
                $ch = curl_init('http://' . $_SERVER['SERVER_NAME'] . '/daemon/start');
32
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
33
                curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);
34
                curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
35
                $content = curl_exec($ch);
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
                curl_close($ch);
37
            }
38
        }
39
    }
40
41
    function start($retry = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
        $workDir = $this->workDir();
43
        $lock = fopen($workDir . '/daemon.lock', 'w+');
44
        if (flock($lock, LOCK_EX | LOCK_NB)) {
45
            set_time_limit(0);
46
            while (true) {
47
                $taskFile = $this->getNextTask();
48
                if (!$taskFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $taskFile of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
49
                    for ($i = 0; $i < 358; $i++) {
50
                        sleep(1);
51
                        $taskFile = $this->getNextTask();
52
                        if ($taskFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $taskFile of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53
                            break;
54
                        }
55
                    }
56
                    if (!$taskFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $taskFile of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57
                        break;
58
                    }
59
                }
60
                $task = $this->unserialize(file_get_contents($taskFile));
61
                unlink($taskFile);
62
                if ($task) {
63
                    $task();
64
                }
65
            }
66
        } else {
67
            if ($retry) {
68
                sleep(1);
69
                $this->start(false);
70
            }
71
        }
72
    }
73
74
    function getNextTask() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
        $workDir = $this->workDir();
76
        if (!is_resource($this->tasksDirResource)) {
77
            $this->tasksDirResource = opendir($workDir . '/tasks/');
78
        }
79
        if ($this->tasksDirResource) {
80
            while (false !== ($entry = readdir($this->tasksDirResource))) {
81
                if ($entry != "." && $entry != "..") {
82
                    return $workDir . '/tasks/' . $entry;
83
                }
84
            }
85
        }
86
        return false;
87
    }
88
89
    function task($callback) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
90
        $keyLog = \App::$cur->log->start('add task');
91
        $workDir = $this->workDir();
92
        $taskFile = $workDir . '/tasks/' . microtime(true) . '.task';
93
        $keyLog2 = \App::$cur->log->start('serialize task');
94
        $serialize = $this->serialize($callback);
95
        \App::$cur->log->end($keyLog2);
96
        $keyLog3 = \App::$cur->log->start('put task to file');
97
        file_put_contents($taskFile, $serialize);
98
        \App::$cur->log->end($keyLog3);
99
        if (!$this->checked) {
100
            $this->checked = true;
101
            $this->check();
102
        }
103
        \App::$cur->log->end($keyLog);
104
        return $taskFile;
105
    }
106
107
    function workDir() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
108
        if ($this->workDir) {
109
            return $this->workDir;
110
        }
111
        $path = App::$primary->path . '/daemon';
112
        Tools::createDir(App::$primary->path . '/daemon/tasks');
113
        return $path;
114
    }
115
116
    function unserialize($item) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
        try {
118
            return $this->serializer()->unserialize($item);
119
        } catch (\SuperClosure\Exception\ClosureUnserializationException $e) {
0 ignored issues
show
Bug introduced by
The class SuperClosure\Exception\C...nserializationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
120
            return false;
121
        }
122
    }
123
124
    function serialize($item) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
125
        return $this->serializer()->serialize($item);
126
    }
127
128
    function serializer() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
129
        if ($this->serializer) {
130
            return $this->serializer;
131
        }
132
        \ComposerCmd::requirePackage('jeremeamia/superclosure');
133
        return $this->serializer = new \SuperClosure\Serializer(new SuperClosure\Analyzer\TokenAnalyzer());
134
    }
135
136
}