Passed
Push — master ( e32a81...12baf4 )
by Antonio Carlos
02:26
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 1
    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 1
            $this->registerRoute($route, $name);
19 1
        });
20 1
    }
21
22
    /**
23
     * Check resource.
24
     *
25
     * @return Result
26
     */
27 1
    public function check()
28
    {
29 1
        $this->loadDatabase();
30
31 1
        $this->bootRouter();
32
33 1
        $isHealthy = ! $this->pingTimedout();
34
35 1
        $this->createPing();
36
37 1
        $this->dispatchEvent();
38
39 1
        return $this->makeResult($isHealthy, $this->target->getErrorMessage());
40
    }
41
42
    /**
43
     * Dispatch event.
44
     */
45 1
    protected function dispatchEvent()
46
    {
47 1
        event(
48 1
            new HealthPing(
49 1
                $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 1
                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 1
                $this->target
52
            )
53
        );
54 1
    }
55
56
    /**
57
     * Create and persist ping.
58
     */
59 1
    protected function createPing()
60
    {
61 1
        $this->database->push($this->createPingRow());
62
63 1
        $this->persist();
64 1
    }
65
66
    /**
67
     * Create ping row array.
68
     *
69
     * @return array
70
     */
71 1
    protected function createPingRow()
72
    {
73
        return [
74 1
            'pinged_at' => Carbon::now(),
75
            'ponged_at' => null,
76 1
            '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...
77
        ];
78
    }
79
80
    /**
81
     * Parse date.
82
     *
83
     * @param $date
84
     * @return Carbon
85
     */
86
    protected function parseDate($date)
87
    {
88
        return Carbon::parse($date['date'], $date['timezone']);
89
    }
90
91
    /**
92
     * Create and persist pong.
93
     *
94
     * @param $secret
95
     */
96
    public function pong($secret)
97
    {
98
        $this->database = $this->database->map(function ($item) use ($secret) {
99
            if ($item['secret'] == $secret) {
100
                $item['ponged_at'] = Carbon::now();
101
            }
102
103
            return $item;
104
        });
105
106
        $this->persist();
107
    }
108
109
    /**
110
     * Check if a ping timed out.
111
     *
112
     * @return bool
113
     */
114 1
    protected function pingTimedout()
115
    {
116 1
        $timedout = false;
117
118
        $this->database = $this->database->filter(function ($item) use (
119
            &$timedout
120
        ) {
121
            if (! $item['ponged_at']) {
122
                if (
123
                    Carbon::now()->diffInSeconds(
124
                        $this->parseDate($item['pinged_at'])
125
                    ) > $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...
126
                ) {
127
                    $timedout = true;
128
129
                    return false;
130
                }
131
132
                return true;
133
            }
134
135
            return false;
136 1
        });
137
138 1
        return $timedout;
139
    }
140
}
141