Completed
Push — master ( 615879...2eb041 )
by Vladimir
02:48
created

FacebookReceivedMessage::hasData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Channels\Facebook;
6
7
use FondBot\Contracts\Channels\ReceivedMessage;
8
use FondBot\Contracts\Channels\Message\Location;
9
use FondBot\Contracts\Channels\Message\Attachment;
10
11
class FacebookReceivedMessage implements ReceivedMessage
12
{
13
    private $payload;
14
15 3
    public function __construct(array $payload)
16
    {
17 3
        $this->payload = $payload;
18 3
    }
19
20
    /**
21
     * Get text.
22
     *
23
     * @return string|null
24
     */
25 2
    public function getText(): ?string
26
    {
27 2
        return $this->payload['text'] ?? null;
28
    }
29
30
    /**
31
     * Get location.
32
     *
33
     * @return Location|null
34
     */
35 2
    public function getLocation(): ?Location
36
    {
37 2
        if ($location = $this->getAttachmentPayload('location')) {
38 1
            return new Location(
39 1
                $location['coordinates']['lat'],
40 1
                $location['coordinates']['long']
41
            );
42
        }
43
44 1
        return null;
45
    }
46
47
    /**
48
     * Determine if message has attachment.
49
     *
50
     * @return bool
51
     */
52 3
    public function hasAttachment(): bool
53
    {
54 3
        return isset($this->payload['attachments']) && collect($this->payload['attachments'])
55 2
                ->whereIn('type', ['audio', 'file', 'image', 'video'])
56 3
                ->count() > 0;
57
    }
58
59
    /**
60
     * Get attachment.
61
     *
62
     * @return Attachment|null
63
     */
64 2
    public function getAttachment(): ?Attachment
65
    {
66 2
        return $this->getImage()
67 2
            ?? $this->getAudio()
68 2
            ?? $this->getVideo()
69 2
            ?? $this->getFile();
70
    }
71
72
    /**
73
     * Determine if message has data payload.
74
     *
75
     * @return bool
76
     */
77
    public function hasData(): bool
78
    {
79
        return false;
80
    }
81
82
    /**
83
     * Get data payload.
84
     *
85
     * @return string|null
86
     */
87
    public function getData(): ?string
88
    {
89
        return null;
90
    }
91
92
    /**
93
     * Get image.
94
     *
95
     * @return Attachment|null
96
     */
97 2
    public function getImage(): ?Attachment
98
    {
99 2
        if ($image = $this->getAttachmentPayload('image')) {
100 1
            return new Attachment(Attachment::TYPE_IMAGE, $image['url']);
101
        }
102
103 2
        return null;
104
    }
105
106
    /**
107
     * Get audio.
108
     *
109
     * @return Attachment|null
110
     */
111 2
    public function getAudio(): ?Attachment
112
    {
113 2
        if ($audio = $this->getAttachmentPayload('audio')) {
114 1
            return new Attachment(Attachment::TYPE_AUDIO, $audio['url']);
115
        }
116
117 2
        return null;
118
    }
119
120
    /**
121
     * Get video.
122
     *
123
     * @return Attachment|null
124
     */
125 2
    public function getVideo(): ?Attachment
126
    {
127 2
        if ($video = $this->getAttachmentPayload('video')) {
128 1
            return new Attachment(Attachment::TYPE_VIDEO, $video['url']);
129
        }
130
131 2
        return null;
132
    }
133
134
    /**
135
     * Get file.
136
     *
137
     * @return Attachment|null
138
     */
139 2
    public function getFile(): ?Attachment
140
    {
141 2
        if ($file = $this->getAttachmentPayload('file')) {
142 1
            return new Attachment(Attachment::TYPE_FILE, $file['url']);
143
        }
144
145 1
        return null;
146
    }
147
148 3
    private function getAttachmentPayload(string $type): ?array
149
    {
150 3
        if (!$attachments = $this->payload['attachments'] ?? null) {
151 1
            return null;
152
        }
153
154
        // Is it real to send many locations or something in one request?
155 2
        return collect($attachments)->first(function ($attachment) use ($type) {
156 2
            return $attachment['type'] === $type;
157 2
        })['payload'] ?? null;
158
    }
159
}
160