Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
CRAP
95.45% covered (success)
95.45%
21 / 22
Requestor
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
2 / 3
7
95.45% covered (success)
95.45%
21 / 22
 request
0.00% covered (danger)
0.00%
0 / 1
3.00
92.31% covered (success)
92.31%
12 / 13
 catchClientException
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
8 / 8
 catchServerException
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace Mecom\Transaction\Traits;
use Illuminate\Support\Collection;
use Mecom\Transaction\Exceptions\TransactionServiceClientException;
use Mecom\Transaction\Exceptions\TransactionServiceServerException;
use Mecom\Transaction\Exceptions\TransactionServiceUnauthorizedException;
use Mecom\Transaction\Exceptions\TransactionUnprocessableEntityException;
trait Requestor
{
    private function request(string $method, string $uri, array $payload = []) : Collection
    {
        try {
            $response = $this->client
                ->request(
                    $method,
                    $uri,
                    [
                        'json' => $payload,
                        'headers' => $this->headers()
                    ]
                );
            $json = json_decode($response->getBody()->getContents(), true);
            return collect($json);
        } catch (\GuzzleHttp\Exception\ClientException $e) {
            $this->catchClientException($e);
        } catch (\GuzzleHttp\Exception\ServerException $e) {
            $this->catchServerException($e);
        }
    }
    private function catchClientException(\GuzzleHttp\Exception\ClientException $exception)
    {
        if ($exception->getCode() == 401) {
            throw new TransactionServiceUnauthorizedException;
        }
        if ($exception->getCode() == 422) {
            throw new TransactionUnprocessableEntityException(
                $exception->getMessage(),
                $exception->getCode(),
                $exception
            );
        }
        throw new TransactionServiceClientException;
    }
    private function catchServerException(\GuzzleHttp\Exception\ServerException $exception)
    {
        throw new TransactionServiceServerException;
    }
}