Completed
Push — master ( a70964...9bca72 )
by Camilo
02:18
created

TelegramMethods::mandatoryUserOrInlineMessageId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\Abstracts;
6
7
use Psr\Log\LoggerInterface;
8
use unreal4u\TelegramAPI\Exceptions\MissingMandatoryField;
9
use unreal4u\TelegramAPI\Interfaces\TelegramMethodDefinitions;
10
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
11
use unreal4u\TelegramAPI\Telegram\Types\Message;
12
13
/**
14
 * Contains methods that all Telegram methods should implement
15
 */
16
abstract class TelegramMethods implements TelegramMethodDefinitions
17
{
18
    /**
19
     * Most of the methods will return a Message object on success, so set that as the default.
20
     *
21
     * This function may however be overwritten if the method uses another object, there are many examples of this, so
22
     * just check out the rest of the code. A good place to start is GetUserProfilePhotos or LeaveChat
23
     *
24
     * @see unreal4u\TelegramAPI\Telegram\Methods\GetUserProfilePhotos
25
     * @see unreal4u\TelegramAPI\Telegram\Methods\LeaveChat
26
     *
27
     * @param TelegramRawData $data
28
     * @param LoggerInterface $logger
29
     *
30
     * @return TelegramTypes
31
     */
32 8
    public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
33
    {
34 8
        return new Message($data->getResult(), $logger);
35
    }
36
37
    /**
38
     * Before making the actual request this method will be called
39
     *
40
     * It must be used to json_encode stuff, or do other changes in the internal class representation _before_ sending
41
     * it to the Telegram servers
42
     *
43
     * @return TelegramMethods
44
     */
45 27
    public function performSpecialConditions(): TelegramMethods
46
    {
47 27
        if (!empty($this->reply_markup)) {
48 1
            $this->reply_markup = json_encode($this->reply_markup);
49
        }
50
51 27
        return $this;
52
    }
53
54
    /**
55
     * Exports the class to an array in order to send it to the Telegram servers without extra fields that we don't need
56
     *
57
     * @return array
58
     */
59 33
    final public function export(): array
60
    {
61 33
        $finalArray = [];
62 33
        $mandatoryFields = $this->getMandatoryFields();
63
64 33
        $cleanObject = new $this();
65 33
        foreach ($cleanObject as $fieldId => $value) {
66 29
            if ($this->$fieldId === $cleanObject->$fieldId) {
67 27
                if (in_array($fieldId, $mandatoryFields)) {
68 5
                    throw new MissingMandatoryField(sprintf(
69 27
                        'The field "%s" is mandatory and empty, please correct',
70
                        $fieldId
71
                    ));
72
                }
73
            } else {
74 26
                $finalArray[$fieldId] = $this->$fieldId;
75
            }
76
        }
77
78 28
        return $finalArray;
79
    }
80
81
    /**
82
     * Will resolve the dependency of a mandatory inline_message_id OR a chat_id + message_id
83
     *
84
     * NOTE: This will use pass by reference instead of copy on write as the use-case for this functions allows this
85
     *
86
     * @param array $return
87
     * @return array
88
     */
89 4
    final protected function mandatoryUserOrInlineMessageId(array &$return): array
90
    {
91 4
        if (empty($this->chat_id) && empty($this->message_id)) {
0 ignored issues
show
Bug introduced by
The property chat_id 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...
Bug introduced by
The property message_id 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...
92 3
            $return[] = 'inline_message_id';
93
        }
94
95
        // On the other hand, chat_id and message_id are mandatory if inline_message_id is not filled in
96 4
        if (empty($this->inline_message_id)) {
0 ignored issues
show
Bug introduced by
The property inline_message_id does not seem to exist. Did you mean message_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
97 3
            $return[] = 'chat_id';
98 3
            $return[] = 'message_id';
99
        }
100
101 4
        return $return;
102
    }
103
}
104