Completed
Push — master ( 0cb834...ce1742 )
by Camilo
04:29
created

FormConstructor::buildMultipartFormData()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 0
loc 24
ccs 0
cts 9
cp 0
rs 8.9713
cc 3
eloc 13
nc 3
nop 3
crap 12
1
<?php
2
3
namespace unreal4u\InternalFunctionality;
4
5
use unreal4u\Abstracts\TelegramMethods;
6
7
class FormConstructor
8
{
9
    /**
10
     * @param TelegramMethods $method
11
     * @return mixed
12
     */
13
    private function constructFormData(TelegramMethods $method): array
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
14
    {
15
        $result = $this->checkSpecialConditions($method);
16
17
        switch ($this->formType) {
0 ignored issues
show
Bug introduced by
The property formType does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
            case 'application/x-www-form-urlencoded':
19
                $formData = [
20
                    'form_params' => get_object_vars($method),
21
                ];
22
                break;
23
            case 'multipart/form-data':
24
                $formData = $this->buildMultipartFormData(get_object_vars($method), $result['id'], $result['stream']);
25
                break;
26
            default:
27
                $formData = [];
28
                break;
29
        }
30
31
        return $formData;
32
    }
33
34
    /**
35
     * Can perform any special checks needed to be performed before sending the actual request to Telegram
36
     *
37
     * This will return an array with data that will be different in each case (for now). This can be changed in the
38
     * future.
39
     *
40
     * @param TelegramMethods $method
41
     * @return array
42
     */
43
    private function checkSpecialConditions(TelegramMethods $method): array
44
    {
45
        $return = [false];
46
47
        foreach ($method as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $method of type object<unreal4u\Abstracts\TelegramMethods> is not traversable.
Loading history...
48
            if (is_object($value)) {
49
                if (get_class($value) == 'unreal4u\\Telegram\\Types\\Custom\\InputFile') {
50
                    // If we are about to send a file, we must use the multipart/form-data way
51
                    $this->formType = 'multipart/form-data';
52
                    $return = [
53
                        'id' => $key,
54
                        'stream' => $value->getStream(),
55
                    ];
56
                } elseif (in_array('unreal4u\\InternalFunctionality\\KeyboardMethods', class_parents($value))) {
57
                    // If we are about to send a KeyboardMethod, we must send a serialized object
58
                    $method->$key = json_encode($value);
59
                    $return = [true];
60
                }
61
            }
62
        }
63
64
        return $return;
65
    }
66
67
    /**
68
     * Builds up a multipart form-like array for Guzzle
69
     *
70
     * @param array $data The original object in array form
71
     * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is
72
     * @param resource $stream The actual file handler
73
     * @return array Returns the actual formdata to be sent
74
     */
75
    private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array
76
    {
77
        $formData = [
78
            'multipart' => [],
79
        ];
80
81
        foreach ($data as $id => $value) {
82
            // Always send as a string unless it's a file
83
            $multiPart = [
84
                'name' => $id,
85
                'contents' => null,
86
            ];
87
88
            if ($id === $fileKeyName) {
89
                $multiPart['contents'] = $stream;
90
            } else {
91
                $multiPart['contents'] = (string)$value;
92
            }
93
94
            $formData['multipart'][] = $multiPart;
95
        }
96
97
        return $formData;
98
    }
99
}
100