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

GameMonitor::updateServer()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 69
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 39
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 69
rs 8.56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace PHPDaemon\Examples;
3
4
use PHPDaemon\HTTPRequest\Generic;
5
6
/**
7
 * @package GameMonitor
8
 *
9
 * @author  Vasily Zorin <[email protected]>
10
 */
11
// db.servers.ensureIndex({address:1}, {unique:true});
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
12
class GameMonitor extends \PHPDaemon\Core\AppInstance
13
{
14
    public $client;
15
    public $db;
16
    public $servers;
17
    public $jobMap = [];
18
19
    /**
20
     * Constructor.
21
     * @return void
22
     */
23
    public function init()
24
    {
25
        if ($this->isEnabled()) {
26
            $this->client = \PHPDaemon\Clients\Valve\Pool::getInstance();
27
            $this->db = \MongoClient::getInstance();
28
            $this->servers = $this->db->{$this->config->dbname->value . '.servers'};
29
        }
30
    }
31
32
    /**
33
     * Creates Request.
34
     * @param object Request.
35
     * @param object Upstream application instance.
36
     * @return GameMonitorHTTPRequest Request.
37
     */
38
    public function beginRequest($req, $upstream)
39
    {
40
        return new GameMonitorHTTPRequest($this, $upstream, $req);
41
    }
42
43
    /**
44
     * Called when the worker is ready to go.
45
     * @return void
46
     */
47
    public function onReady()
48
    {
49
        if ($this->isEnabled()) {
50
            $this->updateTimer = setTimeout(function ($timer) {
0 ignored issues
show
Documentation introduced by
The property updateTimer does not exist on object<PHPDaemon\Examples\GameMonitor>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
                $this->updateAllServers();
52
                $timer->timeout(2e6);
53
            }, 1);
54
        }
55
    }
56
57
    public function updateAllServers()
58
    {
59
        gc_collect_cycles();
60
        $app = $this;
61
        $amount = 1000 - sizeof($this->jobMap);
62
        \PHPDaemon\Core\Daemon::log('amount: ' . $amount);
63
        if ($amount <= 0) {
64
            return;
65
        }
66
        $this->servers->find(function ($cursor) use ($app, $amount) {
67
            if (isset($cursor->items[0]['$err'])) {
68
                \PHPDaemon\Core\Daemon::log(\PHPDaemon\Core\Debug::dump($cursor->items));
69
                return;
70
            }
71
            foreach ($cursor->items as $server) {
72
                $app->updateServer($server);
73
            }
74
            $cursor->destroy();
75
        }, [
76
            'where' => [
77
                '$or' => [
78
                    ['atime' => ['$lte' => time() - 30], 'latency' => ['$ne' => false]],
79
                    ['atime' => ['$lte' => time() - 120], 'latency' => false],
80
                    ['atime' => null],
81
                    //['address' => 'dimon4ik.no-ip.org:27016'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
83
                ]
84
            ],
85
            //'fields' => '_id,atime,address',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
            'limit' => -max($amount, 100),
87
            'sort' => ['atime' => 1],
88
        ]);
89
    }
90
91
    public function updateServer($server)
92
    {
93
        if (!isset($server['address'])) {
94
            return;
95
        }
96
        $server['address'] = trim($server['address']);
97
        $app = $this;
98
        if (isset($app->jobMap[$server['address']])) {
99
            //\PHPDaemon\Daemon::log('already doing: '.$server['address']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
            return;
101
        }
102
        $job = new \PHPDaemon\Core\ComplexJob(function ($job) use ($app, $server) {
103
            unset($app->jobMap[$server['address']]);
104
            //\PHPDaemon\Daemon::log('Removed job for '.$server['address']. ' ('.sizeof($app->jobMap).')');
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
            $set = $job->results['info'];
106
            $set['address'] = $server['address'];
107
            $set['players'] = $job->results['players'];
108
            $set['latency'] = $job->results['latency'];
109
            $set['atime'] = time();
110
            if (0) {
111
                \PHPDaemon\Core\Daemon::log('Updated server (' . round(memory_get_usage(true) / 1024 / 1024,
112
                        5) . '): ' . $server['address'] . ' latency = ' . round($set['latency'] * 1000, 2) . ' ==== '
113
                    . (isset($server['atime']) ?
114
                        round($set['atime'] - $server['atime']) . ' secs. from last update.'
115
                        : ' =---= ' . json_encode($server))
116
                );
117
            }
118
            try {
119
                $app->servers->upsert(['_id' => $server['_id']], ['$set' => $set]);
120
            } catch (\MongoException $e) {
121
                \PHPDaemon\Core\Daemon::uncaughtExceptionHandler($e);
122
                $app->servers->upsert(['_id' => $server['_id']], ['$set' => ['atime' => time()]]);
123
            }
124
        });
125
        $app->jobMap[$server['address']] = $job;
126
        //\PHPDaemon\Daemon::log('Added job for '.$server['address']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
128
        $job('info', function ($jobname, $job) use ($app, $server) {
129
            $app->client->requestInfo($server['address'],
130
                function ($conn, $result) use ($app, $server, $jobname, $job) {
131
132
                    $job('players', function ($jobname, $job) use ($app, $server, $conn) {
133
134
                        $conn->requestPlayers(function ($conn, $result) use ($app, $jobname, $job) {
135
136
                            $job->setResult($jobname, $result);
137
                            $conn->finish();
138
139
                        });
140
                    });
141
142
                    $job->setResult($jobname, $result);
143
                });
144
        });
145
146
        $job('latency', function ($jobname, $job) use ($app, $server) {
147
148
            $app->client->ping($server['address'], function ($conn, $result) use ($app, $jobname, $job) {
149
150
                $job->setResult($jobname, $result);
151
152
                $conn->finish();
153
154
            });
155
156
        });
157
158
        $job();
159
    }
160
161
    /**
162
     * Called when worker is going to update configuration.
163
     * @return void
164
     */
165
    public function onConfigUpdated()
166
    {
167
        if ($this->client) {
168
            $this->client->config = $this->config;
169
            $this->client->onConfigUpdated();
170
        }
171
    }
172
173
    /**
174
     * Called when application instance is going to shutdown.
175
     * @return boolean Ready to shutdown?
176
     */
177
    public function onShutdown()
178
    {
179
        if ($this->client) {
180
            return $this->client->onShutdown();
181
        }
182
        return true;
183
    }
184
185
    /**
186
     * Setting default config options
187
     * Overriden from AppInstance::getConfigDefaults
188
     * Uncomment and return array with your default options
189
     * @return array|false
190
     */
191
    protected function getConfigDefaults()
192
    {
193
        return [
194
            'dbname' => 'gamemonitor',
195
        ];
196
    }
197
}
198
199
class GameMonitorHTTPRequest 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...
200
{
201
202
/**
203
 * Constructor.
204
 * @return void
205
 */
206
public function init()
207
{
208
    $req = $this;
209
210
    $job = $this->job = new \PHPDaemon\Core\ComplexJob(function () use ($req) { // called when job is done
0 ignored issues
show
Documentation introduced by
The property job does not exist on object<PHPDaemon\Examples\GameMonitorHTTPRequest>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
211
212
        $req->wakeup(); // wake up the request immediately
213
214
    });
215
216
    $job('getServers', function ($name, $job) use ($req) { // registering job named 'pingjob'
217
218
        $req->appInstance->servers->find(function ($cursor) use ($name, $job) {
0 ignored issues
show
Bug introduced by
The property servers 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...
219
            $job->setResult($name, $cursor->items);
220
        });
221
    });
222
223
    $job(); // let the fun begin
224
225
    $this->sleep(5); // setting timeout
226
}
227
228
/**
229
 * Called when request iterated.
230
 * @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...
231
 */
232
public function run()
233
{
234
$this->header('Content-Type: text/html');
235
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
236
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
237
<html xmlns="http://www.w3.org/1999/xhtml">
238
<head>
239
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
240
    <title>Game servers</title>
241
</head>
242
<body>
243
</body>
244
</html><?php
245
246
}
247
}
248