InputFile   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 19
c 1
b 0
f 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 21 7
A getInputFile() 0 3 1
A isMultipart() 0 3 1
1
<?php
2
3
namespace Sysbot\Telegram\ExtendedTypes;
4
5
6
use Sysbot\Telegram\Exceptions\BadFileException;
7
use Sysbot\Telegram\Exceptions\InvalidArgumentException;
8
9
trait InputFile
10
{
11
12
    private mixed $inputFile;
13
    private bool $multipart = false;
14
15
16
    /**
17
     * InputFile constructor.
18
     * @param mixed $inputFile
19
     * @throws InvalidArgumentException
20
     * @throws BadFileException
21
     */
22
    public function __construct(mixed $inputFile)
23
    {
24
        if (is_resource($inputFile) and get_resource_type($inputFile) == 'stream') {
25
            $this->multipart = true;
26
            $this->inputFile = $inputFile;
27
            return;
28
        }
29
        if (!is_string($inputFile)) {
30
            throw new InvalidArgumentException('Invalid file type');
31
        }
32
        if (file_exists($inputFile)) {
33
            if (is_dir($inputFile)) {
34
                throw new BadFileException('You can\'t upload a directory');
35
            }
36
            $this->multipart = true;
37
            $inputFile = fopen($inputFile, 'rb');
38
            if (false === $inputFile) {
39
                throw new BadFileException('Unable to open file');
40
            }
41
        }
42
        $this->inputFile = $inputFile;
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48
    public function getInputFile(): mixed
49
    {
50
        return $this->inputFile;
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function isMultipart(): bool
57
    {
58
        return $this->multipart;
59
    }
60
61
}
62