ExampleWithMemcacheRequest::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 23
Ratio 92 %

Importance

Changes 0
Metric Value
cc 2
dl 23
loc 25
rs 9.52
c 0
b 0
f 0
nc 2
nop 0
1
<?php
2
namespace PHPDaemon\Examples;
3
4
use PHPDaemon\HTTPRequest\Generic;
5
6
/**
7
 * @package    Examples
8
 * @subpackage Memcache
9
 *
10
 * @author     Vasily Zorin <[email protected]>
11
 */
12
class ExampleWithMemcache extends \PHPDaemon\Core\AppInstance
13
{
14
    /**
15
     * Creates Request.
16
     * @param object Request.
17
     * @param object Upstream application instance.
18
     * @return ExampleWithMemcacheRequest Request.
19
     */
20
    public function beginRequest($req, $upstream)
21
    {
22
        return new ExampleWithMemcacheRequest($this, $upstream, $req);
23
    }
24
}
25
26
class ExampleWithMemcacheRequest extends Generic
27
{
28
29
public $job;
30
31
/**
32
 * Constructor.
33
 * @return void
34
 */
35
public function init()
36
{
37
    try {
38
        $this->header('Content-Type: text/html');
39
    } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
40
    }
41
    $req = $this;
42
43
    $job = $this->job = new \PHPDaemon\Core\ComplexJob(function () use ($req) { // called when job is done
44
45
        $req->wakeup(); // wake up the request immediately
46
47
    });
48
    $memcache = \PHPDaemon\Clients\Memcache\Pool::getInstance();
49
50
    $job('testquery', function ($name, $job) use ($memcache) { // registering job named 'testquery'
51
52
        $memcache->stats(function ($memcache) use ($name, $job) { // calling 'stats'
53
54
            $job->setResult($name, $memcache->result); // setting job result
55
56
        });
57
58
    });
59
60
    $job(); // let the fun begin
61
62
    $this->sleep(5, true); // setting timeout
63
}
64
65
/**
66
 * Called when request iterated.
67
 * @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...
68
 */
69 View Code Duplication
public function run()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
{
71
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
72
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
73
<html xmlns="http://www.w3.org/1999/xhtml">
74
<head>
75
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
76
    <title>Example with Memcache</title>
77
</head>
78
<body>
79
<?php
80
if ($r = $this->job->getResult('testquery')) {
81
    echo '<h1>It works! Be happy! ;-)</h1>Result of query: <pre>';
82
    var_dump($r);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($r); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
83
    echo '</pre>';
84
} else {
85
    echo '<h1>Something went wrong! We have no result.</h1>';
86
}
87
echo '<br />Request (http) took: ' . round(microtime(true) - $this->attrs->server['REQUEST_TIME_FLOAT'], 6);
88
?>
89
</body>
90
</html>
91
<?php
92
93
}
94
}
95