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

Chat::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 12
rs 9.2
ccs 8
cts 8
cp 1
cc 4
eloc 7
nc 4
nop 1
crap 4
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 14
    public function __construct($data)
34
    {
35 14
        parent::__construct($data);
36
37 14
        if (!$this->getType()) {
38 3
            if ($this->getId() > 0) {
39 2
                $this->type = 'private';
40 1
            } elseif ($this->getId() < 0) {
41 1
                $this->type = 'group';
42
            }
43
        }
44 14
    }
45
46
    /**
47
     * Try mention
48
     *
49
     * @return string|null
50
     */
51
    public function tryMention()
52
    {
53
        if ($this->isPrivateChat()) {
54 View Code Duplication
            if ($this->username === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
                if ($this->last_name !== null) {
56
                    return $this->first_name . ' ' . $this->last_name;
57
                }
58
59
                return $this->first_name;
60
            }
61
62
            return '@' . $this->username;
63
        }
64
65
        return $this->getTitle();
66
    }
67
68
    /**
69
     * Check if this is a group chat
70
     *
71
     * @return bool
72
     */
73
    public function isGroupChat()
74
    {
75
        return $this->type === 'group' || $this->id < 0;
76
    }
77
78
    /**
79
     * Check if this is a private chat
80
     *
81
     * @return bool
82
     */
83
    public function isPrivateChat()
84
    {
85
        return $this->type === 'private';
86
    }
87
88
    /**
89
     * Check if this is a super group
90
     *
91
     * @return bool
92
     */
93
    public function isSuperGroup()
94
    {
95
        return $this->type === 'supergroup';
96
    }
97
98
    /**
99
     * Check if this is a channel
100
     *
101
     * @return bool
102
     */
103
    public function isChannel()
104
    {
105
        return $this->type === 'channel';
106
    }
107
}
108