Completed
Push — master ( 0b71f7...501a02 )
by Camilo
11:03
created

FormConstructor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 25.81%
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 91
ccs 8
cts 31
cp 0.2581
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A constructFormData() 0 20 3
A checkSpecialConditions() 0 21 4
B buildMultipartFormData() 0 24 3
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
    public function constructFormData(TelegramMethods $method): array
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
    public function checkSpecialConditions(TelegramMethods $method): array
44
    {
45
        $method->performSpecialConditions();
46
47
        $return = [false];
48
49
        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...
50
            if (is_object($value)) {
51
                if (get_class($value) == 'unreal4u\\Telegram\\Types\\Custom\\InputFile') {
52
                    // If we are about to send a file, we must use the multipart/form-data way
53
                    $this->formType = 'multipart/form-data';
54
                    $return = [
55
                        'id' => $key,
56
                        'stream' => $value->getStream(),
57
                    ];
58
                }
59
            }
60
        }
61
62
        return $return;
63
    }
64
65
    /**
66
     * Builds up a multipart form-like array for Guzzle
67
     *
68
     * @param array $data The original object in array form
69
     * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is
70
     * @param resource $stream The actual file handler
71
     * @return array Returns the actual formdata to be sent
72
     */
73 2
    public function buildMultipartFormData(array $data, string $fileKeyName, $stream): array
74
    {
75
        $formData = [
76 2
            'multipart' => [],
77
        ];
78
79 2
        foreach ($data as $id => $value) {
80
            // Always send as a string unless it's a file
81
            $multiPart = [
82 1
                'name' => $id,
83
                'contents' => null,
84
            ];
85
86 1
            if ($id === $fileKeyName) {
87
                $multiPart['contents'] = $stream;
88
            } else {
89 1
                $multiPart['contents'] = (string)$value;
90
            }
91
92 1
            $formData['multipart'][] = $multiPart;
93
        }
94
95 2
        return $formData;
96
    }
97
}
98