Skip to content

Commit

Permalink
New changes, add tests and add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
arayiksmbatyan committed Dec 8, 2021
1 parent c863c18 commit 61081eb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 35 deletions.
49 changes: 16 additions & 33 deletions src/Services/DomainService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,64 @@

namespace DigitaloceanApi\Services;

use GuzzleHttp\Client;
use DigitaloceanApi\Services\SendRequestService;

class DomainService
{
/**
* @var $domainsApiUrl
* @var $sendRequestService
*/
protected $domainsApiUrl;
protected $sendRequestService;

/**
* @var $client
*/
protected $client;

public function __construct()
/**
* @param \DigitaloceanApi\Services\SendRequestService $sendRequestService
*/
public function __construct(SendRequestService $sendRequestService)
{
$this->domainsApiUrl = config('digital-ocean.api-urls.domains');
$this->client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '. config('digital-ocean.token'),
]]);
$this->sendRequestService = $sendRequestService;
}

/**
* @return mixed
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @return mixed|\stdClass
*/
public function index()
{
$res = $this->client->request('GET', $this->domainsApiUrl);

return $res->getBody()->getContents();
return $this->sendRequestService->send('GET', $this->domainsApiUrl);
}

/**
* @param array $params
*
* @return mixed
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @return mixed|\stdClass
*/
public function store(array $params)
{
$res = $this->client->request('POST', $this->domainsApiUrl, $params);

return $res->getBody()->getContents();
return $this->sendRequestService->send('POST', $this->domainsApiUrl, ['json' => $params]);
}

/**
* @param $name
*
* @return mixed
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @return mixed|\stdClass
*/
public function show($name)
{
$res = $this->client->request('GET', "$this->domainsApiUrl/${name}");

return $res->getBody()->getContents();
return $this->sendRequestService->send('GET', "{$this->domainsApiUrl}/{$name}");
}

/**
* @param $name
*
* @return mixed
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @return mixed|\stdClass
*/
public function destroy($name)
{
$res = $this->client->request('DELETE', "$this->domainsApiUrl/${name}");

return $res->getBody()->getContents();
return $this->sendRequestService->send('DELETE', "{$this->domainsApiUrl}/${name}");
}
}
55 changes: 55 additions & 0 deletions src/Services/SendRequestService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace DigitaloceanApi\Services;

use GuzzleHttp\Client;

class SendRequestService
{
/**
* @var $client
*/
protected $client;

public function __construct()
{
$this->client = new Client([
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '. config('digital-ocean.token'),
]]);
}

/**
* @param $method
* @param $uri
* @param $params
*
* @return mixed|\stdClass
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function send($method, $uri, $params = [])
{
try {
$res = $this->client->request($method, $uri, $params);

if($method !== 'DELETE') {
$response = \json_decode($res->getBody());
$response->status_code = $res->getStatusCode();

return $response;
}
$response = new \stdClass();
$response->status_code = $res->getStatusCode();

return $response;

} catch (\GuzzleHttp\Exception\RequestException $exception) {
$error = \json_decode($exception->getResponse()->getBody());
$error->status_code = $exception->getResponse()->getStatusCode();

return $error;
}
}
}
24 changes: 22 additions & 2 deletions tests/Feature/Domains/TestDomainService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Tests\TestCase;
use DigitaloceanApi\Services\DomainService;
use function PHPUnit\Framework\assertTrue;
use Illuminate\Support\Facades\Log;

class TestDomainService extends TestCase
{
Expand All @@ -17,8 +18,27 @@ public function setUp(): void
$this->service = $this->app->make(DomainService::class);
}

public function testGago()
public function testIndex()
{
$this->service->index();
$result = $this->service->index();
file_put_contents("tests/logs/DomainServiceLogs.log", json_encode($result).PHP_EOL, FILE_APPEND | LOCK_EX);
}

public function testStore()
{
$result = $this->service->store(['name' => 'digital-ocean-package.io']);
file_put_contents("tests/logs/DomainServiceLogs.log", json_encode($result).PHP_EOL, FILE_APPEND | LOCK_EX);
}

public function testShow()
{
$result = $this->service->show('digital-ocean-package.io');
file_put_contents("tests/logs/DomainServiceLogs.log", json_encode($result).PHP_EOL, FILE_APPEND | LOCK_EX);
}

public function testDestroy()
{
$result = $this->service->destroy('digital-ocean-package.io');
file_put_contents("tests/logs/DomainServiceLogs.log", json_encode($result).PHP_EOL, FILE_APPEND | LOCK_EX);
}
}
2 changes: 2 additions & 0 deletions tests/logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit 61081eb

Please sign in to comment.