Skip to content

Commit

Permalink
PCBC-828 implement collection management (#89)
Browse files Browse the repository at this point in the history
* Implement collection manager

* Linting

* fix arg info
  • Loading branch information
Matt-Woz authored Mar 29, 2023
1 parent 3fe7c8d commit 3f7b7ae
Show file tree
Hide file tree
Showing 13 changed files with 1,119 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Couchbase/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,11 @@ public function viewQuery(string $designDoc, string $viewName, ViewOptions $opti
* Creates a new CollectionManager object for managing collections and scopes.
*
* @return CollectionManager
* @throws UnsupportedOperationException
* @since 4.0.0
*/
public function collections(): CollectionManager
{
throw new UnsupportedOperationException();
return new CollectionManager($this->core, $this->name);
}

/**
Expand Down
67 changes: 61 additions & 6 deletions Couchbase/Management/CollectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,84 @@

namespace Couchbase\Management;

use Couchbase\Extension;

class CollectionManager
{
public function getScope(string $name): ScopeSpec
/**
* @var resource
*/
private $core;
private string $bucketName;
public function __construct($core, string $bucketName)
{
$this->core = $core;
$this->bucketName = $bucketName;
}

public function getAllScopes(): array
/**
* Retrieves all scopes within the bucket
*
* @param GetAllScopesOptions|null $options The options to use when retrieving the scopes
*
* @see ScopeSpec
* @return array array of scopes within the bucket
*/
public function getAllScopes(GetAllScopesOptions $options = null): array
{
$result = Extension\scopeGetAll($this->core, $this->bucketName, GetAllScopesOptions::export($options));
$scopes = [];
foreach ($result['scopes'] as $scope) {
$scopes[] = ScopeSpec::import($scope);
}
return $scopes;
}

public function createScope(string $name)
/**
* Create a new scope
*
* @param string $name name of the scope
* @param CreateScopeOptions|null $options the options to use when creating a scope
* @since 4.1.3
*/
public function createScope(string $name, CreateScopeOptions $options = null)
{
Extension\scopeCreate($this->core, $this->bucketName, $name, CreateScopeOptions::export($options));
}

public function dropScope(string $name)
/**
* Drops an existing scope
*
* @param string $name of the scope to drop
* @param DropScopeOptions|null $options the options to use when dropping a scope
* @since 4.1.3
*/
public function dropScope(string $name, DropScopeOptions $options = null)
{
Extension\scopeDrop($this->core, $this->bucketName, $name, DropScopeOptions::export($options));
}

public function createCollection(CollectionSpec $collection)
/**
* Creates a new collection
*
* @param CollectionSpec $collection The spec of the collection
* @param CreateCollectionOptions|null $options The options to use when creating a collection
* @since 4.1.3
*/
public function createCollection(CollectionSpec $collection, CreateCollectionOptions $options = null)
{
Extension\collectionCreate($this->core, $this->bucketName, CollectionSpec::export($collection), CreateCollectionOptions::export($options));
}

public function dropCollection(CollectionSpec $collection)
/**
* Drops an existing collection
*
* @param CollectionSpec $collection The spec of the collection to drop
* @param DropCollectionOptions|null $options The options to use when dropping a collection
* @since 4.1.3
*/
public function dropCollection(CollectionSpec $collection, DropCollectionOptions $options = null)
{
Extension\collectionDrop($this->core, $this->bucketName, CollectionSpec::export($collection), DropCollectionOptions::export($options));
}
}
114 changes: 112 additions & 2 deletions Couchbase/Management/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,133 @@

class CollectionSpec
{
private string $name;
private string $scopeName;
private ?int $maxExpiry;

/**
* @param string $name
* @param string $scopeName
* @param int|null $maxExpiry
* @since 4.1.3
*/
public function __construct(string $name, string $scopeName, int $maxExpiry = null)
{
$this->name = $name;
$this->scopeName = $scopeName;
$this->maxExpiry = $maxExpiry;
}

/**
* Static helper to keep code more readable
*
* @param string $name
* @param string $scopeName
* @param int|null $maxExpiry
* @return CollectionSpec
* @since 4.1.3
*/
public static function build(string $name, string $scopeName, int $maxExpiry = null): CollectionSpec
{
return new CollectionSpec($name, $scopeName, $maxExpiry);
}

/**
* Get the name of the collection
*
* @return string collection name
* @since 4.1.3
*/
public function name(): string
{
return $this->name;
}

/**
* Get the name of the scope which the collection belongs to
*
* @return string scope name
* @since 4.1.3
*/
public function scopeName(): string
{
return $this->scopeName;
}

/**
* Get the max expiry of the collection
*
* @return int|null
* @since 4.1.3
*/
public function maxExpiry(): ?int
{
return $this->maxExpiry;
}

/**
* Set the name of the collection
*
* @param string $name collection name
* @return CollectionSpec
* @since 4.1.3
*/
public function setName(string $name): CollectionSpec
{
$this->name = $name;
return $this;
}

/**
* Sets the name of the scope which the collection belongs to
* @param string $scopeName scope name
* @return CollectionSpec
* @since 4.1.3
*/
public function setScopeName(string $scopeName): CollectionSpec
{
$this->scopeName = $scopeName;
return $this;
}

/**
* Sets the max expiry of the collection
*
* @param int $seconds max expiry in seconds
* @return CollectionSpec
* @since 4.1.3
*/
public function setMaxExpiry(int $seconds): CollectionSpec
{
$this->maxExpiry = $seconds;
return $this;
}

public function setScopeName(string $name): CollectionSpec
/**
* @param CollectionSpec $spec
* @return array
* @since 4.1.3
*/
public static function export(CollectionSpec $spec): array
{
return [
'name' => $spec->name,
'scopeName' => $spec->scopeName,
'maxExpiry' => $spec->maxExpiry
];
}

public function setMaxExpiry(int $ms): CollectionSpec
/**
* @param array $collection
* @return CollectionSpec
* @since 4.1.3
*/
public static function import(array $collection): CollectionSpec
{
$collectionSpec = new CollectionSpec($collection['name'], $collection['scopeName']);
if (array_key_exists('maxExpiry', $collection)) {
$collectionSpec->setMaxExpiry($collection['maxExpiry']);
}
return $collectionSpec;
}
}
68 changes: 68 additions & 0 deletions Couchbase/Management/CreateCollectionOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* Copyright 2014-Present Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace Couchbase\Management;

class CreateCollectionOptions
{
private ?int $timeoutMilliseconds;

/**
* Static helper to keep code more readable
*
* @return CreateCollectionOptions
* @since 4.1.3
*/
public static function build(): CreateCollectionOptions
{
return new CreateCollectionOptions();
}

/**
* Sets the operation timeout in milliseconds
*
* @param int $milliseconds the operation timeout to apply
*
* @return CreateCollectionOptions
* @since 4.1.3
*/
public function timeout(int $milliseconds): CreateCollectionOptions
{
$this->timeoutMilliseconds = $milliseconds;
return $this;
}

/**
* @param CreateCollectionOptions|null $options
* @return array
* @internal
* @since 4.1.3
*/

public static function export(?CreateCollectionOptions $options): array
{
if ($options == null) {
return [];
}
return [
'timeoutMilliseconds' => $options->timeoutMilliseconds,
];
}
}
66 changes: 66 additions & 0 deletions Couchbase/Management/CreateScopeOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* Copyright 2014-Present Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace Couchbase\Management;

class CreateScopeOptions
{
private ?int $timeoutMilliseconds;

/**
* Static helper to make code more readable
*
* @return CreateScopeOptions
* @since 4.1.3
*/
public static function build(): CreateScopeOptions
{
return new CreateScopeOptions();
}
/**
* Sets the operation timeout in milliseconds
*
* @param int $milliseconds the operation timeout to apply
*
* @return CreateScopeOptions
* @since 4.1.3
*/
public function timeout(int $milliseconds): CreateScopeOptions
{
$this->timeoutMilliseconds = $milliseconds;
return $this;
}

/**
* @param CreateScopeOptions|null $options
* @return array
* @internal
* @since 4.1.3
*/
public static function export(?CreateScopeOptions $options): array
{
if ($options == null) {
return [];
}
return [
'timeoutMilliseconds' => $options->timeoutMilliseconds,
];
}
}
Loading

0 comments on commit 3f7b7ae

Please sign in to comment.