ExampleWithMongoRequest::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
nc 1
nop 0
1
<?php
2
namespace PHPDaemon\Examples;
3
4
/**
5
 * @package    Examples
6
 * @subpackage Mongo
7
 *
8
 * @author     Vasily Zorin <[email protected]>
9
 */
10
class ExampleWithMongo extends \PHPDaemon\Core\AppInstance
11
{
12
    public $mongo;
13
14
    /**
15
     * Constructor.
16
     * @return void
17
     */
18
    public function init()
19
    {
20
        $this->mongo = \PHPDaemon\Clients\Mongo\Pool::getInstance(['maxconnperserv' => 100]);
0 ignored issues
show
Documentation introduced by
array('maxconnperserv' => 100) is of type array<string,integer,{"m...onnperserv":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
21
    }
22
23
    /**
24
     * Creates Request.
25
     * @param object Request.
26
     * @param object Upstream application instance.
27
     * @return ExampleWithMongoRequest Request.
28
     */
29
    public function beginRequest($req, $upstream)
30
    {
31
        return new ExampleWithMongoRequest($this, $upstream, $req);
32
    }
33
}
34
35
class ExampleWithMongoRequest extends \PHPDaemon\HTTPRequest\Generic
36
{
37
38
    public $job;
39
40
    /**
41
     * Constructor.
42
     * @return void
43
     */
44
    public function init()
45
    {
46
        $job = $this->job = new \PHPDaemon\Core\ComplexJob(function ($job) { // called when job is done
47
            $job->keep(); // prevent cleaning up results
48
            $this->wakeup(); // wake up the request immediately
49
50
        });
51
52
        $collection = $this->appInstance->mongo->{'testdb.testcollection'};
0 ignored issues
show
Bug introduced by
The property mongo 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...
53
        $collection->insert(['a' => microtime(true)]); // just pushing something
54
55
        $job('testquery', function ($name, $job) use ($collection) { // registering job named 'testquery'
56
57
            $collection->findOne(function ($result) use ($name, $job) { // calling Mongo findOne
58
59
                $job->setResult($name, $result);
60
61
            }, ['sort' => ['$natural' => -1]]);
62
        });
63
64
        $job(); // let the fun begin
65
66
        $this->sleep(1, true); // setting timeout
67
    }
68
69
    /**
70
     * Called when request iterated.
71
     * @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...
72
     */
73 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...
74
    {
75
        try {
76
            $this->header('Content-Type: text/html');
77
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
78
        }
79
        ?>
80
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
81
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
82
        <html xmlns="http://www.w3.org/1999/xhtml">
83
        <head>
84
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
85
            <title>Example with Mongo</title>
86
        </head>
87
        <body>
88
        <?php
89
        if ($r = $this->job->getResult('testquery')) {
90
            echo '<h1>It works! Be happy! ;-)</h1>Result of query: <pre>';
91
            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...
92
            echo '</pre>';
93
        } else {
94
            echo '<h1>Something went wrong! We have no result.</h1>';
95
        }
96
        echo '<br />Request (http) took: ' . round(microtime(true) - $this->attrs->server['REQUEST_TIME_FLOAT'], 6);
97
        ?>
98
        </body>
99
        </html>
100
        <?php
101
102
    }
103
}
104