Completed
Push — master ( f6c83d...982358 )
by Vasily
06:50
created

ExampleDNSClientRequest::init()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 25
rs 8.8571
1
<?php
2
namespace PHPDaemon\Examples;
3
4
use PHPDaemon\HTTPRequest\Generic;
5
6
class ExampleDNSClientRequest extends Generic
7
{
8
9
    public $job;
10
11
    /**
12
     * Constructor.
13
     * @return void
14
     */
15
    public function init()
16
    {
17
        $req = $this;
18
19
        $job = $this->job = new \PHPDaemon\Core\ComplexJob(function () use ($req) { // called when job is done
20
            $req->wakeup(); // wake up the request immediately
21
22
        });
23
24
        $job('query', function ($name, $job) { // registering job named 'showvar'
25
            \PHPDaemon\Clients\DNS\Pool::getInstance()->get('phpdaemon.net', function ($response) use ($name, $job) {
26
                $job->setResult($name, $response);
27
            });
28
        });
29
30
        $job('resolve', function ($name, $job) { // registering job named 'showvar'
31
            \PHPDaemon\Clients\DNS\Pool::getInstance()->resolve('phpdaemon.net', function ($ip) use ($name, $job) {
32
                $job->setResult($name, ['phpdaemon.net resolved to' => $ip]);
33
            });
34
        });
35
36
        $job(); // let the fun begin
37
38
        $this->sleep(5, true); // setting timeout
39
    }
40
41
    /**
42
     * Called when request iterated.
43
     * @return integer Status.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45
    public function run()
46
    {
47
        try {
48
            $this->header('Content-Type: text/plain');
49
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
50
        }
51
        var_dump($this->job->getResult('query'));
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->job->getResult('query')); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
52
        var_dump($this->job->getResult('resolve'));
53
    }
54
}
55