Skip to content

Commit 2b94543

Browse files
refactor: folder structure for client domains
feat: collection methods ci: bumped max variable length to 30: legibility > brevity docs: Updated README.md with basic usage info
1 parent fad6bc7 commit 2b94543

14 files changed

+662
-65
lines changed

README.md

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
<p align="center">
1+
# ArangoDB PHP client
22

3-
ArangoDB PHP client
4-
---------
3+
Low level PHP client for ArangoDB. Supports PHP versions 7 & 8.
54

65
![Github CI tests](https://github.com/LaravelFreelancerNL/arangodb-php-client/workflows/CI%20tests/badge.svg)
76
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/LaravelFreelancerNL/arangodb-php-client/badges/quality-score.png?b=next)](https://scrutinizer-ci.com/g/LaravelFreelancerNL/arangodb-php-client/?branch=next)
@@ -10,4 +9,31 @@ ArangoDB PHP client
109
<a href="https://packagist.org/packages/laravel-freelancer-nl/arangodb-php-client"><img src="https://poser.pugx.org/laravel-freelancer-nl/arangodb-php-client/downloads" alt="Total Downloads"></a>
1110
<a href="https://packagist.org/packages/laravel-freelancer-nl/arangodb-php-client"><img src="https://poser.pugx.org/laravel-freelancer-nl/arangodb-php-client/license" alt="License"></a>
1211

13-
</p>
12+
## Install
13+
14+
```
15+
composer require laravel-freelancer-nl/arangodb-php-client
16+
```
17+
## Quickstart
18+
19+
```
20+
// Create a new connector
21+
$this->connector = new Connector($config);
22+
23+
//Create the domain clients that suit your purpose and pass the connector to the client.
24+
$this->schemaClient = new SchemaClient($this->connector);
25+
26+
//Create a database
27+
$this->schemaClient->createDatabase('MyKillahProject');
28+
```
29+
30+
### config
31+
The connector has a default configuration for a local ArangoDB instance at it's default port (8529).
32+
33+
## Client domains
34+
### AdministrationClient
35+
Manages administrative functions
36+
37+
38+
### SchemaClient
39+
Manages schema related tasks like creating databases, collections, indexes, views and graphs

bin/qa.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ echo "Fix coding style"
55
echo "Check for remaining coding style errors"
66
./vendor/bin/phpcs
77

8-
98
echo "Run PHPMD"
109
./vendor/bin/phpmd src/ text phpmd-ruleset.xml
1110

phpmd-ruleset.xml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@
1212
<rule ref="rulesets/codesize.xml"/>
1313
<rule ref="rulesets/controversial.xml"/>
1414
<rule ref="rulesets/design.xml"/>
15-
<rule ref="rulesets/naming.xml"/>
15+
<rule ref="rulesets/naming.xml">
16+
<exclude name="ShortVariable"/>
17+
<exclude name="LongVariable"/>
18+
<exclude name="ShortMethodName"/>
19+
</rule>
20+
<rule name="LongVariable"
21+
since="0.2"
22+
message="Avoid excessively long variable names like {0}. Keep variable name length under {1}."
23+
class="PHPMD\Rule\Naming\LongVariable"
24+
externalInfoUrl="https://phpmd.org/rules/naming.html#longvariable">
25+
<description>
26+
Detects when a field, formal or local variable is declared with a long name.
27+
</description>
28+
<priority>3</priority>
29+
<properties>
30+
<property name="maximum" description="The variable length reporting threshold" value="30"/>
31+
<property name="subtract-suffixes" description="Comma-separated list of suffixes that will not count in the length of the variable name. Only the first matching suffix will be subtracted." value=""/>
32+
</properties>
33+
<example>
34+
<![CDATA[
35+
class Something {
36+
protected $reallyLongIntName = -3; // VIOLATION - Field
37+
public static function main( array $interestingArgumentsList[] ) { // VIOLATION - Formal
38+
$otherReallyLongName = -5; // VIOLATION - Local
39+
for ($interestingIntIndex = 0; // VIOLATION - For
40+
$interestingIntIndex < 10;
41+
$interestingIntIndex++ ) {
42+
}
43+
}
44+
}
45+
]]>
46+
</example>
47+
</rule>
1648
<rule ref="rulesets/unusedcode.xml"/>
1749
</ruleset>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArangoClient\Administration;
6+
7+
use ArangoClient\Connector;
8+
use ArangoClient\Exceptions\ArangoException;
9+
use GuzzleHttp\Exception\GuzzleException;
10+
11+
class AdministrationClient
12+
{
13+
/**
14+
* @var Connector
15+
*/
16+
protected Connector $connector;
17+
18+
/**
19+
* Documents constructor.
20+
* @param Connector $connector
21+
*/
22+
public function __construct(Connector $connector)
23+
{
24+
$this->connector = $connector;
25+
}
26+
27+
/**
28+
*
29+
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
30+
*
31+
* @param bool $details
32+
* @return array<mixed>
33+
*
34+
* @throws ArangoException
35+
* @throws GuzzleException
36+
*/
37+
public function version(bool $details = false): array
38+
{
39+
return (array) $this->connector->request(
40+
'get',
41+
'/_api/version',
42+
[
43+
'query' => [
44+
'details' => $details
45+
]
46+
]
47+
);
48+
}
49+
}

src/Connector.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ArangoClient;
66

7-
use ArangoClient\Exceptions\ArangoDbException;
7+
use ArangoClient\Exceptions\ArangoException;
88
use GuzzleHttp\Client;
99
use GuzzleHttp\Exception\GuzzleException;
1010
use GuzzleHttp\Exception\RequestException;
@@ -55,7 +55,7 @@ public function __construct(array $config = null, Client $httpClient = null)
5555
* @param string $uri
5656
* @param array<mixed> $options
5757
* @return mixed
58-
* @throws ArangoDbException|GuzzleException
58+
* @throws ArangoException|GuzzleException
5959
* @throws \Exception
6060
*/
6161
public function request(string $method, string $uri, array $options = [])
@@ -68,7 +68,7 @@ public function request(string $method, string $uri, array $options = [])
6868
if (isset($response)) {
6969
$decodeResponse = $this->decodeResponse($response);
7070
throw(
71-
new ArangoDbException(
71+
new ArangoException(
7272
(string) $decodeResponse['errorMessage'],
7373
(int) $decodeResponse['code'],
7474
$e
@@ -119,7 +119,6 @@ protected function decodeResponse(ResponseInterface $response): array
119119
$phpStream = StreamWrapper::getResource($response->getBody());
120120
$decodedStream = JsonMachine::fromStream($phpStream);
121121

122-
123122
foreach ($decodedStream as $key => $value) {
124123
$decodedResponse[$key] = $value;
125124
}

src/Exceptions/ArangoDbException.php renamed to src/Exceptions/ArangoException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ArangoClient\Exceptions;
44

5-
class ArangoDbException extends \Exception
5+
class ArangoException extends \Exception
66
{
77

88
}

src/Schema/ManagesCollections.php

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
3+
namespace ArangoClient\Schema;
4+
5+
use ArangoClient\Connector;
6+
use ArangoClient\Exceptions\ArangoException;
7+
use GuzzleHttp\Exception\GuzzleException;
8+
9+
/*
10+
* @see https://www.arangodb.com/docs/stable/http/collection.html
11+
*/
12+
trait ManagesCollections
13+
{
14+
protected Connector $connector;
15+
16+
/**
17+
*
18+
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
19+
*
20+
* @param bool $excludeSystemCollections
21+
* @return array<mixed>
22+
* @throws ArangoException
23+
* @throws GuzzleException
24+
*/
25+
public function getCollections(bool $excludeSystemCollections = false): array
26+
{
27+
return (array) $this->connector->request(
28+
'get',
29+
'/_api/collection',
30+
[
31+
'query' => [
32+
'excludeSystem' => $excludeSystemCollections
33+
]
34+
]
35+
);
36+
}
37+
38+
39+
/**
40+
* Check for collection existence in current DB.
41+
*
42+
* @param string $collection
43+
* @return bool
44+
* @throws ArangoException
45+
* @throws GuzzleException
46+
*/
47+
public function hasCollection(string $collection): bool
48+
{
49+
$collections = $this->getCollections();
50+
return array_search($collection, array_column($collections, 'name'), true) !== false;
51+
}
52+
53+
/**
54+
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-information-about-a-collection
55+
*
56+
* @param string $collection
57+
* @return array<mixed>
58+
* @throws ArangoException
59+
* @throws GuzzleException
60+
*/
61+
public function getCollection(string $collection): array
62+
{
63+
$uri = '/_api/collection/' . $collection;
64+
return (array) $this->connector->request('get', $uri);
65+
}
66+
67+
/**
68+
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#read-properties-of-a-collection
69+
*
70+
* @param string $collection
71+
* @return array<mixed>
72+
* @throws ArangoException
73+
* @throws GuzzleException
74+
*/
75+
public function getCollectionProperties(string $collection): array
76+
{
77+
$uri = '/_api/collection/' . $collection . '/properties';
78+
return (array) $this->connector->request('get', $uri);
79+
}
80+
81+
/**
82+
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-number-of-documents-in-a-collection
83+
*
84+
* @param string $collection
85+
* @return array<mixed>
86+
* @throws ArangoException
87+
* @throws GuzzleException
88+
*/
89+
public function getCollectionDocumentCount(string $collection): array
90+
{
91+
$uri = '/_api/collection/' . $collection . '/count';
92+
return (array) $this->connector->request('get', $uri);
93+
}
94+
95+
96+
/**
97+
* @see https://www.arangodb.com/docs/stable/http/collection-getting.html#return-statistics-for-a-collection
98+
*
99+
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
100+
*
101+
* @param string $collection
102+
* @param bool $details
103+
* @return array<mixed>
104+
* @throws ArangoException
105+
* @throws GuzzleException
106+
*/
107+
public function getCollectionStatistics(string $collection, bool $details = false): array
108+
{
109+
$uri = '/_api/collection/' . $collection . '/figures';
110+
return (array) $this->connector->request(
111+
'get',
112+
$uri,
113+
[
114+
'query' => [
115+
'details' => $details
116+
]
117+
]
118+
);
119+
}
120+
121+
/**
122+
* @param string $collection
123+
* @param array<mixed> $config
124+
* @param int|null $waitForSyncReplication
125+
* @param int|null $enforceReplicationFactor
126+
* @return bool
127+
* @throws ArangoException
128+
* @throws GuzzleException
129+
*/
130+
public function createCollection(
131+
string $collection,
132+
array $config = [],
133+
$waitForSyncReplication = null,
134+
$enforceReplicationFactor = null
135+
): bool {
136+
$collection = json_encode((object) array_merge($config, ['name' => $collection]));
137+
138+
$options = ['body' => $collection];
139+
if (isset($waitForSyncReplication)) {
140+
$options['query']['waitForSyncReplication'] = $waitForSyncReplication;
141+
}
142+
if (isset($enforceReplicationFactor)) {
143+
$options['query']['enforceReplicationFactor'] = $enforceReplicationFactor;
144+
}
145+
146+
return (bool) $this->connector->request('post', '/_api/collection', $options);
147+
}
148+
149+
/**
150+
* @param string $name
151+
* @param array<mixed> $config
152+
* @return array<mixed>
153+
* @throws ArangoException
154+
* @throws GuzzleException
155+
*/
156+
public function updateCollection(string $name, array $config = []): array
157+
{
158+
$uri = '/_api/collection/' . $name . '/properties';
159+
160+
$config = json_encode((object) $config);
161+
$options = ['body' => $config];
162+
163+
return (array) $this->connector->request('put', $uri, $options);
164+
}
165+
166+
/**
167+
* @param string $old
168+
* @param string $new
169+
* @return array<mixed>
170+
* @throws ArangoException
171+
* @throws GuzzleException
172+
*/
173+
public function renameCollection(string $old, string $new): array
174+
{
175+
$uri = '/_api/collection/' . $old . '/rename';
176+
177+
$newName = json_encode((object) ['name' => $new]);
178+
$options = ['body' => $newName];
179+
180+
return (array) $this->connector->request('put', $uri, $options);
181+
}
182+
183+
/**
184+
* @param string $name
185+
* @return bool
186+
* @throws ArangoException
187+
* @throws GuzzleException
188+
*/
189+
public function deleteCollection(string $name): bool
190+
{
191+
$uri = '/_api/collection/' . $name;
192+
193+
return (bool) $this->connector->request('delete', $uri);
194+
}
195+
}

0 commit comments

Comments
 (0)