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

Session::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\v1;
13
14
use Balloon\App\Api\Controller;
15
use Balloon\App\Office\Constructor\Http as App;
16
use Balloon\App\Office\Document;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Balloon\App\Office\Api\v1\Document.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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 Session 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/v1/office/session 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/v1/office/session?id=58a18a4ca271f962af6fdbc4"
72
     *
73
     * @apiSuccessExample {json} Success-Response:
74
     * HTTP/1.1 201 Created
75
     * {
76
     *      "code": 201,
77
     *      "data": {
78
     *          "id": "544627ed3c58891f058bbbaa",
79
     *          "wopi_url": "https://localhost",
80
     *          "access_token": "544627ed3c58891f058b4622",
81
     *          "access_token_ttl": "1486989000"
82
     *      }
83
     * }
84
     *
85
     * @param string $id
86
     * @param string $p
87
     *
88
     * @return Response
89
     */
90
    public function post(?string $id = null, ?string $p = null): Response
91
    {
92
        $node = $this->fs->getNode($id, $p, File::class);
93
        $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...
94
        $ttl = $this->app->getTokenTtl();
95
96
        $session = new WopiSession($this->fs, $document, $ttl);
97
        $member = new Member($this->fs->getUser(), $ttl);
98
        $session->join($member)
99
                ->store();
100
101
        return (new Response())->setCode(201)->setBody([
102
            'code' => 201,
103
            'data' => [
104
                'id' => (string) $session->getId(),
105
                'wopi_url' => $this->app->getWopiUrl(),
106
                'access_token' => $member->getAccessToken(),
107
                'access_token_ttl' => ($member->getTTL()->toDateTime()->format('U') * 1000),
108
            ],
109
        ]);
110
    }
111
112
    /**
113
     * @api {post} /api/v1/office/session/join?id=:id Join session
114
     * @apiName postJoin
115
     * @apiVersion 2.0.0
116
     * @apiGroup App\Office
117
     * @apiPermission none
118
     * @apiDescription Join running session
119
     * @apiParam (GET Parameter) {string} session_id The session id to join to
120
     *
121
     * @apiExample (cURL) example:
122
     * curl -XPOST "https://SERVER/api/v1/office/session/join?session_id=58a18a4ca271f962af6fdbc4"
123
     *
124
     * @apiSuccessExample {json} Success-Response:
125
     * HTTP/1.1 200 OK
126
     * {
127
     *      "code": 200,
128
     *      "data": {
129
     *          "wopi_url": "https://localhost",
130
     *          "access_token": "544627ed3c58891f058b4622",
131
     *          "access_token_ttl": "1486989000"
132
     *      }
133
     * }
134
     *
135
     * @param ObjectId $id
136
     *
137
     * @return Response
138
     */
139
    public function postJoin(ObjectId $id): Response
140
    {
141
        $session = WopiSession::getSessionById($this->fs, $id);
142
        $ttl = $this->app->getTokenTtl();
143
        $member = new Member($this->fs->getUser(), $ttl);
144
        $session->join($member)
145
                ->store();
146
147
        return (new Response())->setCode(200)->setBody([
148
            'code' => 200,
149
            'data' => [
150
                'wopi_url' => $this->app->getWopiUrl(),
151
                'access_token' => $member->getAccessToken(),
152
                'access_token_ttl' => ($member->getTTL()->toDateTime()->format('U') * 1000),
153
            ],
154
        ]);
155
    }
156
157
    /**
158
     * @api {delete} /api/v1/office/session?id=:id Delete session
159
     * @apiName delete
160
     * @apiVersion 2.0.0
161
     * @apiGroup App\Office
162
     * @apiPermission none
163
     * @apiDescription Delete a running session. If more members are active in the requested session than only the membership gets removed.
164
     * The session gets completely removed if only one member exists.
165
     * @apiParam (GET Parameter) {string} session_id The session id to delete
166
     * @apiParam (GET Parameter) {string} access_token Access token
167
     *
168
     * @apiExample (cURL) example:
169
     * curl -XDELETE "https://SERVER/api/v1/office/session?session_id=58a18a4ca271f962af6fdbc4&access_token=97223329239823bj223232323"
170
     *
171
     * @apiSuccessExample {json} Success-Response:
172
     * HTTP/1.1 204 OK
173
     *
174
     * @param ObjectId $id
175
     * @param string   $access_token
176
     *
177
     * @return Response
178
     */
179
    public function delete(ObjectId $id, string $access_token): Response
180
    {
181
        $session = WopiSession::getByAccessToken($this->server, $id, $access_token);
182
        $session->leave($this->fs->getUser())
183
                ->store();
184
185
        return (new Response())->setCode(204);
186
    }
187
}
188