Completed
Push — master ( b496ca...bc1769 )
by Camilo
02:55
created

SendMediaGroup::performSpecialConditions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\Telegram\Methods;
6
7
use Psr\Log\LoggerInterface;
8
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
9
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
10
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse;
11
use unreal4u\TelegramAPI\Telegram\Types\Custom\MessageArray;
12
use unreal4u\TelegramAPI\Telegram\Types\InputMedia;
13
14
/**
15
 * Use this method to send photos. On success, the sent Message is returned
16
 *
17
 * Objects defined as-is January 2017
18
 *
19
 * @see https://core.telegram.org/bots/api#sendphoto
20
 */
21
class SendMediaGroup extends TelegramMethods
22
{
23
    /**
24
     * Unique identifier for the target chat or username of the target channel (in the format @channelusername)
25
     * @var string
26
     */
27
    public $chat_id = '';
28
29
    /**
30
     * A JSON-serialized array describing photos and videos to be sent
31
     * @var InputMedia[]
32
     */
33
    public $media = [];
34
35
    /**
36
     * Optional. Sends the message silently. iOS users will not receive a notification, Android users will receive a
37
     * notification with no sound.
38
     * @see https://telegram.org/blog/channels-2-0#silent-messages
39
     * @var bool
40
     */
41
    public $disable_notification = false;
42
43
    /**
44
     * Optional. If the message is a reply, ID of the original message
45
     * @var int
46
     */
47
    public $reply_to_message_id = 0;
48
49
    public function getMandatoryFields(): array
50
    {
51
        return [
52
            'chat_id',
53
            'media',
54
        ];
55
    }
56
    
57
    public static function bindToObject(TelegramResponse $data, LoggerInterface $logger): TelegramTypes
58
    {
59
        return new MessageArray($data->getResult(), $logger);
60
    }
61
62
    public function performSpecialConditions(): TelegramMethods
63
    {
64
        $imageQuantity = \count($this->media);
65
        if ($imageQuantity < 2) {
66
            throw new \RuntimeException('Must include at least 2 images');
67
        }
68
69
        if ($imageQuantity > 10) {
70
            throw new \RuntimeException('Can not include more than 10 images');
71
        }
72
73
        $this->media = json_encode($this->media);
74
75
        return parent::performSpecialConditions();
76
    }
77
}
78