Completed
Push — feature/improve-code ( e986a1...36787d )
by Avtandil
14:14
created

User::__construct()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6.0702

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 13
rs 8.8571
ccs 7
cts 8
cp 0.875
cc 6
eloc 7
nc 18
nop 1
crap 6.0702
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
use Longman\TelegramBot\Exception\TelegramException;
14
15
class User extends Entity
16
{
17
    /**
18
     * @var mixed|null
19
     */
20
    protected $id;
21
22
    /**
23
     * @var mixed|null
24
     */
25
    protected $first_name;
26
27
    /**
28
     * @var mixed|null
29
     */
30
    protected $last_name;
31
32
    /**
33
     * @var mixed|null
34
     */
35
    protected $username;
36
37
    /**
38
     * User constructor.
39
     *
40
     * @param array $data
41
     * @throws \Longman\TelegramBot\Exception\TelegramException
42
     */
43 17
    public function __construct(array $data)
44
    {
45
46 17
        $this->id = isset($data['id']) ? $data['id'] : null;
47 17
        if (empty($this->id)) {
48
            throw new TelegramException('id is empty!');
49
        }
50
51 17
        $this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
52
53 17
        $this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
54 17
        $this->username  = isset($data['username']) ? $data['username'] : null;
55 17
    }
56
57
    /**
58
     * Get id
59
     *
60
     * @return mixed|null
61
     */
62 8
    public function getId()
63
    {
64 8
        return $this->id;
65
    }
66
67
    /**
68
     * Get first name
69
     *
70
     * @return mixed|null
71
     */
72 8
    public function getFirstName()
73
    {
74 8
        return $this->first_name;
75
    }
76
77
    /**
78
     * Get last name
79
     *
80
     * @return mixed|null
81
     */
82 6
    public function getLastName()
83
    {
84 6
        return $this->last_name;
85
    }
86
87
    /**
88
     * Get username
89
     *
90
     * @return mixed|null
91
     */
92 8
    public function getUsername()
93
    {
94 8
        return $this->username;
95
    }
96
97
    /**
98
     * Try nebtion
99
     *
100
     * @return mixed|null|string
101
     */
102
    public function tryMention()
103
    {
104 View Code Duplication
        if (is_null($this->username)) {
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...
105
            if (!is_null($this->last_name)) {
106
                return $this->first_name . ' ' . $this->last_name;
107
            }
108
            return $this->first_name;
109
        }
110
        return '@' . $this->username;
111
    }
112
}
113