Passed
Push — master ( a9f547...49cca7 )
by Raffael
04:18
created

Sharelink::deleteShareLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 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\Sharelink;
13
14
use Balloon\Filesystem;
15
use Balloon\Filesystem\Node\NodeInterface;
16
use Balloon\Server;
17
18
class Sharelink
19
{
20
    /**
21
     * Filesystem.
22
     *
23
     * @var Filesystem
24
     */
25
    protected $fs;
26
27
    /**
28
     * Constructor.
29
     */
30
    public function __construct(Server $server)
31
    {
32
        $this->fs = $server->getFilesystem();
33
    }
34
35
    /**
36
     * Share link.
37
     */
38
    public function shareLink(NodeInterface $node, ?string $expiration = null, ?string $password = null): bool
39
    {
40
        $set = $node->getAppAttributes(__NAMESPACE__);
41
42
        if (!isset($set['token'])) {
43
            $set['token'] = bin2hex(random_bytes(16));
44
        }
45
46
        if ($expiration !== null) {
47
            if (empty($expiration) && isset($set['expiration'])) {
48
                unset($set['expiration']);
49
            } elseif (!empty($expiration)) {
50
                $set['expiration'] = (int) $expiration;
51
            }
52
        }
53
54
        if ($password !== null) {
55
            if (empty($password) && isset($set['password'])) {
56
                unset($set['password']);
57
            } elseif (!empty($password)) {
58
                $set['password'] = hash('sha256', $password);
59
            }
60
        }
61
62
        $node->setAppAttributes(__NAMESPACE__, $set);
63
64
        return true;
65
    }
66
67
    /**
68
     * Delete sharelink.
69
     */
70
    public function deleteShareLink(NodeInterface $node): bool
71
    {
72
        $node->unsetAppAttributes(__NAMESPACE__);
73
74
        return true;
75
    }
76
77
    /**
78
     * Get share options.
79
     */
80
    public function getShareLink(NodeInterface $node): array
81
    {
82
        return $node->getAppAttributes(__NAMESPACE__);
83
    }
84
85
    /**
86
     * Get attributes.
87
     */
88
    public function getAttributes(NodeInterface $node, array $attributes = []): array
0 ignored issues
show
Unused Code introduced by
The parameter $attributes 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...
89
    {
90
        return ['shared' => $this->isShareLink($node)];
91
    }
92
93
    /**
94
     * Check if the node is a shared link.
95
     */
96
    public function isShareLink(NodeInterface $node): bool
97
    {
98
        return 0 !== count($node->getAppAttributes(__NAMESPACE__));
99
    }
100
101
    /**
102
     * Get node by access token.
103
     */
104
    public function findNodeWithShareToken(string $token): NodeInterface
105
    {
106
        $node = $this->fs->findNodeByFilter([
107
            'app.'.__NAMESPACE__.'.token' => $token,
108
            'deleted' => false,
109
        ]);
110
111
        $attributes = $node->getAppAttributes(__NAMESPACE__);
112
113
        if ($attributes['token'] !== $token) {
114
            throw new Exception\TokenInvalid('token given is invalid');
115
        }
116
117
        if (isset($attributes['expiration']) && !empty($attributes['expiration'])) {
118
            $time = (int) $attributes['expiration'];
119
            if ($time < time()) {
120
                throw new Exception\LinkExpired('link is expired');
121
            }
122
        }
123
124
        return $node;
125
    }
126
}
127