Test Failed
Push — master ( d0f0bf...80812e )
by Alexey
07:36
created

Daemon::start()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 25
nc 12
nop 1
dl 0
loc 34
rs 4.909
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
            $fp = fsockopen($_SERVER['SERVER_NAME'],
16
                80,
17
                $errno, $errstr, 30);
18
            $out = "GET /daemon/start HTTP/1.1\r\n";
19
            $out .= "Host: " . $_SERVER['SERVER_NAME'] . "\r\n";
20
            $out .= "Connection: Close\r\n\r\n";
21
            fwrite($fp, $out);
22
            fclose($fp);
23
        }
24
    }
25
26
    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...
27
        $workDir = $this->workDir();
28
        $lock = fopen($workDir . '/daemon.lock', 'w+');
29
        ignore_user_abort(true);
30
        if (flock($lock, LOCK_EX | LOCK_NB)) {
31
            set_time_limit(0);
32
            while (true) {
33
                $taskFile = $this->getNextTask();
34
                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...
35
                    for ($i = 0; $i < 60; $i++) {
36
                        sleep(1);
37
                        $taskFile = $this->getNextTask();
38
                        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...
39
                            break;
40
                        }
41
                    }
42
                    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...
43
                        break;
44
                    }
45
                }
46
                $task = $this->unserialize(file_get_contents($taskFile));
47
                unlink($taskFile);
48
                if ($task) {
49
                    var_dump(1);
0 ignored issues
show
Security Debugging Code introduced by
var_dump(1); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
50
                    $task();
51
                }
52
            }
53
        } else {
54
            if ($retry) {
55
                sleep(1);
56
                $this->start(false);
57
            }
58
        }
59
    }
60
61
    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...
62
        $workDir = $this->workDir();
63
        if (!is_resource($this->tasksDirResource)) {
64
            $this->tasksDirResource = opendir($workDir . '/tasks/');
65
        }
66
        if ($this->tasksDirResource) {
67
            while (false !== ($entry = readdir($this->tasksDirResource))) {
68
                if ($entry != "." && $entry != "..") {
69
                    return $workDir . '/tasks/' . $entry;
70
                }
71
            }
72
        }
73
        return false;
74
    }
75
76
    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...
77
        $keyLog = \App::$cur->log->start('add task');
78
        $workDir = $this->workDir();
79
        $taskFile = $workDir . '/tasks/' . microtime(true) . '.task';
80
        $keyLog2 = \App::$cur->log->start('serialize task');
81
        $serialize = $this->serialize($callback);
82
        \App::$cur->log->end($keyLog2);
83
        $keyLog3 = \App::$cur->log->start('put task to file');
84
        file_put_contents($taskFile, $serialize);
85
        \App::$cur->log->end($keyLog3);
86
        if (!$this->checked) {
87
            $this->checked = true;
88
            $this->check();
89
        }
90
        \App::$cur->log->end($keyLog);
91
        return $taskFile;
92
    }
93
94
    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...
95
        if ($this->workDir) {
96
            return $this->workDir;
97
        }
98
        $path = App::$primary->path . '/daemon';
99
        Tools::createDir(App::$primary->path . '/daemon/tasks');
100
        return $path;
101
    }
102
103
    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...
104
        try {
105
            return $this->serializer()->unserialize($item);
106
        } 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...
107
            return false;
108
        }
109
    }
110
111
    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...
112
        return $this->serializer()->serialize($item);
113
    }
114
115
    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...
116
        if ($this->serializer) {
117
            return $this->serializer;
118
        }
119
        \ComposerCmd::requirePackage('jeremeamia/superclosure');
120
        return $this->serializer = new \SuperClosure\Serializer(new SuperClosure\Analyzer\TokenAnalyzer());
121
    }
122
123
}