Completed
Push — 3.x ( bc12f6...bea62d )
by Ryota
25s queued 20s
created

Messages::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Polidog\Chatwork\Api\Rooms;
6
7
use Polidog\Chatwork\Client\ClientInterface;
8
use Polidog\Chatwork\Entity\Collection\CollectionInterface;
9
use Polidog\Chatwork\Entity\EntityInterface;
10
use Polidog\Chatwork\Entity\Factory\MessageFactory;
11
use Polidog\Chatwork\Entity\Message;
12
13
/**
14
 * Class Messages.
15
 */
16
class Messages
17
{
18
    /**
19
     * @var ClientInterface
20
     */
21
    private $client;
22
23
    /**
24
     * @var MessageFactory
25
     */
26
    private $factory;
27
28
    /**
29
     * @var int
30
     */
31
    private $roomId;
32
33
    /**
34
     * Messages constructor.
35
     *
36
     * @param ClientInterface $client
37
     * @param MessageFactory  $factory
38
     * @param int             $roomId
39
     */
40
    public function __construct(ClientInterface $client, MessageFactory $factory, int $roomId)
41
    {
42
        $this->client = $client;
43
        $this->factory = $factory;
44
        $this->roomId = $roomId;
45
    }
46
47
    /**
48
     * @param bool $force
49
     *
50
     * @return CollectionInterface
51
     */
52
    public function show($force = false)
53
    {
54
        return $this->factory->collection(
55
            $this->client->get(
56
                "rooms/{$this->roomId}/messages",
57
                [
58
                    'force' => (int) $force,
59
                ]
60
            )
61
        );
62
    }
63
64
    /**
65
     * @param $id
66
     * @param bool $force
67
     *
68
     * @return Message|EntityInterface
69
     */
70
    public function detail($id, $force = false)
71
    {
72
        return $this->factory->entity(
73
            $this->client->get(
74
                "rooms/{$this->roomId}/messages/{$id}",
75
                [
76
                    'force' => (int) $force,
77
                ]
78
            )
79
        );
80
    }
81
82
    /**
83
     * @param Message $message
84
     */
85
    public function create(Message $message)
86
    {
87
        $result = $this->client->post(
88
            "rooms/{$this->roomId}/messages",
89
            [
90
                'body' => $message->body,
91
            ]
92
        );
93
94
        $message->messageId = $result['message_id'];
95
    }
96
97
    /**
98
     * @param Message $message
99
     * @param $id
100
     */
101
    public function update(Message $message, $id)
102
    {
103
        $result = $this->client->put(
104
            "rooms/{$this->roomId}/messages/{$id}",
105
            [
106
                'body' => $message->body,
107
            ]
108
        );
109
110
        $message->messageId = $result['message_id'];
111
    }
112
113
    /**
114
     * @param $id
115
     */
116
    public function delete($id)
117
    {
118
        $result = $this->client->delete(
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
119
            "rooms/{$this->roomId}/messages/{$id}"
120
        );
121
    }
122
}
123