Skip to content

Commit

Permalink
Added factory generator to generate fake responses
Browse files Browse the repository at this point in the history
  • Loading branch information
firebed committed Oct 8, 2022
1 parent bd6a5c2 commit e61ebd5
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 30 deletions.
54 changes: 54 additions & 0 deletions src/Factories/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Firebed\AadeMyData\Factories;

abstract class Factory
{
private string $modelName;
private array $state = [];
private array $except = [];

public static function factoryForModel($modelName): Factory
{
$namespace = dirname(__NAMESPACE__);
$factoryName = $namespace . '\\Factories\\' . basename($modelName) . 'Factory';

$factory = self::newFactory($factoryName);
$factory->modelName = $modelName;
return $factory;
}

public static function newFactory($factoryName): Factory
{
return new $factoryName;
}

public function make(array $attributes = [])
{
$attributes = array_merge($this->definition(), $attributes, $this->state);

if (!empty($this->except)) {
$attributes = array_diff_key($attributes, array_flip($this->except));
}

$instance = new $this->modelName;
$instance->setAttributes($attributes);
return $instance;
}

public function except(array $fields): self
{
$this->except = $fields;

return $this;
}

public function state(array $attributes): self
{
$this->state = array_merge($attributes);

return $this;
}

public abstract function definition(): array;
}
30 changes: 30 additions & 0 deletions src/Factories/ResponseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Firebed\AadeMyData\Factories;

class ResponseFactory extends Factory
{
public function definition(): array
{
return [
'index' => 1,
'invoiceUid' => strtoupper(sha1('invoice-uid')),
'invoiceMark' => rand(100_000_000_000_000, 999_999_999_999_999),
'statusCode' => 'Success'
];
}

public function uid(string $uid): self
{
return $this->state([
'uid' => strtoupper(sha1($uid))
]);
}

public function cancelled(): self
{
return $this->state([
'cancellationMark' => rand(100_000_000_000_000, 999_999_999_999_999)
])->except(['index', 'invoiceUid', 'invoiceMark']);
}
}
84 changes: 54 additions & 30 deletions src/Http/MyDataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,43 @@
namespace Firebed\AadeMyData\Http;

