PusherBroadcaster::getMessageText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Broadcast;
15
16
use GuzzleHttp\Exception\GuzzleException;
17
use JsonException;
18
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Pusher\ApiErrorException;
20
use Pusher\Pusher;
21
use Pusher\PusherException;
22
use Valkyrja\Broadcast\Contract\Broadcaster as Contract;
23
use Valkyrja\Broadcast\Data\Contract\Message;
24
use Valkyrja\Type\BuiltIn\Support\Arr;
25
26
/**
27
 * Class PusherBroadcaster.
28
 *
29
 * @author Melech Mizrachi
30
 */
31
class PusherBroadcaster implements Contract
32
{
33
    /**
34
     * PusherBroadcaster constructor.
35
     *
36
     * @param Pusher $pusher The pusher service
37
     */
38
    public function __construct(
39
        protected Pusher $pusher
40
    ) {
41
    }
42
43
    /**
44
     * @inheritDoc
45
     *
46
     * @throws JsonException
47
     * @throws GuzzleException
48
     * @throws ApiErrorException
49
     * @throws PusherException
50
     */
51
    #[Override]
52
    public function send(Message $message): void
53
    {
54
        $this->pusher->trigger(
55
            $message->getChannel(),
56
            $message->getEvent(),
57
            $this->getMessageText($message)
58
        );
59
    }
60
61
    /**
62
     * Get the message text.
63
     *
64
     * @param Message $message The message
65
     *
66
     * @throws JsonException On json decode failure
67
     *
68
     * @return string
69
     */
70
    protected function getMessageText(Message $message): string
71
    {
72
        $this->prepareMessage($message);
73
74
        return $message->getMessage();
75
    }
76
77
    /**
78
     * Prepare a message that has data.
79
     *
80
     * @param Message $message The message
81
     *
82
     * @throws JsonException On json decode failure
83
     *
84
     * @return void
85
     */
86
    protected function prepareMessage(Message $message): void
87
    {
88
        $data = $message->getData();
89
90
        if ($data !== null && $data !== []) {
91
            $message->setMessage(Arr::toString($data));
92
        }
93
    }
94
}
95