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

Document::postContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
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\Exception;
18
use Balloon\Server;
19
use Micro\Http\Response;
20
use MongoDB\BSON\ObjectId;
21
use Psr\Log\LoggerInterface;
22
23 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...
24
{
25
    /**
26
     * Server.
27
     *
28
     * @var Server
29
     */
30
    protected $server;
31
32
    /**
33
     * Logger.
34
     *
35
     * @var LoggerInterface
36
     */
37
    protected $logger;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param Server          $server
43
     * @param LoggerInterface $logger
44
     */
45
    public function __construct(Server $server, LoggerInterface $logger)
46
    {
47
        $this->server = $server;
48
        $this->logger = $logger;
49
    }
50
51
    /**
52
     * @api {get} /api/v2/office/wopi/document Get document sesssion information
53
     * @apiName get
54
     * @apiVersion 2.0.0
55
     * @apiGroup App\Office
56
     * @apiPermission none
57
     * @apiDescription Get document session information including document owner, session user and document size
58
     *
59
     * @apiParam (GET Parameter) {string} id The document id
60
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
61
     *
62
     * @apiExample (cURL) example:
63
     * curl -XGET "https://SERVER/api/v2/office/wopi/document/58a18a4ca271f962af6fdbc4?access_token=aae366363ee743412abb"
64
     *
65
     * @apiSuccessExample {json} Success-Response:
66
     * HTTP/1.1 200 OK
67
     * {
68
     *      [***]
69
     * }
70
     *
71
     * @param string $id
72
     * @param string $access_token
73
     *
74
     * @return Response
75
     */
76
    public function get(string $id, string $access_token): Response
77
    {
78
        $session = Member::getByAccessToken($this->server, $this->logger, $this->parseId($id), $access_token);
79
80
        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...
81
    }
82
83
    /**
84
     * @api {post} /api/v2/office/wopi/document/contents Save document contents
85
     * @apiName postContents
86
     * @apiVersion 2.0.0
87
     * @apiGroup App\Office
88
     * @apiPermission none
89
     * @apiDescription Save document contents
90
     *
91
     * @apiParam (GET Parameter) {string} id The document id
92
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
93
     *
94
     * @apiExample (cURL) example:
95
     * curl -XPOST "https://SERVER/api/v2/office/wopi/document/58a18a4ca271f962af6fdbaa/contents?access_token=aae366363ee743412abb"
96
     *
97
     * @apiSuccessExample {json} Success-Response:
98
     * HTTP/1.1 200 OK
99
     * {
100
     *      "status": 200,
101
     *      "data": true
102
     * }
103
     *
104
     * @param string $id
105
     * @param string $access_token
106
     *
107
     * @return Response
108
     */
109
    public function postContents(string $id, string $access_token): Response
110
    {
111
        $session = Session::getByAccessToken($this->server, $this->parseId($id), $access_token);
112
        $node = $session->getDocument()->getNode();
113
        ini_set('auto_detect_line_endings', '1');
114
        $content = fopen('php://input', 'rb');
115
        $result = $node->put($content, false);
116
117
        return (new Response())->setCode(200)->setBody($result);
118
    }
119
120
    /**
121
     * @api {get} /api/v2/office/wopi/document/contents Get document contents
122
     * @apiName getContents
123
     * @apiVersion 2.0.0
124
     * @apiGroup App\Office
125
     * @apiPermission none
126
     * @apiDescription Get document contents
127
     *
128
     * @apiParam (GET Parameter) {string} id The document id
129
     * @apiParam (GET Parameter) {string} access_token An access token to access the document
130
     *
131
     * @apiExample (cURL) Exampl:
132
     * curl -XGET "https://SERVER/api/v2/office/document/58a18a4ca271f962af6fdbaa/contents?access_token=aae366363ee743412abb"
133
     *
134
     * @apiSuccessExample {binary} Success-Response:
135
     * HTTP/1.1 200 OK
136
     *
137
     * @param string $id
138
     * @param string $access_token
139
     */
140
    public function getContents(string $id, string $access_token): void
141
    {
142
        $session = Session::getByAccessToken($this->server, $this->parseId($id), $access_token);
143
        $stream = $session->getDocument()->get();
144
145
        while (!feof($stream)) {
146
            echo fread($stream, 8192);
147
        }
148
149
        exit();
150
    }
151
152
    /**
153
     * Get by access token.
154
     *
155
     * @param string $id
156
     * @param string $access_token
0 ignored issues
show
Bug introduced by
There is no parameter named $access_token. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
157
     *
158
     * @return Session
159
     */
160
    protected function parseId(string $id): ObjectId
161
    {
162
        try {
163
            return new ObjectId($id);
164
        } catch (\Exception $e) {
165
            throw new Exception\InvalidArgument('bad session id given');
166
        }
167
    }
168
}
169