Completed
Pull Request — master (#95)
by
unknown
04:48
created

SendMediaGroup::performSpecialConditions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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