Completed
Pull Request — master (#444)
by Raffael
05:17
created

Factory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 12
cbo 6
dl 0
loc 149
rs 10
c 0
b 0
f 0
lcom 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A prepareQuery() 0 14 2
A getOne() 0 13 2
A deleteOne() 0 8 1
A update() 0 35 3
A add() 0 29 2
A build() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Session;
13
14
use Balloon\Filesystem\Node\Collection;
15
use Balloon\Server\User;
16
use Balloon\Session;
17
use MongoDB\BSON\ObjectIdInterface;
18
use MongoDB\BSON\UTCDateTime;
19
use MongoDB\Database;
20
21
class Factory
22
{
23
    /**
24
     * Collection name.
25
     */
26
    public const COLLECTION_NAME = 'sessions';
27
28
    /**
29
     * Database.
30
     *
31
     * @var Database
32
     */
33
    protected $db;
34
35
    /**
36
     * Initialize.
37
     */
38
    public function __construct(Database $db)
39
    {
40
        $this->db = $db;
41
    }
42
43
    /**
44
     * Prepare query.
45
     */
46
    public function prepareQuery(User $user, ?array $query = null): array
47
    {
48
        $filter = [
49
            'owner' => $user->getId(),
50
        ];
51
52
        if (!empty($query)) {
53
            $filter = [
54
                '$and' => [$filter, $query],
55
            ];
56
        }
57
58
        return $filter;
59
    }
60
61
    /**
62
     * Get session.
63
     */
64
    public function getOne(User $user, ObjectIdInterface $id): SessionInterface
65
    {
66
        $result = $this->db->{self::COLLECTION_NAME}->findOne([
67
            'owner' => $user->getId(),
68
            '_id' => $id,
69
        ]);
70
71
        if ($result === null) {
72
            throw new Exception\NotFound('session '.$id.' is not registered');
73
        }
74
75
        return $this->build($result);
76
    }
77
78
    /**
79
     * Delete by name.
80
     */
81
    public function deleteOne(ObjectIdInterface $id): bool
82
    {
83
        $this->db->{self::COLLECTION_NAME}->deleteOne([
84
            '_id' => $id,
85
        ]);
86
87
        return true;
88
    }
89
90
    /**
91
     * Update.
92
     */
93
    public function update(User $user, SessionInterface $resource, Collection $parent, $stream): bool
94
    {
95
        if ($resource->isFinalized()) {
96
            throw new Exception\Closed('This session has been finalized and can not be updated.');
97
        }
98
99
        $ctx = $resource->getHashContext();
100
        $size = md5_update_stream($ctx, $stream);
101
        rewind($stream);
102
103
        $storage = $parent->getStorage();
104
        $session = $storage->storeTemporaryFile($stream, $user, $resource->getId());
0 ignored issues
show
Unused Code introduced by
$session is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
        $size = $resource->getSize() + $size;
106
107
        $data = [
108
            '$set' => [
109
                'size' => $size,
110
                'changed' => new UTCDateTime(),
111
            ],
112
        ];
113
114
        if ($size % 64 !== 0) {
115
            $data['$set']['hash'] = md5_final($ctx);
116
        } else {
117
            $data['$set']['context'] = serialize($ctx);
118
        }
119
120
        $resource->set(array_merge(['size' => $size], $data['$set']));
121
        $this->db->{self::COLLECTION_NAME}->updateOne([
122
            '_id' => $resource->getId(),
123
            'owner' => $user->getId(),
124
        ], $data);
125
126
        return true;
127
    }
128
129
    /**
130
     * Add session.
131
     */
132
    public function add(User $user, Collection $parent, $stream): SessionInterface
133
    {
134
        $ctx = md5_init();
135
        $size = md5_update_stream($ctx, $stream);
136
        rewind($stream);
137
138
        $storage = $parent->getStorage();
139
        $session = $storage->storeTemporaryFile($stream, $user);
140
141
        $resource = [
142
            '_id' => $session,
143
            'kind' => 'Session',
144
            'created' => new UTCDateTime(),
145
            'changed' => new UTCDateTime(),
146
            'parent' => $parent->getId(),
147
            'size' => $size,
148
            'owner' => $user->getId(),
149
        ];
150
151
        if ($size % 64 !== 0) {
152
            $resource['hash'] = md5_final($ctx);
153
        } else {
154
            $resource['context'] = serialize($ctx);
155
        }
156
157
        $this->db->{self::COLLECTION_NAME}->insertOne($resource);
158
159
        return $this->build($resource);
160
    }
161
162
    /**
163
     * Build instance.
164
     */
165
    public function build(array $resource): SessionInterface
166
    {
167
        return new Session($resource);
168
    }
169
}
170