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

Documents::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 5
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 as OfficeDoc;
17
use Balloon\App\Office\Template;
18
use Balloon\Filesystem;
19
use Balloon\Filesystem\Node\AttributeDecorator;
20
use Balloon\Filesystem\Node\Collection;
21
use Balloon\Filesystem\Node\File;
22
use Balloon\Server;
23
use Balloon\Server\User;
24
use Micro\Http\Response;
25
26
class Documents 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
     * Decorator.
51
     *
52
     * @var AttributeDecorator
53
     */
54
    protected $decorator;
55
56
    /**
57
     * Constructor.
58
     *
59
     * @param App                $app
60
     * @param Server             $server
61
     * @param AttributeDecorator $decorator
62
     */
63
    public function __construct(App $app, Server $server, AttributeDecorator $decorator)
64
    {
65
        $this->server = $server;
66
        $this->fs = $server->getFilesystem();
67
        $this->app = $app;
68
        $this->decorator = $decorator;
69
    }
70
71
    /**
72
     * @api {get} /api/v2/office/documents/:id Get document
73
     * @apiName get
74
     * @apiVersion 2.0.0
75
     * @apiUse _getNode
76
     * @apiGroup App\Office
77
     * @apiPermission none
78
     * @apiDescription Retreive office document
79
     *
80
     * @apiExample (cURL) example:
81
     * curl -XGET "https://SERVER/api/v2/office/documents/544627ed3c58891f058b4611"
82
     * curl -XGET "https://SERVER/api/v2/office/documents?id=544627ed3c58891f058b4611"
83
     *
84
     * @apiSuccessExample {json} Success-Response:
85
     * HTTP/1.1 200 OK
86
     * {
87
     *      "loleaflet": "https:\/\/officeserver:9980\/loleaflet\/dist\/loleaflet.html",
88
     *      "sessions": []
89
     * }
90
     *
91
     * @param string $id
92
     * @param string $p
93
     *
94
     * @return Response
95
     */
96
    public function get(?string $id = null, ?string $p = null): Response
97
    {
98
        $node = $this->fs->getNode($id, $p, File::class);
99
        $document = new OfficeDoc($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...
100
        $sessions = [];
101
102
        foreach ($document->getSessions() as $session) {
103
            $sessions[] = [
104
                'id' => (string) $session['_id'],
105
                'created' => $session['_id']->getTimestamp(),
106
                'user' => [
107
                    'id' => (string) $session['user'],
108
                    'name' => $this->server->getUserById($session['user'])->getUsername(),
109
                ],
110
            ];
111
        }
112
113
        $result = [
114
            'loleaflet' => $this->app->getLoleaflet(),
115
            'session' => $sessions,
116
        ];
117
118
        return (new Response())->setCode(200)->setBody($result);
119
    }
120
121
    /**
122
     * @api {put} /api/v2/office/documents Create new empty document
123
     * @apiName put
124
     * @apiVersion 2.0.0
125
     * @apiGroup App\Office
126
     * @apiPermission none
127
     * @apuUse _conflictNode
128
     * @apiDescription Create new document from an existing office document template, option type has to be one of the follwing:
129
     *  - xlsx  => "Office Open XML Spreadsheet",
130
     *  - xls   => "Microsoft Excel 97-2003",
131
     *  - xlt   => "Microsoft Excel 97-2003 Template",
132
     *  - csv   => "Text CSV",
133
     *  - ods   => "ODF Spreadsheet",
134
     *  - ots   => "ODF Spreadsheet Template",
135
     *  - docx  => "Office Open XML Text",
136
     *  - doc   => "Microsoft Word 97-2003",
137
     *  - dot   => "Microsoft Word 97-2003 Template",
138
     *  - odt   => "ODF Textdocument",
139
     *  - ott   => "ODF Textdocument Template",
140
     *  - pptx  => "Office Open XML Presentation",
141
     *  - ppt   => "Microsoft Powerpoint 97-2003",
142
     *  - potm  => "Microsoft Powerpoint 97-2003 Template",
143
     *  - odp   => "ODF Presentation",
144
     *  - otp   => "ODF Presentation Template"
145
     *
146
     * @apiParam (GET Parameter) {string} name The name of the new document
147
     * @apiParam (GET Parameter) {string} [collection] Parent collection id (If none  given, the document will be placed under root)
148
     * @apiParam (GET Parameter) {string} type Office document file type
149
     * @apiParam (GET Parameter) {string[]} attributes Node attributes
150
     *
151
     * @apiExample (cURL) example:
152
     * curl -XPUT "https://SERVER/api/v2/office/documents?type=xlsx"
153
     *
154
     * @apiSuccessExample {json} Success-Response:
155
     * HTTP/1.1 201 Created
156
     * {
157
     *      "id": "544627ed3c58891f058b4611"
158
     * }
159
     *
160
     * @param string $name
161
     * @param string $type
162
     * @param string $collection
163
     * @param array  $attributes
164
     * @param int    $conflict
165
     *
166
     * @return Response
167
     */
168
    public function put(string $name, string $type, ?string $collection = null, array $attributes = [], int $conflict = 0): Response
0 ignored issues
show
Unused Code introduced by
The parameter $conflict is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
169
    {
170
        $parent = $this->fs->getNode($collection, null, Collection::class, false, true);
171
        $tpl = new Template($type);
172
        $result = $parent->addFile($name, $tpl->get(), $attributes);
173
        $result = $this->decorator->decorate($result);
174
175
        return (new Response())->setCode(201)->setBody($result);
176
    }
177
}
178