Completed
Branch dev (276354)
by Raffael
15:43
created

Sessions::parseId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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\Filesystem;
20
use Balloon\Filesystem\Node\File;
21
use Balloon\Server;
22
use Micro\Http\Response;
23
use MongoDB\BSON\ObjectId;
24
25
class Sessions extends Controller
26
{
27
    /**
28
     * App.
29
     *
30
     * @var App
31
     */
32
    protected $app;
33
34
    /**
35
     * Filesystem.
36
     *
37
     * @var Filesystem
38
     */
39
    protected $fs;
40
41
    /**
42
     * Server.
43
     *
44
     * @var Server
45
     */
46
    protected $server;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param App    $app
52
     * @param Server $server
53
     */
54
    public function __construct(App $app, Server $server)
55
    {
56
        $this->server = $server;
57
        $this->fs = $server->getFilesystem();
58
        $this->app = $app;
59
    }
60
61
    /**
62
     * @api {post} /api/v2/office/sessions Create session
63
     * @apiName post
64
     * @apiVersion 2.0.0
65
     * @apiGroup App\Office
66
     * @apiPermission none
67
     * @apiUse _getNode
68
     * @apiDescription Create new session for a document
69
     *
70
     * @apiExample (cURL) example:
71
     * curl -XPOST "https://SERVER/api/v2/office/session?id=58a18a4ca271f962af6fdbc4"
72
     *
73
     * @apiSuccessExample {json} Success-Response:
74
     * HTTP/1.1 201 Created
75
     * {
76
     *      "id": "544627ed3c58891f058bbbaa",
77
     *      "wopi_url": "https://localhost",
78
     *      "access_token": "544627ed3c58891f058b4622",
79
     *      "access_token_ttl": "1486989000"
80
     * }
81
     *
82
     * @param string $id
83
     * @param string $p
84
     *
85
     * @return Response
86
     */
87
    public function post(?string $id = null, ?string $p = null): Response
88
    {
89
        $node = $this->fs->getNode($id, $p, File::class);
90
        $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...
91
        $ttl = $this->app->getTokenTtl();
92
93
        $session = new WopiSession($this->fs, $document, $ttl);
94
        $member = new Member($this->fs->getUser(), $ttl);
95
        $session->join($member)
96
                ->store();
97
98
        return (new Response())->setCode(201)->setBody([
99
            'id' => (string) $session->getId(),
100
            'wopi_url' => $this->app->getWopiUrl(),
101
            'access_token' => $member->getAccessToken(),
102
            'access_token_ttl' => ($member->getTTL()->toDateTime()->format('U') * 1000),
103
        ]);
104
    }
105
106
    /**
107
     * @api {post} /api/v2/office/sessions/:id/join Join session
108
     * @apiName postJoin
109
     * @apiVersion 2.0.0
110
     * @apiGroup App\Office
111
     * @apiPermission none
112
     * @apiDescription Join running session
113
     * @apiParam (GET Parameter) {string} session_id The session id to join to
114
     *
115
     * @apiExample (cURL) example:
116
     * curl -XPOST "https://SERVER/api/v2/office/session/join?session_id=58a18a4ca271f962af6fdbc4"
117
     *
118
     * @apiSuccessExample {json} Success-Response:
119
     * HTTP/1.1 200 OK
120
     * {
121
     *      "id": "544627ed3c58891f058bbbaa",
122
     *      "wopi_url": "https://localhost",
123
     *      "access_token": "544627ed3c58891f058b4622",
124
     *      "access_token_ttl": "1486989000"
125
     * }
126
     *
127
     * @param ObjectId $id
128
     *
129
     * @return Response
130
     */
131
    public function postJoin(ObjectId $id): Response
132
    {
133
        $session = WopiSession::getSessionById($this->fs, $id);
134
        $ttl = $this->app->getTokenTtl();
135
        $member = new Member($this->fs->getUser(), $ttl);
136
        $session->join($member)
137
                ->store();
138
139
        return (new Response())->setCode(200)->setBody([
140
            'id' => (string) $session->getId(),
141
            'wopi_url' => $this->app->getWopiUrl(),
142
            'access_token' => $member->getAccessToken(),
143
            'access_token_ttl' => ($member->getTTL()->toDateTime()->format('U') * 1000),
144
        ]);
145
    }
146
147
    /**
148
     * @api {delete} /api/v2/office/session/:id Delete session
149
     * @apiName delete
150
     * @apiVersion 2.0.0
151
     * @apiGroup App\Office
152
     * @apiPermission none
153
     * @apiDescription Delete a running session. If more members are active in the requested session than only the membership gets removed.
154
     * The session gets completely removed if only one member exists.
155
     * @apiParam (GET Parameter) {string} session_id The session id to delete
156
     * @apiParam (GET Parameter) {string} access_token Access token
157
     *
158
     * @apiExample (cURL) example:
159
     * curl -XDELETE "https://SERVER/api/v2/office/session?session_id=58a18a4ca271f962af6fdbc4&access_token=97223329239823bj223232323"
160
     *
161
     * @apiSuccessExample {json} Success-Response:
162
     * HTTP/1.1 204 OK
163
     *
164
     * @param ObjectId $id
165
     * @param string   $access_token
166
     *
167
     * @return Response
168
     */
169
    public function delete(ObjectId $id, string $access_token): Response
170
    {
171
        $session = WopiSession::getByAccessToken($this->server, $id, $access_token);
172
        $session->leave($this->fs->getUser())
173
                ->store();
174
175
        return (new Response())->setCode(204);
176
    }
177
}
178