Completed
Push — master ( 53691c...15e6b6 )
by Marcel
01:44
created

HttpStatisticsLogger::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Statistics\Logger;
4
5
use Clue\React\Buzz\Browser;
6
use Ratchet\ConnectionInterface;
7
use function GuzzleHttp\Psr7\stream_for;
8
use BeyondCode\LaravelWebSockets\Apps\App;
9
use BeyondCode\LaravelWebSockets\Statistics\Statistic;
10
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
11
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
12
13
class HttpStatisticsLogger implements StatisticsLogger
14
{
15
    /** @var \BeyondCode\LaravelWebSockets\Statistics\Statistic[] */
16
    protected $statistics = [];
17
18
    /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
19
    protected $channelManager;
20
21
    /** @var \Clue\React\Buzz\Browser */
22
    protected $browser;
23
24
    public function __construct(ChannelManager $channelManager, Browser $browser)
25
    {
26
        $this->channelManager = $channelManager;
27
28
        $this->browser = $browser;
29
    }
30
31
    public function webSocketMessage(ConnectionInterface $connection)
32
    {
33
        $this
34
            ->findOrMakeStatisticForAppId($connection->app->id)
0 ignored issues
show
Bug introduced by
Accessing app on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
35
            ->webSocketMessage();
36
    }
37
38
    public function apiMessage($appId)
39
    {
40
        $this
41
            ->findOrMakeStatisticForAppId($appId)
42
            ->apiMessage();
43
    }
44
45
    public function connection(ConnectionInterface $connection)
46
    {
47
        $this
48
            ->findOrMakeStatisticForAppId($connection->app->id)
0 ignored issues
show
Bug introduced by
Accessing app on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49
            ->connection();
50
    }
51
52
    public function disconnection(ConnectionInterface $connection)
53
    {
54
        $this
55
            ->findOrMakeStatisticForAppId($connection->app->id)
0 ignored issues
show
Bug introduced by
Accessing app on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56
            ->disconnection();
57
    }
58
59
    protected function findOrMakeStatisticForAppId($appId): Statistic
60
    {
61
        if (! isset($this->statistics[$appId])) {
62
            $this->statistics[$appId] = new Statistic($appId);
63
        }
64
65
        return $this->statistics[$appId];
66
    }
67
68
    public function save()
69
    {
70
        foreach ($this->statistics as $appId => $statistic) {
71
            if (! $statistic->isEnabled()) {
72
                continue;
73
            }
74
75
            $postData = array_merge($statistic->toArray(), [
76
                'secret' => App::findById($appId)->secret,
77
            ]);
78
79
            $this
80
                ->browser
81
                ->post(
82
                    action([WebSocketStatisticsEntriesController::class, 'store']),
83
                    ['Content-Type' => 'application/json'],
84
                    stream_for(json_encode($postData))
85
                );
86
87
            $currentConnectionCount = $this->channelManager->getConnectionCount($appId);
88
            $statistic->reset($currentConnectionCount);
89
        }
90
    }
91
}
92