InputMedia::getInputMedia()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Sysbot\Telegram\ExtendedTypes;
5
6
7
use GuzzleHttp\Psr7\Utils;
8
use Psr\Http\Message\StreamInterface;
9
use Sysbot\Telegram\Exceptions\BadFileException;
10
use Sysbot\Telegram\Exceptions\InvalidArgumentException;
11
12
trait InputMedia
13
{
14
15
    protected StreamInterface|string $inputMedia;
16
    protected bool $multipart = false;
17
    public string $media;
18
    protected ?string $filename;
19
20
21
    /**
22
     * InputFile constructor.
23
     * @param mixed $inputMedia
24
     * @param string|null $filename
25
     * @throws BadFileException
26
     * @throws InvalidArgumentException
27
     */
28
    public function __construct(mixed $inputMedia, string $filename = null)
29
    {
30
        $attachId = md5(uniqid(more_entropy: true));
31
        if (is_resource($inputMedia) and get_resource_type($inputMedia) == 'stream') {
32
            $streamData = stream_get_meta_data($inputMedia);
33
            if (empty($filename)) {
34
                if ($streamData['wrapper_type'] != 'plainfile') {
35
                    throw new InvalidArgumentException('Filename is required for non-plainfile stream uploads');
36
                }
37
                $this->filename = $streamData['uri'];
38
            }
39
            $this->media = $attachId;
40
            $this->multipart = true;
41
            $this->inputMedia = Utils::streamFor($inputMedia);
42
            return;
43
        }
44
        if (!is_string($inputMedia)) {
45
            throw new InvalidArgumentException('Invalid file type');
46
        }
47
        if (file_exists($inputMedia)) {
48
            if (is_dir($inputMedia)) {
49
                throw new BadFileException('You can\'t upload a directory');
50
            }
51
            $this->filename = basename($inputMedia);
52
            $this->media = $attachId;
53
            $this->multipart = true;
54
            $inputMedia = fopen($inputMedia, 'rb');
55
            if (false === $inputMedia) {
56
                throw new BadFileException('Unable to open file');
57
            }
58
        }
59
        $this->filename ??= $filename;
60
        $this->inputMedia = Utils::streamFor($inputMedia);
61
        $this->media = $inputMedia;
62
    }
63
64
    /**
65
     * @return string|StreamInterface
66
     */
67
    public function getInputMedia(): string|StreamInterface
68
    {
69
        return $this->inputMedia;
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function isMultipart(): bool
76
    {
77
        return $this->multipart;
78
    }
79
80
    public static function create(mixed $inputMedia, string $type = null): \Sysbot\Telegram\Types\InputMedia
81
    {
82
        if (empty($type)) {
83
            $type = static::class;
84
            /** @noinspection PhpIncompatibleReturnTypeInspection */
85
            return new $type($inputMedia);
86
        }
87
        $class = \Sysbot\Telegram\Types\InputMedia::class . ucfirst($type);
88
        if (!class_exists($class)) {
89
            throw new InvalidArgumentException('Invalid media type');
90
        }
91
        return new $class($inputMedia);
92
    }
93
94
    /**
95
     * @return string|null
96
     */
97
    public function getFilename(): ?string
98
    {
99
        return $this->filename;
100
    }
101
102
}