1
<?php
2

3
declare(strict_types=1);
4

5
namespace Kreait\Firebase\Messaging;
6

7
use Kreait\Firebase\Exception\InvalidArgumentException;
8
use Kreait\Firebase\Exception\Messaging\InvalidArgument;
9

10
final class CloudMessage implements Message
11
{
12
    /** @var MessageTarget|null */
13
    private $target;
14

15
    /** @var MessageData|null */
16
    private $data;
17

18
    /** @var Notification|null */
19
    private $notification;
20

21
    /** @var AndroidConfig|null */
22
    private $androidConfig;
23

24
    /** @var ApnsConfig|null */
25
    private $apnsConfig;
26

27
    /** @var WebPushConfig|null */
28
    private $webPushConfig;
29

30
    /** @var FcmOptions|null */
31
    private $fcmOptions;
32

33 12
    private function __construct()
34
    {
35
    }
36

37
    /**
38
     * @param string $type One of "condition", "token", "topic"
39
     *
40
     * @throws InvalidArgumentException if the target type or value is invalid
41
     */
42 9
    public static function withTarget(string $type, string $value): self
43
    {
44 9
        return self::new()->withChangedTarget($type, $value);
45
    }
46

47 12
    public static function new(): self
48
    {
49 12
        return new self();
50
    }
51

52
    /**
53
     * @param array<string, mixed> $data
54
     */
55 12
    public static function fromArray(array $data): self
56
    {
57 12
        $new = new self();
58

59 12
        if (\count(\array_intersect(\array_keys($data), MessageTarget::TYPES)) > 1) {
60 9
            throw new InvalidArgument(
61
                'A message can only have one of the following targets: '
62 9
                .\implode(', ', MessageTarget::TYPES)
63
            );
64
        }
65

66 12
        if ($targetValue = $data[MessageTarget::CONDITION] ?? null) {
67 3
            $new = $new->withChangedTarget(MessageTarget::CONDITION, (string) $targetValue);
68 12
        } elseif ($targetValue = $data[MessageTarget::TOKEN] ?? null) {
69 3
            $new = $new->withChangedTarget(MessageTarget::TOKEN, (string) $targetValue);
70 12
        } elseif ($targetValue = $data[MessageTarget::TOPIC] ?? null) {
71 0
            $new = $new->withChangedTarget(MessageTarget::TOPIC, (string) $targetValue);
72
        }
73

74 12
        if ($data['data'] ?? null) {
75 3
            $new = $new->withData($data['data']);
76
        }
77

78 12
        if ($data['notification'] ?? null) {
79 3
            $new = $new->withNotification(Notification::fromArray($data['notification']));
80
        }
81

82 12
        if ($data['android'] ?? null) {
83 3
            $new = $new->withAndroidConfig(AndroidConfig::fromArray($data['android']));
84
        }
85

86 12
        if ($data['apns'] ?? null) {
87 3
            $new = $new->withApnsConfig(ApnsConfig::fromArray($data['apns']));
88
        }
89

90 12
        if ($data['webpush'] ?? null) {
91 3
            $new = $new->withWebPushConfig(WebPushConfig::fromArray($data['webpush']));
92
        }
93

94 12
        if ($data['fcm_options'] ?? null) {
95 3
            $new = $new->withFcmOptions(FcmOptions::fromArray($data['fcm_options']));
96
        }
97

98 12
        return $new;
99
    }
100

101
    /**
102
     * @param string $type One of "condition", "token", "topic"
103
     *
104
     * @throws InvalidArgumentException if the target type or value is invalid
105
     */
106 12
    public function withChangedTarget(string $type, string $value): self
107
    {
108 12
        $new = clone $this;
109 12
        $new->target = MessageTarget::with($type, $value);
110

111 12
        return $new;
112
    }
113

114
    /**
115
     * @param MessageData|array<string, string> $data
116
     *
117
     * @throws InvalidArgumentException
118
     */
119 12
    public function withData($data): self
120
    {
121 12
        $new = clone $this;
122 12
        $new->data = $data instanceof MessageData ? $data : MessageData::fromArray($data);
123

124 12
        return $new;
125
    }
126

127
    /**
128
     * @param Notification|array<string, string> $notification
129
     *
130
     * @throws InvalidArgumentException
131
     */
132 12
    public function withNotification($notification): self
133
    {
134 12
        $new = clone $this;
135 12
        $new->notification = $notification instanceof Notification ? $notification : Notification::fromArray($notification);
136

137 12
        return $new;
138
    }
139

140
    /**
141
     * @param AndroidConfig|array<string, mixed> $config
142
     *
143
     * @throws InvalidArgumentException
144
     */
145 3
    public function withAndroidConfig($config): self
146
    {
147 3
        $new = clone $this;
148 3
        $new->androidConfig = $config instanceof AndroidConfig ? $config : AndroidConfig::fromArray($config);
149

150 3
        return $new;
151
    }
152

153
    /**
154
     * @param ApnsConfig|array<string, mixed> $config
155
     *
156
     * @throws InvalidArgumentException
157
     */
158 3
    public function withApnsConfig($config): self
159
    {
160 3
        $new = clone $this;
161 3
        $new->apnsConfig = $config instanceof ApnsConfig ? $config : ApnsConfig::fromArray($config);
162

163 3
        return $new;
164
    }
165

166
    /**
167
     * @param WebPushConfig|array<string, mixed> $config
168
     */
169 3
    public function withWebPushConfig($config): self
170
    {
171 3
        $new = clone $this;
172 3
        $new->webPushConfig = $config instanceof WebPushConfig ? $config : WebPushConfig::fromArray($config);
173

174 3
        return $new;
175
    }
176

177
    /**
178
     * @param FcmOptions|array<string, mixed> $options
179
     */
180 12
    public function withFcmOptions($options): self
181
    {
182 12
        $new = clone $this;
183 12
        $new->fcmOptions = $options instanceof FcmOptions ? $options : FcmOptions::fromArray($options);
184

185 12
        return $new;
186
    }
187

188 9
    public function hasTarget(): bool
189
    {
190 9
        return (bool) $this->target;
191
    }
192

193
    /**
194
     * @return array<string, mixed>
195
     */
196 12
    public function jsonSerialize(): array
197
    {
198
        $data = [
199 12
            'data' => $this->data,
200 12
            'notification' => $this->notification,
201 12
            'android' => $this->androidConfig,
202 12
            'apns' => $this->apnsConfig,
203 12
            'webpush' => $this->webPushConfig,
204 12
            'fcm_options' => $this->fcmOptions,
205
        ];
206

207 12
        if ($this->target) {
208 12
            $data[$this->target->type()] = $this->target->value();
209
        }
210

211 12
        return \array_filter($data);
212
    }
213
}

Read our documentation on viewing source code .

Loading