Completed
Push — master ( 9592aa...d27900 )
by Bram
15:35
created

example.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 32 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once './vendor/autoload.php';
3
4
use Symfony\Component\HttpFoundation\Request;
5
use Symfony\Component\HttpFoundation\Response;
6
use Stack\CallableHttpKernel;
7
use BramR\Stack\Heartbeat;
8
9
$diContainer = array('db.name' => 'super_important_production_db');
10
11
$app = new  CallableHttpKernel(function (Request $request) {
0 ignored issues
show
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
12
    return new Response('#yolo');
13
});
14
15
$app = (new Stack\Builder)
16
    ->push(Heartbeat::class)
17
    ->push(Heartbeat::class, '/just-a-diffent-route')
18
    ->push(Heartbeat::class, '/custom', function () use ($diContainer) {
19
        return new Response(
20
            'Custom heartbeat to check some stuff in db:' . $diContainer['db.name']
21
        );
22
    })
23
    ->push(Heartbeat::class, '/cpu', new CPULoadChecker)
24
    ->push(Heartbeat::class, '/cpu15m', new CPULoadChecker(CPULoadChecker::INTERVAL_15M))
25
    ->resolve($app);
26
27
Stack\run($app);
28
29
/**
30
 * As an example: super simple cpu load checker class.
31
 **/
32
class CPULoadChecker
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
33
{
34
    const INTERVAL_1M = 0;
35
    const INTERVAL_5M = 1;
36
    const INTERVAL_15M = 2;
37
38
    private $interval;
39
40
    public function __construct($interval = self::INTERVAL_1M)
41
    {
42
        $this->interval = $interval;
43
    }
44
45
    public function __invoke()
46
    {
47
        $load = sys_getloadavg();
48
        if (isset($load[$this->interval])) {
49
            return new Response($load[$this->interval], 200, array('Content-Type' => 'text/plain'));
50
        } else {
51
            return new Response('Whoops!', 500, array('Content-Type' => 'text/plain'));
52
        }
53
    }
54
}
55