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

ShareLink   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
dl 0
loc 139
rs 10
c 0
b 0
f 0
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A post() 0 9 1
A delete() 0 9 1
A get() 0 7 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\Sharelink\Api\v1;
13
14
use Balloon\App\Api\Controller;
15
use Balloon\App\Sharelink\Sharelink as Share;
16
use Balloon\Filesystem;
17
use Balloon\Server;
18
use Micro\Http\Response;
19
20
class ShareLink extends Controller
21
{
22
    /**
23
     * Sharelink.
24
     *
25
     * @var Share
26
     */
27
    protected $sharelink;
28
29
    /**
30
     * Filesystem.
31
     *
32
     * @var Filesystem
33
     */
34
    protected $fs;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param Share  $sharelink
40
     * @param Server $server
41
     */
42
    public function __construct(Share $sharelink, Server $server)
43
    {
44
        $this->fs = $server->getFilesystem();
45
        $this->sharelink = $sharelink;
46
    }
47
48
    /**
49
     * @api {post} /api/v1/node/share-link?id=:id Create sharing link
50
     * @apiVersion 1.0.0
51
     * @apiName postShareLink
52
     * @apiGroup Node
53
     * @apiPermission none
54
     * @apiDescription Create a unique sharing link of a node (global accessible):
55
     * a possible existing link will be deleted if this method will be called.
56
     * @apiUse _getNode
57
     * @apiUse _writeAction
58
     *
59
     * @apiParam (POST Parameter) {object} [options] Sharing options
60
     * @apiParam (POST Parameter) {number} [options.expiration] Expiration unix timestamp of the sharing link
61
     * @apiParam (POST Parameter) {string} [options.password] Protected shared link with password
62
     *
63
     * @apiExample (cURL) example:
64
     * curl -XPOST "https://SERVER/api/v1/node/share-link?id=544627ed3c58891f058b4686&pretty"
65
     * curl -XPOST "https://SERVER/api/v1/node/544627ed3c58891f058b4686/share-link?pretty"
66
     * curl -XPOST "https://SERVER/api/v1/node/share-link?p=/absolute/path/to/my/node&pretty"
67
     *
68
     * @apiSuccessExample {json} Success-Response (Created or modified share link):
69
     * HTTP/1.1 204 No Content
70
     *
71
     * @param string $id
72
     * @param string $p
73
     * @param array  $options
74
     *
75
     * @return Response
76
     */
77
    public function post(?string $id = null, ?string $p = null, array $options = []): Response
78
    {
79
        $node = $this->fs->getNode($id, $p);
80
        $options['shared'] = true;
81
82
        $this->sharelink->shareLink($node, $options);
83
84
        return (new Response())->setCode(204);
85
    }
86
87
    /**
88
     * @api {delete} /api/v1/node/share-link?id=:id Delete sharing link
89
     * @apiVersion 1.0.0
90
     * @apiName deleteShareLink
91
     * @apiGroup Node
92
     * @apiPermission none
93
     * @apiDescription Delete an existing sharing link
94
     * @apiUse _getNode
95
     * @apiUse _writeAction
96
     *
97
     * @apiExample (cURL) example:
98
     * curl -XDELETE "https://SERVER/api/v1/node/share-link?id=544627ed3c58891f058b4686?pretty"
99
     *
100
     * @apiSuccessExample {json} Success-Response:
101
     * HTTP/1.1 204 No Content
102
     *
103
     * @param string $id
104
     * @param string $p
105
     *
106
     * @return Response
107
     */
108
    public function delete(?string $id = null, ?string $p = null): Response
109
    {
110
        $node = $this->fs->getNode($id, $p);
111
        $options = ['shared' => false];
112
113
        $this->sharelink->shareLink($node, $options);
114
115
        return (new Response())->setCode(204);
116
    }
117
118
    /**
119
     * @api {get} /api/v1/node/share-link?id=:id Get sharing link
120
     * @apiVersion 1.0.0
121
     * @apiName getShareLink
122
     * @apiGroup Node
123
     * @apiPermission none
124
     * @apiDescription Get an existing sharing link
125
     * @apiUse _getNode
126
     *
127
     * @apiExample (cURL) example:
128
     * curl -XGET "https://SERVER/api/v1/node/share-link?id=544627ed3c58891f058b4686&pretty"
129
     * curl -XGET "https://SERVER/api/v1/node/544627ed3c58891f058b4686/share-link?pretty"
130
     * curl -XGET "https://SERVER/api/v1/node/share-link?p=/path/to/my/node&pretty"
131
     *
132
     * @apiSuccess (200 OK) {number} status Status Code
133
     * @apiSuccess (200 OK) {object} data Share options
134
     * @apiSuccess (200 OK) {string} data.token Shared unique node token
135
     * @apiSuccess (200 OK) {string} [data.password] Share link is password protected
136
     * @apiSuccess (200 OK) {string} [data.expiration] Unix timestamp
137
     * @apiSuccessExample {json} Success-Response:
138
     * HTTP/1.1 200 OK
139
     * {
140
     *     "status": 200,
141
     *     "data": {
142
     *        "token": "544627ed3c51111f058b468654db6b7daca8e5.69846614",
143
     *     }
144
     * }
145
     *
146
     * @param string $id
147
     * @param string $p
148
     *
149
     * @return Response
150
     */
151
    public function get(?string $id = null, ?string $p = null): Response
152
    {
153
        $node = $this->fs->getNode($id, $p);
154
        $result = $this->sharelink->getShareLink($node);
155
156
        return (new Response())->setCode(200)->setBody($result);
157
    }
158
}
159