Skip to content

Commit cd91dc0

Browse files
refactor[collection]: group index related methods into 'IndexableTrait'
1 parent 9a54948 commit cd91dc0

File tree

2 files changed

+132
-100
lines changed

2 files changed

+132
-100
lines changed

src/Collection/Collection.php

Lines changed: 20 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@
44

55
namespace ArangoDB\Collection;
66

7+
use JsonSerializable;
78
use ArangoDB\Http\Api;
89
use ArangoDB\Document\Edge;
910
use ArangoDB\Document\Vertex;
1011
use ArangoDB\Document\Document;
1112
use ArangoDB\Database\Database;
13+
use ArangoDB\Exceptions\Exception;
1214
use ArangoDB\Connection\Connection;
1315
use ArangoDB\Cursor\CollectionCursor;
14-
use ArangoDB\Collection\Index\Factory;
15-
use ArangoDB\DataStructures\ArrayList;
16-
use ArangoDB\Exceptions\IndexException;
1716
use GuzzleHttp\Exception\ClientException;
1817
use GuzzleHttp\Exception\GuzzleException;
1918
use ArangoDB\Cursor\Contracts\CursorInterface;
2019
use ArangoDB\Cursor\Exceptions\CursorException;
21-
use ArangoDB\Collection\Contracts\IndexInterface;
2220
use ArangoDB\Exceptions\Database\DatabaseException;
2321
use ArangoDB\Validation\Collection\CollectionValidator;
2422
use ArangoDB\Validation\Exceptions\InvalidParameterException;
2523
use ArangoDB\Validation\Exceptions\MissingParameterException;
26-
use JsonSerializable;
2724

