Skip to content

Commit 3f7b7ae

Browse files
authored
PCBC-828 implement collection management (#89)
* Implement collection manager * Linting * fix arg info
1 parent 3fe7c8d commit 3f7b7ae

13 files changed

+1119
-10
lines changed

Couchbase/Bucket.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,11 @@ public function viewQuery(string $designDoc, string $viewName, ViewOptions $opti
137137
* Creates a new CollectionManager object for managing collections and scopes.
138138
*
139139
* @return CollectionManager
140-
* @throws UnsupportedOperationException
141140
* @since 4.0.0
142141
*/
143142
public function collections(): CollectionManager
144143
{
145-
throw new UnsupportedOperationException();
144+
return new CollectionManager($this->core, $this->name);
146145
}
147146

148147
/**

Couchbase/Management/CollectionManager.php

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,84 @@
2020

2121
namespace Couchbase\Management;
2222

23+
use Couchbase\Extension;
24+
2325
class CollectionManager
2426
{
25-
public function getScope(string $name): ScopeSpec
27+
/**
28+
* @var resource
29+
*/
30+
private $core;
31+
private string $bucketName;
32+
public function __construct($core, string $bucketName)
2633
{
34+
$this->core = $core;
35+
$this->bucketName = $bucketName;
2736
}
2837

29-
public function getAllScopes(): array
38+
/**
39+
* Retrieves all scopes within the bucket
40+
*
41+
* @param GetAllScopesOptions|null $options The options to use when retrieving the scopes
42+
*
43+
* @see ScopeSpec
44+
* @return array array of scopes within the bucket
45+
*/
46+
public function getAllScopes(GetAllScopesOptions $options = null): array
3047
{
48+
$result = Extension\scopeGetAll($this->core, $this->bucketName, GetAllScopesOptions::export($options));
49+
$scopes = [];
50+
foreach ($result['scopes'] as $scope) {
51+
$scopes[] = ScopeSpec::import($scope);
52+
}
53+
return $scopes;
3154
}
3255

33-
public function createScope(string $name)
56+
/**
57+
* Create a new scope
58+
*
59+
* @param string $name name of the scope
60+
* @param CreateScopeOptions|null $options the options to use when creating a scope
61+
* @since 4.1.3
62+
*/
63+
public function createScope(string $name, CreateScopeOptions $options = null)
3464
{
65+
Extension\scopeCreate($this->core, $this->bucketName, $name, CreateScopeOptions::export($options));
3566
}
3667

37-
public function dropScope(string $name)
68+
/**
69+
* Drops an existing scope
70+
*
71+
* @param string $name of the scope to drop
72+
* @param DropScopeOptions|null $options the options to use when dropping a scope
73+
* @since 4.1.3
74+
*/
75+
public function dropScope(string $name, DropScopeOptions $options = null)
3876
{
77+
Extension\scopeDrop($this->core, $this->bucketName, $name, DropScopeOptions::export($options));
3978
}
4079

41-
public function createCollection(CollectionSpec $collection)
80+
/**
81+
* Creates a new collection
82+
*
83+
* @param CollectionSpec $collection The spec of the collection
84+
* @param CreateCollectionOptions|null $options The options to use when creating a collection
85+
* @since 4.1.3
86+
*/
87+
public function createCollection(CollectionSpec $collection, CreateCollectionOptions $options = null)
4288
{
89+
Extension\collectionCreate($this->core, $this->bucketName, CollectionSpec::export($collection), CreateCollectionOptions::export($options));
4390
}
4491

45-
public function dropCollection(CollectionSpec $collection)
92+
/**
93+
* Drops an existing collection
94+
*
95+
* @param CollectionSpec $collection The spec of the collection to drop
96+
* @param DropCollectionOptions|null $options The options to use when dropping a collection
97+
* @since 4.1.3
98+
*/
99+
public function dropCollection(CollectionSpec $collection, DropCollectionOptions $options = null)
46100
{
101+
Extension\collectionDrop($this->core, $this->bucketName, CollectionSpec::export($collection), DropCollectionOptions::export($options));
47102
}
48103
}

Couchbase/Management/CollectionSpec.php

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,133 @@
2222

2323
class CollectionSpec
2424
{
25+
private string $name;
26+
private string $scopeName;
27+
private ?int $maxExpiry;
28+
29+
/**
30+
* @param string $name
31+
* @param string $scopeName
32+
* @param int|null $maxExpiry
33+
* @since 4.1.3
34+
*/
35+
public function __construct(string $name, string $scopeName, int $maxExpiry = null)
36+
{
37+
$this->name = $name;
38+
$this->scopeName = $scopeName;
39+
$this->maxExpiry = $maxExpiry;
40+
}
41+
42+
/**
43+
* Static helper to keep code more readable
44+
*
45+
* @param string $name
46+
* @param string $scopeName
47+
* @param int|null $maxExpiry
48+
* @return CollectionSpec
49+
* @since 4.1.3
50+
*/
51+
public static function build(string $name, string $scopeName, int $maxExpiry = null): CollectionSpec
52+
{
53+
return new CollectionSpec($name, $scopeName, $maxExpiry);
54+
}
55+
56+
/**
57+
* Get the name of the collection
58+
*
59+
* @return string collection name
60+
* @since 4.1.3
61+
*/
2562
public function name(): string
2663
{
64+
return $this->name;
2765
}
2866

67+
/**
68+
* Get the name of the scope which the collection belongs to
69+
*
70+
* @return string scope name
71+
* @since 4.1.3
72+
*/
2973
public function scopeName(): string
3074
{
75+
return $this->scopeName;
76+
}
77+
78+
/**
79+
* Get the max expiry of the collection
80+
*
81+
* @return int|null
82+
* @since 4.1.3
83+
*/
84+
public function maxExpiry(): ?int
85+
{
86+
return $this->maxExpiry;
3187
}
3288

89+
/**
90+
* Set the name of the collection
91+
*
92+
* @param string $name collection name
93+
* @return CollectionSpec
94+
* @since 4.1.3
95+
*/
3396
public function setName(string $name): CollectionSpec
3497
{
98+
$this->name = $name;
99+
return $this;
100+
}
101+
102+
/**
103+
* Sets the name of the scope which the collection belongs to
104+
* @param string $scopeName scope name
105+
* @return CollectionSpec
106+
* @since 4.1.3
107+
*/
108+
public function setScopeName(string $scopeName): CollectionSpec
109+
{
110+
$this->scopeName = $scopeName;
111+
return $this;
112+
}
113+
114+
/**
115+
* Sets the max expiry of the collection
116+
*
117+
* @param int $seconds max expiry in seconds
118+
* @return CollectionSpec
119+
* @since 4.1.3
120+
*/
121+
public function setMaxExpiry(int $seconds): CollectionSpec
122+
{
123+
$this->maxExpiry = $seconds;
124+
return $this;
35125
}
36126

37-
public function setScopeName(string $name): CollectionSpec
127+
/**
128+
* @param CollectionSpec $spec
129+
* @return array
130+
* @since 4.1.3
131+
*/
132+
public static function export(CollectionSpec $spec): array
38133
{
134+
return [
135+
'name' => $spec->name,
136+
'scopeName' => $spec->scopeName,
137+
'maxExpiry' => $spec->maxExpiry
138+
];
39139
}
40140

41-
public function setMaxExpiry(int $ms): CollectionSpec
141+
/**
142+
* @param array $collection
143+
* @return CollectionSpec
144+
* @since 4.1.3
145+
*/
146+
public static function import(array $collection): CollectionSpec
42147
{
148+
$collectionSpec = new CollectionSpec($collection['name'], $collection['scopeName']);
149+
if (array_key_exists('maxExpiry', $collection)) {
150+
$collectionSpec->setMaxExpiry($collection['maxExpiry']);
151+
}
152+
return $collectionSpec;
43153
}
44154
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014-Present Couchbase, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
declare(strict_types=1);
20+
21+
namespace Couchbase\Management;
22+
23+
class CreateCollectionOptions
24+
{
25+
private ?int $timeoutMilliseconds;
26+
27+
/**
28+
* Static helper to keep code more readable
29+
*
30+
* @return CreateCollectionOptions
31+
* @since 4.1.3
32+
*/
33+
public static function build(): CreateCollectionOptions
34+
{
35+
return new CreateCollectionOptions();
36+
}
37+
38+
/**
39+
* Sets the operation timeout in milliseconds
40+
*
41+
* @param int $milliseconds the operation timeout to apply
42+
*
43+
* @return CreateCollectionOptions
44+
* @since 4.1.3
45+
*/
46+
public function timeout(int $milliseconds): CreateCollectionOptions
47+
{
48+
$this->timeoutMilliseconds = $milliseconds;
49+
return $this;
50+
}
51+
52+
/**
53+
* @param CreateCollectionOptions|null $options
54+
* @return array
55+
* @internal
56+
* @since 4.1.3
57+
*/
58+
59+
public static function export(?CreateCollectionOptions $options): array
60+
{
61+
if ($options == null) {
62+
return [];
63+
}
64+
return [
65+
'timeoutMilliseconds' => $options->timeoutMilliseconds,
66+
];
67+
}
68+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014-Present Couchbase, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
declare(strict_types=1);
20+
21+
namespace Couchbase\Management;
22+
23+
class CreateScopeOptions
24+
{
25+
private ?int $timeoutMilliseconds;
26+
27+
/**
28+
* Static helper to make code more readable
29+
*
30+
* @return CreateScopeOptions
31+
* @since 4.1.3
32+
*/
33+
public static function build(): CreateScopeOptions
34+
{
35+
return new CreateScopeOptions();
36+
}
37+
/**
38+
* Sets the operation timeout in milliseconds
39+
*
40+
* @param int $milliseconds the operation timeout to apply
41+
*
42+
* @return CreateScopeOptions
43+
* @since 4.1.3
44+
*/
45+
public function timeout(int $milliseconds): CreateScopeOptions
46+
{
47+
$this->timeoutMilliseconds = $milliseconds;
48+
return $this;
49+
}
50+
51+
/**
52+
* @param CreateScopeOptions|null $options
53+
* @return array
54+
* @internal
55+
* @since 4.1.3
56+
*/
57+
public static function export(?CreateScopeOptions $options): array
58+
{
59+
if ($options == null) {
60+
return [];
61+
}
62+
return [
63+
'timeoutMilliseconds' => $options->timeoutMilliseconds,
64+
];
65+
}
66+
}

0 commit comments

Comments
 (0)