Completed
Branch dev (d5d70c)
by Raffael
11:00
created

Sessions::postJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Office\Api\v2;
13
14
use Balloon\App\Api\Controller;
15
use Balloon\App\Office\Constructor\Http as App;
16
use Balloon\App\Office\Document;
17
use Balloon\App\Office\Session as WopiSession;
18
use Balloon\App\Office\Session\Member;
19
use Balloon\Exception;
20
use Balloon\Filesystem;
21
use Balloon\Filesystem\Node\File;
22
use Balloon\Server;
23
use Micro\Http\Response;
24
use MongoDB\BSON\ObjectId;
25
26
class Sessions extends Controller
27
{
28
    /**
29
     * App.
30
     *
31
     * @var App
32
     */
33
    protected $app;
34
35
    /**
36
     * Filesystem.
37
     *
38
     * @var Filesystem
39
     */
40
    protected $fs;
41
42
    /**
43
     * Server.
44
     *
45
     * @var Server
46
     */
47
    protected $server;
48
49
    /**
50
     * Constructor.
51
     *
52
     * @param App    $app
53
     * @param Server $server
54
     */
55
    public function __construct(App $app, Server $server)
56
    {
57
        $this->server = $server;
58
        $this->fs = $server->getFilesystem();
59
        $this->app = $app;
60
    }
61
62
    /**
63
     * @api {post} /api/v2/office/sessions Create session
64
     * @apiName post
65
     * @apiVersion 2.0.0
66
     * @apiGroup App\Office
67
     * @apiPermission none
68
     * @apiUse _getNode
69
     * @apiDescription Create new session for a document
70
     *
71
     * @apiExample (cURL) example:
72
     * curl -XPOST "https://SERVER/api/v2/office/session?id=58a18a4ca271f962af6fdbc4"
73
     *
74
     * @apiSuccessExample {json} Success-Response:
75
     * HTTP/1.1 201 Created
76
     * {
77
     *      "id": "544627ed3c58891f058bbbaa",
78
     *      "wopi_url": "https://localhost",
79
     *      "access_token": "544627ed3c58891f058b4622",
80
     *      "access_token_ttl": "1486989000"
81
     * }
82
     *
83
     * @param string $id
84
     * @param string $p
85
     *
86
     * @return Response
87
     */
88
    public function post(?string $id = null, ?string $p = null): Response
89
    {
90
        $node = $this->fs->getNode($id, $p, File::class);
91
        $document = new Document($this->fs->getDatabase(), $node);
0 ignored issues
show
Compatibility introduced by
$node of type object<Balloon\Filesystem\Node\NodeInterface> is not a sub-type of object<Balloon\Filesystem\Node\File>. It seems like you assume a concrete implementation of the interface Balloon\Filesystem\Node\NodeInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
92
        $ttl = $this->app->getTokenTtl();
93
94
        $session = new WopiSession($this->fs, $document, $ttl);
95
        $member = new Member($this->fs->getUser(), $ttl);
96
        $session->join($member)
97
                ->store();
98
99
        return (new Response())->setCode(201)->setBody([
100
            'id' => (string) $session->getId(),
101
            'wopi_url' => $this->app->getWopiUrl(),
102
            'access_token' => $member->getAccessToken(),
103
            'access_token_ttl' => ($member->getTTL()->toDateTime()->format('U') * 1000),
104
        ]);
105
    }
106
107
    /**
108
     * @api {post} /api/v2/office/sessions/:id/join Join session
109
     * @apiName postJoin
110
     * @apiVersion 2.0.0
111
     * @apiGroup App\Office
112
     * @apiPermission none
113
     * @apiDescription Join running session
114
     * @apiParam (GET Parameter) {string} session_id The session id to join to
115
     *
116
     * @apiExample (cURL) example:
117
     * curl -XPOST "https://SERVER/api/v2/office/session/join?session_id=58a18a4ca271f962af6fdbc4"
118
     *
119
     * @apiSuccessExample {json} Success-Response:
120
     * HTTP/1.1 200 OK
121
     * {
122
     *      "id": "544627ed3c58891f058bbbaa",
123
     *      "wopi_url": "https://localhost",
124
     *      "access_token": "544627ed3c58891f058b4622",
125
     *      "access_token_ttl": "1486989000"
126
     * }
127
     *
128
     * @param string $id
129
     *
130
     * @return Response
131
     */
132
    public function postJoin(string $id): Response
133
    {
134
        $session = WopiSession::getSessionById($this->fs, $this->parseId($id));
135
        $ttl = $this->app->getTokenTtl();
136
        $member = new Member($this->fs->getUser(), $ttl);
137
        $session->join($member)
138
                ->store();
139
140
        return (new Response())->setCode(200)->setBody([
141
            'id' => (string) $session->getId(),
142
            'wopi_url' => $this->app->getWopiUrl(),
143
            'access_token' => $member->getAccessToken(),
144
            'access_token_ttl' => ($member->getTTL()->toDateTime()->format('U') * 1000),
145
        ]);
146
    }
147
148
    /**
149
     * @api {delete} /api/v2/office/session/:id Delete session
150
     * @apiName delete
151
     * @apiVersion 2.0.0
152
     * @apiGroup App\Office
153
     * @apiPermission none
154
     * @apiDescription Delete a running session. If more members are active in the requested session than only the membership gets removed.
155
     * The session gets completely removed if only one member exists.
156
     * @apiParam (GET Parameter) {string} session_id The session id to delete
157
     * @apiParam (GET Parameter) {string} access_token Access token
158
     *
159
     * @apiExample (cURL) example:
160
     * curl -XDELETE "https://SERVER/api/v2/office/session?session_id=58a18a4ca271f962af6fdbc4&access_token=97223329239823bj223232323"
161
     *
162
     * @apiSuccessExample {json} Success-Response:
163
     * HTTP/1.1 204 OK
164
     *
165
     * @param string $id
166
     * @param string $access_token
167
     *
168
     * @return Response
169
     */
170
    public function delete(string $id, string $access_token): Response
171
    {
172
        $session = WopiSession::getByAccessToken($this->server, $this->parseId($id), $access_token);
173
        $session->leave($this->fs->getUser())
174
                ->store();
175
176
        return (new Response())->setCode(204);
177
    }
178
179
    /**
180
     * Parse id.
181
     *
182
     * @param string $id
183
     *
184
     * @return ObjectId
185
     */
186
    protected function parseId(string $id): ObjectId
187
    {
188
        try {
189
            return new ObjectId($id);
190
        } catch (\Exception $e) {
191
            throw new Exception\InvalidArgument('bad session id given');
192
        }
193
    }
194
}
195