2825
/**
2926
* Represents an ArangoDB collection
@@ -33,23 +30,33 @@
3330
*/
3431
class Collection implements JsonSerializable
3532
{
33+
use IndexableTrait;
34+
3635
/**
3736
* Attributes of collection
37+
*
38+
* @var array
3839
*/
3940
protected array $attributes;
4041

4142
/**
4243
* If the collection is a new one or a representation of an existing collection on server
44+
*
45+
* @var bool
4346
*/
4447
protected bool $isNew;
4548

4649
/**
4750
* Database object
51+
*
52+
* @var Database
4853
*/
4954
protected Database $database;
5055

5156
/**
5257
* Connection object
58+
*
59+
* @var Connection
5360
*/
5461
protected Connection $connection;
5562

@@ -82,6 +89,8 @@ class Collection implements JsonSerializable
8289

8390
/**
8491
* Status descriptions
92+
*
93+
* @var array
8594
*/
8695
protected array $statusStrings = [
8796
0 => 'unknown',
@@ -190,21 +199,21 @@ public function __get(string $name)
190199
}
191200

192201
/**
193-
* Set a attribute
202+
* Set an attribute
194203
*
195204
* @param string $name
196205
* @param mixed $value
197-
* @throws \Exception
206+
* @throws Exception
198207
*/
199-
public function __set(string $name, $value)
208+
public function __set(string $name, mixed $value)
200209
{
201210
// Allow defaults attributes to be set.
202211
if (array_key_exists($name, $this->attributes)) {
203212
$this->attributes[$name] = $value;
204213
return;
205214
}
206215

207-
throw new \Exception("Non-default collection property with name: ($name)");
216+
throw new Exception("Non-default collection property with name: ($name)");
208217
}
209218

210219
/**
@@ -383,34 +392,6 @@ public function getChecksum(): string
383392
}
384393
}
385394

386-
/**
387-
* Return all indexes of collection
388-
*
389-
* @return ArrayList
390-
* @throws DatabaseException|GuzzleException|InvalidParameterException|IndexException|MissingParameterException
391-
*/
392-
public function getIndexes(): ArrayList
393-
{
394-
try {
395-
if ($this->isNew()) {
396-
return new ArrayList();
397-
}
398-
399-
$uri = Api::addQuery(Api::INDEX, ['collection' => $this->getName()]);
400-
$response = $this->connection->get($uri);
401-
$data = json_decode((string)$response->getBody(), true);
402-
$indexes = new ArrayList();
403-
foreach ($data['indexes'] as $index) {
404-
$indexes->push(Factory::factory($index));
405-
}
406-
407-
return $indexes;
408-
} catch (ClientException $exception) {
409-
$response = json_decode((string)$exception->getResponse()->getBody(), true);
410-
throw new DatabaseException($response['errorMessage'], $exception, $response['errorNum']);
411-
}
412-
}
413-
414395
/**
415396
* Return the revision of collection
416397
*
@@ -435,65 +416,6 @@ public function getRevision(): string
435416
}
436417
}
437418

438-
/**
439-
* Create a index for collection
440-
* @param IndexInterface $index
441-
*
442-
* @return bool
443-
* @throws DatabaseException|GuzzleException
444-
*/
445-
public function addIndex(IndexInterface $index): bool
446-
{
447-
try {
448-
// If the collection is a new one,
449-
// we cannot add indexes on server.
450-
if ($this->isNew()) {
451-
return false;
452-
}
453-
454-
$uri = Api::addQuery(Api::INDEX, ['collection' => $this->getName()]);
455-
$response = $this->connection->post($uri, $index->getCreateData());
456-
457-
json_decode((string)$response->getBody(), true);
458-
return true;
459-
} catch (ClientException $exception) {
460-
$response = json_decode((string)$exception->getResponse()->getBody(), true);
461-
throw new DatabaseException($response['errorMessage'], $exception, $response['errorNum']);
462-
}
463-
}
464-
465-
/**
466-
* Drops a index of collection
467-
* @param IndexInterface $index
468-
*
469-
* @return bool
470-
* @throws DatabaseException|GuzzleException
471-
*/
472-
public function dropIndex(IndexInterface $index): bool
473-
{
474-
try {
475-
// If the collection is a new one, or the index,
476-
// we cannot drop it on server.
477-
if ($this->isNew() || $index->isNew()) {
478-
return false;
479-
}
480-
481-
$uri = Api::addUriParam(Api::INDEX, $index->getId());
482-
$response = $this->connection->delete($uri);
483-
$data = json_decode((string)$response->getBody(), true);
484-
return !$data['error'];
485-
} catch (ClientException $exception) {
486-
$response = json_decode((string)$exception->getResponse()->getBody(), true);
487-
$databaseException = new DatabaseException($response['errorMessage'], $exception, $response['errorNum']);
488-
489-
if ($exception->getResponse()->getStatusCode() === 404) {
490-
return false;
491-
}
492-
493-
throw $databaseException;
494-
}
495-
}
496-
497419
/**
498420
* Saves or update the collection.
499421
* Except for 'waitForSync' and 'name', a collection can not be modified after creation.
@@ -587,9 +509,7 @@ public function findByKey(string $key, bool $isVertex = false)
587509
$response = $this->connection->get(sprintf("%s/%s", $uri, $handle));
588510
$data = json_decode((string)$response->getBody(), true);
589511
$document = $this->isGraph() ? new Edge($data, $this) : new Document($data, $this);
590-
$document = $isVertex ? new Vertex($data, $this) : $document;
591-
592-
return $document;
512+
return $isVertex ? new Vertex($data, $this) : $document;
593513
} catch (ClientException $exception) {
594514
$response = json_decode((string)$exception->getResponse()->getBody(), true);
595515

@@ -665,7 +585,7 @@ public function rename(string $newName): bool
665585
}
666586

667587
/**
668-
* @see \JsonSerializable::jsonSerialize()
588+
* @see JsonSerializable::jsonSerialize
669589
*/
670590
public function jsonSerialize(): mixed
671591
{

src/Collection/IndexableTrait.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArangoDB\Collection;
6+
7+
use ArangoDB\Http\Api;
8+
use ArangoDB\Collection\Index\Factory;
9+
use ArangoDB\DataStructures\ArrayList;
10+
use ArangoDB\Exceptions\IndexException;
11+
use GuzzleHttp\Exception\ClientException;
12+
use GuzzleHttp\Exception\GuzzleException;
13+
use ArangoDB\Collection\Contracts\IndexInterface;
14+
use ArangoDB\Exceptions\Database\DatabaseException;
15+
use ArangoDB\Validation\Exceptions\InvalidParameterException;
16+
use ArangoDB\Validation\Exceptions\MissingParameterException;
17+
18+
/**
19+
* Indexable trait
20+
*
21+
* @package ArangoDB\Collection
22+
* @author Lucas S. Vieira
23+
*/
24+
trait IndexableTrait
25+
{
26+
/**
27+
* Return all indexes of collection
28+
*
29+
* @return ArrayList
30+
* @throws DatabaseException|GuzzleException|InvalidParameterException|IndexException|MissingParameterException
31+
*/
32+
public function getIndexes(): ArrayList
33+
{
34+
try {
35+
if ($this->isNew()) {
36+
return new ArrayList();
37+
}
38+
39+
$uri = Api::addQuery(Api::INDEX, ['collection' => $this->getName()]);
40+
$response = $this->connection->get($uri);
41+
$data = json_decode((string)$response->getBody(), true);
42+
$indexes = new ArrayList();
43+
foreach ($data['indexes'] as $index) {
44+
$indexes->push(Factory::factory($index));
45+
}
46+
47+
return $indexes;
48+
} catch (ClientException $exception) {
49+
$response = json_decode((string)$exception->getResponse()->getBody(), true);
50+
throw new DatabaseException($response['errorMessage'], $exception, $response['errorNum']);
51+
}
52+
}
53+
54+
/**
55+
* Create an index for collection
56+
* @param IndexInterface $index
57+
*
58+
* @return bool
59+
* @throws DatabaseException|GuzzleException
60+
*/
61+
public function addIndex(IndexInterface $index): bool
62+
{
63+
try {
64+
// If the collection is a new one,
65+
// we cannot add indexes on server.
66+
if ($this->isNew()) {
67+
return false;
68+
}
69+
70+
$uri = Api::addQuery(Api::INDEX, ['collection' => $this->getName()]);
71+
$response = $this->connection->post($uri, $index->getCreateData());
72+
73+
json_decode((string)$response->getBody(), true);
74+
return true;
75+
} catch (ClientException $exception) {
76+
$response = json_decode((string)$exception->getResponse()->getBody(), true);
77+
throw new DatabaseException($response['errorMessage'], $exception, $response['errorNum']);
78+
}
79+
}
80+
81+
/**
82+
* Drops an index of collection
83+
* @param IndexInterface $index
84+
*
85+
* @return bool
86+
* @throws DatabaseException|GuzzleException
87+
*/
88+
public function dropIndex(IndexInterface $index): bool
89+
{
90+
try {
91+
// If the collection is a new one, or the index,
92+
// we cannot drop it on server.
93+
if ($this->isNew() || $index->isNew()) {
94+
return false;
95+
}
96+
97+
$uri = Api::addUriParam(Api::INDEX, $index->getId());
98+
$response = $this->connection->delete($uri);
99+
$data = json_decode((string)$response->getBody(), true);
100+
return !$data['error'];
101+
} catch (ClientException $exception) {
102+
$response = json_decode((string)$exception->getResponse()->getBody(), true);
103+
$databaseException = new DatabaseException($response['errorMessage'], $exception, $response['errorNum']);
104+
105+
if ($exception->getResponse()->getStatusCode() === 404) {
106+
return false;
107+
}
108+
109+
throw $databaseException;
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)