Completed
Pull Request — master (#171)
by
unknown
02:33
created

User::setCurrentConversation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Telegram\Bot\Objects;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\FilesystemCache;
7
use Doctrine\Common\Cache\SQLite3Cache;
8
use Telegram\Bot\Conversations\Conversation;
9
use Telegram\Bot\Conversations\HelloConversation;
10
11
/**
12
 * Class User.
13
 *
14
 *
15
 * @method int      getId()         Unique identifier for this user or bot.
16
 * @method string   getFirstName()  User's or bot's first name.
17
 * @method string   getLastName()   (Optional). User's or bot's last name.
18
 * @method string   getUsername()   (Optional). User's or bot's username.
19
 */
20
class User extends BaseObject
21
{
22
    /**
23
     * @var string $currentConversation
24
     */
25
    private $currentConversation;
26
27
    /**
28
     * @var Cache $cache
29
     */
30
    private $cache;
31
32
    /**
33
     * @inheritdoc
34
     */
35 10
    public function __construct($data, Cache $cache = null)
36
    {
37 10
        parent::__construct($data);
38
39 10
        $this->cache = is_null($cache) ? new FilesystemCache(__DIR__.'/../Storage/cache') : $cache;
40 10
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getCurrentConversation()
46
    {
47
        if (is_null($this->currentConversation)) {
48
            $cacheId = $this->getId() . '_current_conversation';
49
            if (($currentConversation = $this->cache->fetch($cacheId)) !== false) {
50
                $this->currentConversation = $currentConversation;
51
52
                return $currentConversation;
53
            };
54
55
            $this->setCurrentConversation(HelloConversation::class);
56
        }
57
58
        return $this->currentConversation;
59
    }
60
61
    /**
62
     * @param string $currentConversation
63
     */
64
    public function setCurrentConversation($currentConversation)
65
    {
66
        $cacheId = $this->getId() . '_current_conversation';
67
        
68
        $this->cache->save($cacheId, $currentConversation);
69
        
70
        $this->currentConversation = $currentConversation;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 10
    public function relations()
77
    {
78 10
        return [];
79
    }
80
}
81