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

ExampleGibsonRequest::run()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 26
Ratio 89.66 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 4
nop 0
dl 26
loc 29
rs 8.8571
1
<?php
2
/**
3
 * @package    Examples
4
 * @subpackage ExampleGibson
5
 *
6
 * @author     Vasily Zorin <[email protected]>
7
 */
8
namespace PHPDaemon\Examples;
9
10
use PHPDaemon\HTTPRequest\Generic;
11
12
/**
13
 * Class ExampleGibson
14
 * @package PHPDaemon\Applications
15
 * For testing gideros functionality
16
 */
17
class ExampleGibson extends \PHPDaemon\Core\AppInstance
18
{
19
    public $gibson;
20
21
    /**
22
     * Called when the worker is ready to go.
23
     * @return void
24
     */
25
    public function onReady()
26
    {
27
        $this->gibson = \PHPDaemon\Clients\Gibson\Pool::getInstance();
28
    }
29
30
    /**
31
     * Creates Request.
32
     * @param object Request.
33
     * @param object Upstream application instance.
34
     * @return ExampleGibsonRequest Request.
35
     */
36
    public function beginRequest($req, $upstream)
37
    {
38
        return new ExampleGibsonRequest($this, $upstream, $req);
39
    }
40
}
41
42
class ExampleGibsonRequest extends Generic
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
43
{
44
    public $job;
45
46
    /**
47
     * Constructor.
48
     * @return void
49
     */
50
    public function init()
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
51
    {
52
        $job = $this->job = new \PHPDaemon\Core\ComplexJob(function ($job) { // called when job is done
53
            $this->wakeup(); // wake up the request immediately
54
            $job->keep(); // prevent from cleaning
55
        });
56
57
        if (isset($_GET['fill'])) {
58
            for ($i = 0; $i < 100; ++$i) {
59
                $this->appInstance->gibson->set(3600, 'key' . $i, 'val' . $i);
0 ignored issues
show
Bug introduced by
The property gibson does not seem to exist in PHPDaemon\Core\AppInstance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60
            }
61
        }
62
63
        $job('testquery', function ($jobname, $job) { // registering job named 'testquery'
64
            $this->appInstance->gibson->mget('key99', function ($conn) use ($job, $jobname) {
0 ignored issues
show
Bug introduced by
The property gibson does not seem to exist in PHPDaemon\Core\AppInstance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65
                if ($conn->isFinal()) {
66
                    $job->setResult($jobname, $conn->result);
67
                }
68
            });
69
        });
70
71
        $job(); // let the fun begin
72
73
        $this->sleep(1, true); // setting timeout*/
74
    }
75
76
    /**
77
     * Called when request iterated.
78
     * @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...
79
     */
80 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...
81
    {
82
        try {
83
            $this->header('Content-Type: text/html');
84
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
85
        }
86
        ?>
87
        <!DOCTYPE html>
88
        <html xmlns="http://www.w3.org/1999/xhtml">
89
        <head>
90
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
91
            <title>Example with Gibson</title>
92
        </head>
93
        <body>
94
        <?php
95
        if ($r = $this->job->getResult('testquery')) {
96
            echo '<h1>It works! Be happy! ;-)</h1>Result of query: <pre>';
97
            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...
98
            echo '</pre>';
99
        } else {
100
            echo '<h1>Something went wrong! We have no result...</h1>';
101
        }
102
        echo '<br />Request (http) took: ' . round(microtime(true) - $this->attrs->server['REQUEST_TIME_FLOAT'], 6);
103
        ?>
104
        </body>
105
        </html>
106
        <?php
107
108
    }
109
}
110