Test Failed
Push — master ( e0bb23...a0e316 )
by Antonio Carlos
31:20
created

src/Checkers/Broadcasting.php (7 issues)

Labels
Severity

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
2
3
namespace PragmaRX\Health\Checkers;
4
5
use Carbon\Carbon;
6
use PragmaRX\Health\Support\Result;
7
use PragmaRX\Health\Events\HealthPing;
8
use PragmaRX\Health\Support\Traits\Routing;
9
use PragmaRX\Health\Support\Traits\Database;
0 ignored issues
show
This use statement conflicts with another class in this namespace, PragmaRX\Health\Checkers\Database.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
11
class Broadcasting extends Base
12
{
13
    use Routing, Database;
14
15
    protected function bootRouter()
16
    {
17
        $this->target->routes->each(function ($route, $name) {
0 ignored issues
show
The property routes does not seem to exist in PragmaRX\Health\Support\Target.

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...
18
            $this->registerRoute($route, $name);
19
        });
20
    }
21
22
    /**
23
     * Check resource.
24
     *
25
     * @return Result
26
     */
27
    public function check()
28
    {
29
        $this->loadDatabase();
30
31
        $this->bootRouter();
32
33
        $isHealthy = !$this->pingTimedout();
34
35
        $this->createPing();
36
37
        $this->dispatchEvent();
38
39
        return $this->makeResult($isHealthy, $this->target->getErrorMessage());
40
    }
41
42
    /**
43
     * Dispatch event.
44
     */
45
    protected function dispatchEvent()
46
    {
47
        event(
48
            new HealthPing(
49
                $this->target->channel,
0 ignored issues
show
The property channel does not seem to exist in PragmaRX\Health\Support\Target.

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...
50
                route($this->target->routeName, [$this->target->secret]),
0 ignored issues
show
The property routeName does not seem to exist in PragmaRX\Health\Support\Target.

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...
The property secret does not seem to exist in PragmaRX\Health\Support\Target.

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...
51
                $this->target
52
            )
53
        );
54
    }
55
56
    /**
57
     * Create and persist ping.
58
     */
59
    protected function createPing()
60
    {
61
        $this->database->push($this->createPingRow());
62
63
        $this->persist();
64
    }
65
66
    /**
67
     * Create ping row array.
68
     *
69
     * @return array
70
     */
71
    protected function createPingRow()
72
    {
73
        info('Laravel Health Panel - PING - secret: ' . $this->target->secret);
0 ignored issues
show
The property secret does not seem to exist in PragmaRX\Health\Support\Target.

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...
74
75
        return [
76
            'pinged_at' => Carbon::now(),
77
            'ponged_at' => null,
78
            'secret' => $this->target->secret,
79
        ];
80
    }
81
82
    /**
83
     * Parse date.
84
     *
85
     * @param $date
86
     * @return Carbon
87
     */
88
    protected function parseDate($date)
89
    {
90
        return Carbon::parse($date['date'], $date['timezone']);
91
    }
92
93
    /**
94
     * Create and persist pong.
95
     *
96
     * @param $secret
97
     */
98
    public function pong($secret)
99
    {
100
        info('Laravel Health Panel - PONG - secret: ' . $secret);
101
102
        $this->database = $this->database->map(function ($item) use ($secret) {
103
            if ($item['secret'] == $secret) {
104
                $item['ponged_at'] = Carbon::now();
105
            }
106
107
            return $item;
108
        });
109
110
        $this->persist();
111
    }
112
113
    /**
114
     * Check if a ping timed out.
115
     *
116
     * @return bool
117
     */
118
    protected function pingTimedout()
119
    {
120
        $timedout = false;
121
122
        $this->database = $this->database->filter(function ($item) use (
123
            &$timedout
124
        ) {
125
            if (!$item['ponged_at']) {
126
                if (
127
                    Carbon::now()->diffInSeconds(
128
                        $this->parseDate($item['pinged_at'])
129
                    ) > $this->target->timeout
0 ignored issues
show
The property timeout does not seem to exist in PragmaRX\Health\Support\Target.

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...
130
                ) {
131
                    $timedout = true;
132
133
                    return false;
134
                }
135
136
                return true;
137
            }
138
139
            return false;
140
        });
141
142
        return $timedout;
143
    }
144
}
145