use Error;
use Firebed\AadeMyData\Loader;
use Firebed\AadeMyData\Models\ResponseDoc;
use Firebed\AadeMyData\Parser\RequestedDocParser;
use Firebed\AadeMyData\Parser\ResponseDocParser;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class MyDataRequest
abstract class MyDataRequest
{
private static ?string $user_id = null;
private static ?string $subscription_key = null;
private static ?string $env = null;

private ?string $url = null;

public function __construct()
{
$urls = require __DIR__ . '/../../config/urls.php';
private static mixed $mockResponse;

if (!array_key_exists(self::$env, $urls)) {
throw new Error("Invalid or missing environment value. Please invoke MyDataRequest::setEnvironment to set the environment value, possible values are [dev,prod].");
public static function mockResponse($mockResponse): void
{
if (is_callable($mockResponse)) {
self::$mockResponse = $mockResponse;
return;
}

$class = get_class($this);
if (!array_key_exists($class, $urls[self::$env])) {
throw new Error("Unspecified URL for " . basename($class));
}
self::$mockResponse = fn() => $mockResponse;
}

$this->url = $urls[self::$env][$class];
public static function isMocking(): bool
{
return isset(self::$mockResponse);
}

public static function init(string $user_id, string $subscription_key, string $env): void
{
self::setCredentials($user_id, $subscription_key);
self::setEnvironment($env);
}

public static function setCredentials($user_id, $subscription_key): void
public static function setCredentials(string $user_id, string $subscription_key): void
{
self::$user_id = $user_id;
self::$subscription_key = $subscription_key;
Expand All @@ -44,21 +50,14 @@ public static function setEnvironment($env): void
self::$env = $env;
}

private static function validateCredentials(): void
{
if (empty(self::$user_id) || empty(self::$subscription_key)) {
throw new Error("Missing credentials. Please use MyDataRequest::setCredentials method to set your myDATA Rest API credentials.");
}
}

public function isDevelopment(): bool
public static function isDevelopment(): bool
{
return self::$env === 'dev';
}

public function isProduction(): bool
public static function isProduction(): bool
{
return !$this->isDevelopment();
return self::$env === 'prod';
}

/**
Expand All @@ -68,9 +67,15 @@ protected function get(array $query): mixed
{
self::validateCredentials();

$response = $this->client()->get($this->url, ['query' => $query]);
$url = Loader::getUrl(get_class($this), self::$env);

return RequestedDocParser::parseXML(simplexml_load_string($response->getBody()));
if (self::isMocking()) {
return self::mock();
}

$response = $this->client()->get($url, ['query' => $query]);

return RequestedDocParser::parseXML(simplexml_load_string($response->getBody()->getContents()));
}

/**
Expand All @@ -80,6 +85,12 @@ protected function post(array $query = null, string $body = null): ResponseDoc
{
self::validateCredentials();

$url = Loader::getUrl(get_class($this), self::$env);

if (self::isMocking()) {
return self::mock();
}

$params = [];
if (!empty($query)) {
$params['query'] = $query;
Expand All @@ -89,13 +100,19 @@ protected function post(array $query = null, string $body = null): ResponseDoc
$params['body'] = $body;
}

$response = $this->client()->post($this->url, $params);
$xml = simplexml_load_string($response->getBody());
$response = $this->client()->post($url, $params);
$xml = simplexml_load_string($response->getBody()->getContents());

return ResponseDocParser::parseXML($xml);
}

protected function client(): Client
private function mock()
{
$callback = self::$mockResponse;
return $callback();
}

private function client(): Client
{
return new Client([
'headers' => [
Expand All @@ -105,4 +122,11 @@ protected function client(): Client
],
]);
}
}

private static function validateCredentials(): void
{
if (empty(self::$user_id) || empty(self::$subscription_key)) {
throw new Error("Missing credentials. Please use MyDataRequest::setCredentials method to set your myDATA Rest API credentials.");
}
}
}
39 changes: 39 additions & 0 deletions src/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Firebed\AadeMyData;

use Error;

class Loader
{
private static array $map;

public static function getUrl(string $class, string $env)
{
self::loadMap($env);

if (!array_key_exists($class, self::$map[$env])) {
throw new Error("Unspecified URL for " . basename($class));
}

return self::$map[$env][$class];
}

private static function loadMap(string $env): void
{
if (self::mapLoaded()) {
return;
}

self::$map = require __DIR__ . '/../config/urls.php';

if (!array_key_exists($env, self::$map)) {
throw new Error("Invalid environment '$env'. Please invoke MyDataRequest::setEnvironment to set the environment value, possible values are [dev,prod].");
}
}

private static function mapLoaded(): bool
{
return isset(self::$map);
}
}
39 changes: 39 additions & 0 deletions src/Models/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Firebed\AadeMyData\Models;

use Firebed\AadeMyData\Traits\HasFactory;

class Response extends Type
{
use HasFactory;

/**
* @return int|null Αριθμός Σειράς Οντότητας εντός του υποβληθέντος xml
*/
Expand All @@ -12,6 +16,11 @@ public function getIndex(): ?int
return $this->get('index');
}

public function setIndex(int $index): void
{
$this->put('index', $index);
}

/**
* <p>Καθορίζει το είδος της απάντησης (πετυχημένη ή αποτυχημένη διαδικασία).</p>
*
Expand All @@ -37,6 +46,11 @@ public function getStatusCode(): ?string
return $this->get('statusCode');
}

public function setStatusCode(string $statusCode): void
{
$this->put('statusCode', $statusCode);
}

/**
* Επιστρέφει μόνο στην περίπτωση που η υποβολή αφορούσε παραστατικό.
*
Expand All @@ -47,6 +61,11 @@ public function getInvoiceUid(): ?string
return $this->get('invoiceUid');
}

public function setInvoiceUid(string $invoiceUid): void
{
$this->put('invoiceUid', $invoiceUid);
}

/**
* Περιέχει το mark του υποβληθέντος παραστατικού στην
* περίπτωση που υποβλήθηκαν παραστατικά και το mark του παραστατικού που
Expand All @@ -60,6 +79,11 @@ public function getInvoiceMark(): ?string
return $this->get('invoiceMark');
}

public function setInvoiceMark(string $invoiceMark): void
{
$this->put('invoiceMark', $invoiceMark);
}

/**
* Επιστρέφει μόνο στην περίπτωση που η υποβολή αφορούσε χαρακτηρισμό.
*
Expand All @@ -70,6 +94,11 @@ public function getClassificationMark(): ?string
return $this->get('classificationMark');
}

public function setClassificationMark(string $classificationMark): void
{
$this->put('classificationMark', $classificationMark);
}

/**
* Επιστρέφει στην περίπτωση που η υποβολή έγινε μέσω παρόχου.
*
Expand All @@ -80,6 +109,11 @@ public function getAuthenticationCode(): ?string
return $this->get('authenticationCode');
}

public function setAuthenticationCode(string $authenticationCode): void
{
$this->put('authenticationCode', $authenticationCode);
}

/**
* Επιστρέφει μόνο στην περίπτωση που η υποβολή αφορούσε ακύρωση παραστατικού.
*
Expand All @@ -90,6 +124,11 @@ public function getCancellationMark(): ?string
return $this->get('cancellationMark');
}

public function setCancellationMark(string $cancellationMark): void
{
$this->put('cancellationMark', $cancellationMark);
}

public function hasErrors(): bool
{
return !is_null($this->getErrors()) && count($this->getErrors()) > 0;
Expand Down
5 changes: 5 additions & 0 deletions src/Models/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function attributes(): array
{
return $this->attributes;
}

public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}
}
Loading

0 comments on commit e61ebd5

Please sign in to comment.