Test Failed
Push — master ( 2a8a67...5ddd1f )
by Alexey
04:25
created

Daemon   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 18 2
A start() 0 11 3
B getNextTask() 0 14 6
A task() 0 7 1
A workDir() 0 8 2
A unserialize() 0 3 1
A serialize() 0 3 1
A serializer() 0 7 2
A __destruct() 0 5 2
1
<?php
2
3
class Daemon extends Module {
4
    private $tasksDirResource;
5
    private $serializer;
6
    private $workDir;
7
    public $needCheck = 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
            echo 'started';
14
            flock($lock, LOCK_UN);
15
            fclose($lock);
16
            $fp = fsockopen($_SERVER['SERVER_NAME'],
17
                80,
18
                $errno, $errstr, 30);
19
20
            $out = "GET /daemon/start HTTP/1.1\r\n";
21
            $out .= "Host: " . $_SERVER['SERVER_NAME'] . "\r\n";
22
            $out .= "Connection: Close\r\n\r\n";
23
            fwrite($fp, $out);
24
            fclose($fp);
25
        }
26
    }
27
28
    function start() {
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...
29
        $workDir = $this->workDir();
30
        $lock = fopen($workDir . '/daemon.lock', 'w+');
31
        if (flock($lock, LOCK_EX | LOCK_NB)) {
32
            while ($taskFile = $this->getNextTask()) {
33
                $task = $this->unserialize(file_get_contents($taskFile));
34
                $task();
35
                unlink($taskFile);
36
            }
37
        }
38
    }
39
40
    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...
41
        $workDir = $this->workDir();
42
        if (!is_resource($this->tasksDirResource)) {
43
            $this->tasksDirResource = opendir($workDir . '/tasks/');
44
        }
45
        if ($this->tasksDirResource) {
46
            while (false !== ($entry = readdir($this->tasksDirResource))) {
47
                if ($entry != "." && $entry != "..") {
48
                    return $workDir . '/tasks/' . $entry;
49
                }
50
            }
51
        }
52
        return false;
53
    }
54
55
    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...
56
        $workDir = $this->workDir();
57
        $taskFile = $workDir . '/tasks/' . microtime(true) . '.task';
58
        file_put_contents($taskFile, $this->serialize($callback));
59
        $this->needCheck = true;
60
        return $taskFile;
61
    }
62
63
    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...
64
        if ($this->workDir) {
65
            return $this->workDir;
66
        }
67
        $path = App::$primary->path . '/daemon';
68
        Tools::createDir(App::$primary->path . '/daemon/tasks');
69
        return $path;
70
    }
71
72
    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...
73
        return $this->serializer()->unserialize($item);
74
    }
75
76
    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...
77
        return $this->serializer()->serialize($item);
78
    }
79
80
    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...
81
        if ($this->serializer) {
82
            return $this->serializer;
83
        }
84
        \ComposerCmd::requirePackage('jeremeamia/superclosure');
85
        return $this->serializer = new \SuperClosure\Serializer();
86
    }
87
88
    function __destruct() {
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...
89
        if ($this->needCheck) {
90
            $this->check();
91
        }
92
    }
93
94
}