Skip to content

Commit c9a2a98

Browse files
feat: Database functions
docs: fixed badges ci: --no_cache option added for psalm
1 parent 54e2165 commit c9a2a98

File tree

14 files changed

+485
-11
lines changed

14 files changed

+485
-11
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
php: [7.3, 7.4, 8.0]
13+
php: [7.4, 8.0]
1414
arangodb: [3.6.0, 3.6, latest]
1515
name: Quality checks PHP ${{ matrix.php }} / ArangoDB ${{ matrix.arangodb }}
1616

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
ArangoDB PHP client
44
---------
55

6-
![Github CI tests](https://github.com/LaravelFreelancerNL/arangodb-php-client/workflows/Continuous%20Integration/badge.svg)
6+
![Github CI tests](https://github.com/LaravelFreelancerNL/arangodb-php-client/workflows/CI%20tests/badge.svg)
77
[![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)
8-
[![Code Coverage](https://scrutinizer-ci.com/g/LaravelFreelancerNL/arangodb-php-client/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/LaravelFreelancerNL/arangodb-php-client/?branch=next)
8+
[![Code Coverage](https://scrutinizer-ci.com/g/LaravelFreelancerNL/arangodb-php-client/badges/coverage.png?b=next)](https://scrutinizer-ci.com/g/LaravelFreelancerNL/arangodb-php-client/?branch=next)
99
<a href="https://packagist.org/packages/laravel-freelancer-nl/arangodb-php-client"><img src="https://poser.pugx.org/laravel-freelancer-nl/arangodb-php-client/v/unstable" alt="Latest Version"></a>
1010
<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>
1111
<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>

bin/qa.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#!/usr/bin/env bash
2-
2+
echo "Fix coding style"
33
./vendor/bin/phpcbf
44

5+
echo "Check for remaining coding style errors"
56
./vendor/bin/phpcs
67

8+
9+
echo "Run PHPMD"
710
./vendor/bin/phpmd src/ text phpmd-ruleset.xml
811

12+
echo "Run PHPStan"
913
./vendor/bin/phpstan analyse -c phpstan.neon
1014

11-
./vendor/bin/psalm
15+
echo "Run Psalm"
16+
./vendor/bin/psalm --no-cache
1217

18+
echo "Run PHPUnit"
1319
./vendor/bin/phpunit

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
"minimum-stability": "dev",
1717
"prefer-stable" : true,
1818
"require": {
19-
"php": "^7.3|^7.4|^8.0",
19+
"php": "^7.4|^8.0",
2020
"ext-json": "*",
21-
"guzzlehttp/guzzle": "^7.2"
21+
"guzzlehttp/guzzle": "^7.2",
22+
"spatie/data-transfer-object": "^2.8",
23+
"halaxa/json-machine": "^0.6.0"
2224
},
2325
"require-dev": {
2426
"phpunit/phpunit": "^9.5",

composer.lock

Lines changed: 112 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
44
bootstrap="vendor/autoload.php"
55
colors="true"
6+
stopOnFailure="false"
67
>
78
<testsuites>
89
<testsuite name="all">

src/Connector.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArangoClient;
6+
7+
use ArangoClient\Exceptions\ArangoDbException;
8+
use GuzzleHttp\Client;
9+
use GuzzleHttp\Exception\GuzzleException;
10+
use GuzzleHttp\Exception\RequestException;
11+
use GuzzleHttp\Psr7\StreamWrapper;
12+
use JsonMachine\JsonMachine;
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
/*
16+
* The connector handles connections to ArangoDB's HTTP REST API.
17+
* @see https://www.arangodb.com/docs/stable/http/
18+
*/
19+
class Connector implements ConnectorInterface
20+
{
21+
/**
22+
* @var array<string|numeric|null>
23+
*/
24+
protected array $config = [
25+
'host' => 'http://localhost',
26+
'port' => '8529',
27+
'AuthUser' => 'root',
28+
'AuthPassword' => null,
29+
'AuthType' => 'basic'
30+
];
31+
32+
protected Client $httpClient;
33+
34+
/**
35+
* Connector constructor.
36+
*
37+
* @param array<string|numeric|null>|null $config
38+
* @param Client|null $httpClient
39+
*/
40+
public function __construct(array $config = null, Client $httpClient = null)
41+
{
42+
if ($config !== null) {
43+
$this->config = $config;
44+
}
45+
46+
$this->config = $this->mapHttpClientConfig();
47+
48+
$this->httpClient = isset($httpClient) ? $httpClient : new Client($this->config);
49+
}
50+
51+
/**
52+
* @psalm-suppress MixedReturnStatement
53+
*
54+
* @param string $method
55+
* @param string $uri
56+
* @param array<mixed> $options
57+
* @return mixed
58+
* @throws ArangoDbException|GuzzleException
59+
* @throws \Exception
60+
*/
61+
public function request(string $method, string $uri, array $options = [])
62+
{
63+
$response = null;
64+
try {
65+
$response = $this->httpClient->request($method, $uri, $options);
66+
} catch (RequestException $e) {
67+
$response = $e->getResponse();
68+
if (isset($response)) {
69+
$decodeResponse = $this->decodeResponse($response);
70+
throw(
71+
new ArangoDbException(
72+
(string) $decodeResponse['errorMessage'],
73+
(int) $decodeResponse['code'],
74+
$e
75+
)
76+
);
77+
}
78+
throw($e);
79+
}
80+
81+
$decodeResponse = $this->decodeResponse($response);
82+
83+
if (isset($decodeResponse['result'])) {
84+
return $decodeResponse['result'];
85+
}
86+
87+
return $decodeResponse;
88+
}
89+
90+
/**
91+
* @return array<string|numeric|null>
92+
*/
93+
protected function mapHttpClientConfig(): array
94+
{
95+
$this->config['base_uri'] = (string) $this->config['host'] . ':' . (string) $this->config['port'];
96+
97+
return $this->config;
98+
}
99+
100+
/**
101+
* @return array<string|numeric|null>
102+
*/
103+
public function getConfig(): array
104+
{
105+
return $this->config;
106+
}
107+
108+
/**
109+
* @psalm-suppress MixedAssignment, MixedArrayOffset
110+
* @SuppressWarnings(PHPMD.StaticAccess)
111+
*
112+
* @param ResponseInterface $response
113+
* @return array<array-key, mixed>
114+
*/
115+
protected function decodeResponse(ResponseInterface $response): array
116+
{
117+
$decodedResponse = [];
118+
119+
$phpStream = StreamWrapper::getResource($response->getBody());
120+
$decodedStream = JsonMachine::fromStream($phpStream);
121+
122+
123+
foreach ($decodedStream as $key => $value) {
124+
$decodedResponse[$key] = $value;
125+
}
126+
127+
return $decodedResponse;
128+
}
129+
}

src/Client.php renamed to src/ConnectorInterface.php

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

33
namespace ArangoClient;
44

5-
class Client
5+
interface ConnectorInterface
66
{
77

88
}

0 commit comments

Comments
 (0)