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

Document::parseId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
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\Wopi;
13
14
use Balloon\App\Api\Controller;
15
use Balloon\App\Office\Session;
16
use Balloon\App\Office\Session\Member;
17
use Balloon\Server;
18
use Micro\Http\Response;
19
use MongoDB\BSON\ObjectId;
20
use Psr\Log\LoggerInterface;
21
22 View Code Duplication
class Document extends Controller
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    /**
25
     * Server.
26
     *
27
     * @var Server
28
     */
29
    protected $server;
30
31
    /**
32
     * Logger.
33
     *
34
     * @var LoggerInterface
35
     */
36
    protected $logger;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param Server          $server
42
     * @param LoggerInterface $logger
43
     */
44
    public function __construct(Server $server, LoggerInterface $logger)
45
    {
46
        $this->server = $server;
47
        $this->logger = $logger;
48
    }
49
50
    /**
51
     * @api {get} /api/v2/office/wopi/document Get document sesssion information
52
     * @apiName get
53
     * @apiVersion 2.0.0
54
     * @apiGroup App\Office
55
     * @apiPermission none
56
     * @apiDescription Get document session information including document owner, session user and document size
57
     *
58
     * @apiParam (GET Parameter) {string} id The document id
59
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
60
     *
61
     * @apiExample (cURL) example:
62
     * curl -XGET "https://SERVER/api/v2/office/wopi/document/58a18a4ca271f962af6fdbc4?access_token=aae366363ee743412abb"
63
     *
64
     * @apiSuccessExample {json} Success-Response:
65
     * HTTP/1.1 200 OK
66
     * {
67
     *      [***]
68
     * }
69
     *
70
     * @param ObjectId $id
71
     * @param string   $access_token
72
     *
73
     * @return Response
74
     */
75
    public function get(ObjectId $id, string $access_token): Response
76
    {
77
        $session = Member::getByAccessToken($this->server, $this->logger, $id, $access_token);
78
79
        return (new Response())->setCode(200)->setBody($session->getAttributes(), true);
0 ignored issues
show
Unused Code introduced by
The call to Response::setBody() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
80
    }
81
82
    /**
83
     * @api {post} /api/v2/office/wopi/document/contents Save document contents
84
     * @apiName postContents
85
     * @apiVersion 2.0.0
86
     * @apiGroup App\Office
87
     * @apiPermission none
88
     * @apiDescription Save document contents
89
     *
90
     * @apiParam (GET Parameter) {string} id The document id
91
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
92
     *
93
     * @apiExample (cURL) example:
94
     * curl -XPOST "https://SERVER/api/v2/office/wopi/document/58a18a4ca271f962af6fdbaa/contents?access_token=aae366363ee743412abb"
95
     *
96
     * @apiSuccessExample {json} Success-Response:
97
     * HTTP/1.1 200 OK
98
     * {
99
     *      "status": 200,
100
     *      "data": true
101
     * }
102
     *
103
     * @param ObjectId $id
104
     * @param string   $access_token
105
     *
106
     * @return Response
107
     */
108
    public function postContents(ObjectId $id, string $access_token): Response
109
    {
110
        $session = Session::getByAccessToken($this->server, $id, $access_token);
111
        $node = $session->getDocument()->getNode();
112
        ini_set('auto_detect_line_endings', '1');
113
        $content = fopen('php://input', 'rb');
114
        $result = $node->put($content, false);
115
116
        return (new Response())->setCode(200)->setBody($result);
117
    }
118
119
    /**
120
     * @api {get} /api/v2/office/wopi/document/contents Get document contents
121
     * @apiName getContents
122
     * @apiVersion 2.0.0
123
     * @apiGroup App\Office
124
     * @apiPermission none
125
     * @apiDescription Get document contents
126
     *
127
     * @apiParam (GET Parameter) {string} id The document id
128
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
129
     *
130
     * @apiExample (cURL) Exampl:
131
     * curl -XGET "https://SERVER/api/v2/office/document/58a18a4ca271f962af6fdbaa/contents?access_token=aae366363ee743412abb"
132
     *
133
     * @apiSuccessExample {binary} Success-Response:
134
     * HTTP/1.1 200 OK
135
     *
136
     * @param ObjectId $id
137
     * @param string   $access_token
138
     */
139
    public function getContents(ObjectId $id, string $access_token): void
140
    {
141
        $session = Session::getByAccessToken($this->server, $id, $access_token);
142
        $stream = $session->getDocument()->get();
143
144
        while (!feof($stream)) {
145
            echo fread($stream, 8192);
146
        }
147
148
        exit();
149
    }
150
}
151