Response::setResult()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 4
nc 3
nop 1
1
<?php
2
3
4
namespace Sysbot\Telegram\ExtendedTypes;
5
6
7
use stdClass;
8
use Symfony\Component\Serializer\Exception\ExceptionInterface;
9
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
10
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
11
use Sysbot\Telegram\API;
12
use Sysbot\Telegram\Constants\ApiClasses;
13
use Sysbot\Telegram\Constants\ReturnMap;
14
use Sysbot\Telegram\Types\Animation;
15
use Sysbot\Telegram\Types\Audio;
16
use Sysbot\Telegram\Types\ChatPhoto;
17
use Sysbot\Telegram\Types\Document;
18
use Sysbot\Telegram\Types\InlineQueryResultCachedAudio;
19
use Sysbot\Telegram\Types\InlineQueryResultCachedDocument;
20
use Sysbot\Telegram\Types\InlineQueryResultCachedGif;
21
use Sysbot\Telegram\Types\InlineQueryResultCachedMpeg4Gif;
22
use Sysbot\Telegram\Types\InlineQueryResultCachedPhoto;
23
use Sysbot\Telegram\Types\InlineQueryResultCachedSticker;
24
use Sysbot\Telegram\Types\InlineQueryResultCachedVideo;
25
use Sysbot\Telegram\Types\InlineQueryResultCachedVoice;
26
use Sysbot\Telegram\Types\PassportFile;
27
use Sysbot\Telegram\Types\PhotoSize;
28
use Sysbot\Telegram\Types\Response as ResponseType;
29
use Sysbot\Telegram\Types\ResponseParameters;
30
use Sysbot\Telegram\Types\Sticker;
31
use Sysbot\Telegram\Types\TypeInterface;
32
use Sysbot\Telegram\Types\Video;
33
use Sysbot\Telegram\Types\VideoNote;
34
use Sysbot\Telegram\Types\Voice;
35
use Throwable;
36
37
trait Response
38
{
39
40
    public stdClass|TypeInterface|array|int|string|bool|null $result = null;
41
    public stdClass|ResponseParameters|null $parameters = null;
42
43
    public function __construct(private API $api, private string $methodName)
44
    {
45
    }
46
47
    /**
48
     * @param bool $ok
49
     * @return ResponseType
50
     */
51
    public function setOk(bool $ok): ResponseType
52
    {
53
        $this->ok = $ok;
54
        /** @var ResponseType $this */
55
        return $this;
56
    }
57
58
    /**
59
     * @param array|bool|int|string|object|null $result
60
     * @return ResponseType
61
     */
62
    public function setResult(object|array|int|string|bool|null $result): ResponseType
63
    {
64
        if (is_object($result) or is_array($result)) {
65
            $parsedResult = $this->parseResult($result);
66
            if (null !== $parsedResult) {
67
                $this->result = $parsedResult;
68
                return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Sysbot\Telegram\ExtendedTypes\Response which includes types incompatible with the type-hinted return Sysbot\Telegram\Types\Response.
Loading history...
69
            }
70
        }
71
        $this->result = $result;
72
        return $this;
73
    }
74
75
    /**
76
     * @param int|null $errorCode
77
     * @return ResponseType
78
     */
79
    public function setErrorCode(?int $errorCode): ResponseType
80
    {
81
        $this->errorCode = $errorCode;
82
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Sysbot\Telegram\ExtendedTypes\Response which includes types incompatible with the type-hinted return Sysbot\Telegram\Types\Response.
Loading history...
83
    }
84
85
    /**
86
     * @param string|null $description
87
     * @return ResponseType
88
     */
89
    public function setDescription(?string $description): ResponseType
90
    {
91
        $this->description = $description;
92
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Sysbot\Telegram\ExtendedTypes\Response which includes types incompatible with the type-hinted return Sysbot\Telegram\Types\Response.
Loading history...
93
    }
94
95
    public function setParameters(?object $parameters): ResponseType
96
    {
97
        if (!empty($parameters) and !$parameters instanceof ResponseParameters) {
98
            try {
99
                $parsedParameters = $this->api->getSerializer()->denormalize(
100
                    $parameters,
101
                    ResponseParameters::class,
102
                    null,
103
                    [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true]
104
                );
105
                $parameters = $parsedParameters;
106
            } catch (ExceptionInterface) {
107
                // nothing
108
            }
109
        }
110
        $this->parameters = $parameters;
111
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Sysbot\Telegram\ExtendedTypes\Response which includes types incompatible with the type-hinted return Sysbot\Telegram\Types\Response.
Loading history...
112
    }
113
114
    /**
115
     * @param object|array $result
116
     * @return TypeInterface|TypeInterface[]|null
117
     */
118
    public function parseResult(object|array $result): TypeInterface|array|null
119
    {
120
        $parsedResult = null;
121
        $resultTypes = ReturnMap::MAP[$this->methodName] ?? null;
122
        if (empty($resultTypes)) {
123
            return null;
124
        }
125
        foreach ($resultTypes as $resultType) {
126
            $args = [AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true];
127
            $args += ApiClasses::buildArgs($this->api);
128
            if (is_array($result)) {
129
                $resultType .= '[]';
130
            }
131
            try {
132
                $parsedResult = $this->api->getSerializer()->denormalize($result, $resultType, null, $args);
133
            } catch (Throwable) {
134
                $parsedResult = null;
135
            }
136
        }
137
        return $parsedResult;
138
    }
139
}