ConnectionLogger   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 92
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A decorate() 0 5 1
A setConnection() 0 5 1
A close() 0 5 1
A __unset() 0 3 1
A __get() 0 3 1
A __isset() 0 3 1
A send() 0 7 1
A __set() 0 3 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Loggers;
4
5
use Ratchet\ConnectionInterface;
6
7
class ConnectionLogger extends Logger implements ConnectionInterface
8
{
9
    /**
10
     * The connection to watch.
11
     *
12
     * @var \Ratchet\ConnectionInterface
13
     */
14
    protected $connection;
15
16
    /**
17
     * Create a new instance and add a connection to watch.
18
     *
19
     * @param  \Ratchet\ConnectionInterface  $connection
20
     * @return self
21
     */
22
    public static function decorate(ConnectionInterface $app): self
23
    {
24
        $logger = app(self::class);
25
26
        return $logger->setConnection($app);
0 ignored issues
show
Bug introduced by
The method setConnection() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        return $logger->/** @scrutinizer ignore-call */ setConnection($app);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
27
    }
28
29
    /**
30
     * Set a new connection to watch.
31
     *
32
     * @param  \Ratchet\ConnectionInterface  $connection
33
     * @return $this
34
     */
35
    public function setConnection(ConnectionInterface $connection)
36
    {
37
        $this->connection = $connection;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Send data through the connection.
44
     *
45
     * @param  string  $data
46
     * @return void
47
     */
48
    public function send($data)
49
    {
50
        $socketId = $this->connection->socketId ?? null;
0 ignored issues
show
Bug introduced by
Accessing socketId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
51
52
        $this->info("Connection id {$socketId} sending message {$data}");
53
54
        $this->connection->send($data);
55
    }
56
57
    /**
58
     * Close the connection.
59
     *
60
     * @return void
61
     */
62
    public function close()
63
    {
64
        $this->warn("Connection id {$this->connection->socketId} closing.");
0 ignored issues
show
Bug introduced by
Accessing socketId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
65
66
        $this->connection->close();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function __set($name, $value)
73
    {
74
        return $this->connection->$name = $value;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function __get($name)
81
    {
82
        return $this->connection->$name;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function __isset($name)
89
    {
90
        return isset($this->connection->$name);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function __unset($name)
97
    {
98
        unset($this->connection->$name);
99
    }
100
}
101