Edit File: BucketManager.inc
<?php /** * Author: Shlomi * Date: Sep 4, 2017 1:39:39 PM * Founder: Shlomi Bazak (SBZsoft) (c) * File: BucketManager.inc * * Description: * */ namespace B2\Entities; use B2\Http\B2Client; use B2\Exceptions\NotFoundException; // No direct access. defined("__NSS__") or die("Restrected Access."); class BucketManager { private $_client; private $_bucketList; public function __construct(B2Client $client) { $this->_client = $client; $this->_bucketList = null; } /** * * @param string $name * @param string $type can be Bucket::TYPE_PUBLIC | Bucket::TYPE_PRIVATE * @return \B2\Entities\Bucket */ public function createBucket($name, $type){ $params = [ 'accountId' => $this->_client->getAccountId(), 'bucketName' => $name, 'bucketType' => $type ]; $data = $this->_client->requestApi(B2Api::CREATE_BUCKET, $params); return new Bucket($data['bucketId'], $data['bucketName'], $data['bucketType']); } /** * List all buckets for the given client * @param B2Client $client * @return \B2\Entities\Bucket[] */ public function listBuckets($force=false){ if(!$force && $this->_bucketList !== null) return $this->_bucketList; $this->_bucketList = []; $params = [ 'accountId' => $this->_client->getAccountId() ]; $data = $this->_client->requestApi(B2Api::LIST_BUCKETS, $params); foreach ($data['buckets'] as $bucket) { $this->_bucketList[] = new Bucket($bucket['bucketId'], $bucket['bucketName'], $bucket['bucketType']); } return $this->_bucketList; } public function deleteBucket(Bucket $bucket){ $params = [ 'accountId' => $this->_client->getAccountId(), 'bucketId' => $bucket->getId() ]; $this->_client->requestApi(B2Api::DELETE_BUCKET, $params); return true; } /** * Bet bucket by name * @param string $name * @return \B2\Entities\Bucket|NULL null if no bucket found. */ public function getBucketByName($name){ $list = $this->listBuckets(); foreach ($list as $bucket) if($name === $bucket->getName()) return $bucket; throw new NotFoundException("Bucket '$name' not found."); } }