Completed
Pull Request — develop (#291)
by Armando
03:42
created

Chat::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 5
nop 1
crap 5
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Entities;
12
13
/**
14
 * Class Chat
15
 *
16
 * @link https://core.telegram.org/bots/api#chat
17
 *
18
 * @property int    $id            Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
19
 * @property string $type          Type of chat, can be either "private", "group", "supergroup" or "channel"
20
 * @property string $title         Optional. Title, for channels and group chats
21
 * @property string $username      Optional. Username, for private chats, supergroups and channels if available
22
 * @property string $first_name    Optional. First name of the other party in a private chat
23
 * @property string $last_name     Optional. Last name of the other party in a private chat
24
 * @method   int    getId()        Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
25
 * @method   string getType()      Type of chat, can be either "private ", "group", "supergroup" or "channel"
26
 * @method   string getTitle()     Optional. Title, for channels and group chats
27
 * @method   string getUsername()  Optional. Username, for private chats, supergroups and channels if available
28
 * @method   string getFirstName() Optional. First name of the other party in a private chat
29
 * @method   string getLastName()  Optional. Last name of the other party in a private chat
30
 */
31
class Chat extends Entity
32
{
33 16
    public function __construct($data)
34
    {
35 16
        parent::__construct($data);
36
37 16
        $id   = $this->getId();
38 16
        $type = $this->getType();
39 16
        if (!$type && $id !== 0) {
40 5
            $id > 0 && $this->type = 'private';
41 5
            $id < 0 && $this->type = 'group';
42
        }
43 16
    }
44
45
    /**
46
     * Try to mention the user of this chat, else return the title
47
     *
48
     * @param bool $escape_markdown
49
     *
50
     * @return string|null
51
     */
52 1
    public function tryMention($escape_markdown = false)
53
    {
54 1
        if ($this->isPrivateChat()) {
55 1
            return parent::tryMention($escape_markdown);
56
        }
57
58 1
        return $this->getTitle();
59
    }
60
61
    /**
62
     * Check if this is a group chat
63
     *
64
     * @return bool
65
     */
66 1
    public function isGroupChat()
67
    {
68 1
        return $this->getType() === 'group' || $this->getId() < 0;
69
    }
70
71
    /**
72
     * Check if this is a private chat
73
     *
74
     * @return bool
75
     */
76 2
    public function isPrivateChat()
77
    {
78 2
        return $this->getType() === 'private';
79
    }
80
81
    /**
82
     * Check if this is a super group
83
     *
84
     * @return bool
85
     */
86 1
    public function isSuperGroup()
87
    {
88 1
        return $this->getType() === 'supergroup';
89
    }
90
91
    /**
92
     * Check if this is a channel
93
     *
94
     * @return bool
95
     */
96 1
    public function isChannel()
97
    {
98 1
        return $this->getType() === 'channel';
99
    }
100
}
101