Skip to content

Commit

Permalink
facade, tests, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
matusstafura committed Mar 17, 2022
1 parent 73d580f commit 195a85a
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ FIO_TOKEN = "your_api_token"
```php
<?php

use Matusstafura\FioApi\FioReport;
use Matusstafura\FioApi\Facades\FioReport;

FioReport::yesterday()->getReport();
FioReport::yesterday();
// will return transaction from previous day

FioReport::today()->getReport();
FioReport::today();
// will return today's transactions

FioReport::betweenDates("2022-02-14", "2022-02-18")->getReport();
FioReport::betweenDates("2022-02-14", "2022-02-18");
// will return transactions between dates in format YYYY-MM-DD

```
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"require-dev": {
"orchestra/testbench": "^7.1",
"pestphp/pest": "^1.21",
"guzzlehttp/guzzle": "^7.0"
"guzzlehttp/guzzle": "^7.0",
"spatie/pest-plugin-test-time": "^1.0"
},
"config": {
"allow-plugins": {
Expand All @@ -40,7 +41,10 @@
"laravel": {
"providers": [
"Matusstafura\\FioApi\\FioReportServiceProvider"
]
],
"aliases": {
"FioReport": "Matusstafura\\FioApi\\Facades\\FioReport"
}
}
}
}
131 changes: 129 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/Facades/FioReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Matusstafura\FioApi\Facades;

use Illuminate\Support\Facades\Facade;

class FioReport extends Facade
{
protected static function getFacadeAccessor()
{
return 'fio-report';
}
}
30 changes: 15 additions & 15 deletions src/FioReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

class FioReport
{
public function __construct(public string $date_from, public string $date_to)
public function __construct(protected FioApiService $fioApiService)
{
}

public function getReport()
public function getReport($date_from, $date_to)
{
$fioApiService = new FioApiService();
$url = $fioApiService->baseUrl()."/$this->date_from/$this->date_to/transactions.json";
$url = $this->fioApiService->baseUrl()."/${date_from}/${date_to}/transactions.json";

$response = Http::get($url);

Expand All @@ -25,35 +24,36 @@ public function getReport()
return $response->json();
}

public static function betweenDates(string $date_from, string $date_to): static
public function betweenDates(string $date_from, string $date_to)
{
try {
$date_from = Carbon::parse($date_from)->format('Y-m-d');
$date_to = Carbon::parse($date_to)->format('Y-m-d');

return new static($date_from, $date_to);
return $this->getReport($date_from, $date_to);
} catch (\Exception $e) {
throw new \Exception("Invalid input date.");
}
}

public static function today(): static
public function today()
{
$today = Carbon::today()->toDateString();
return new static($today, $today);
return $this->getReport($this->todayDate(), $this->todayDate());
}

public static function yesterday(): static
public function yesterday()
{
$yesterday = Carbon::yesterday()->toDateString();
return new static($yesterday, $yesterday);
return $this->getReport($this->yesterdayDate(), $this->yesterdayDate() );
}

public function validateDate($date, $format = 'Y-m-d')
public function todayDate()
{
$d = Carbon::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
return Carbon::today()->toDateString();
}

public function yesterdayDate()
{
return Carbon::yesterday()->toDateString();
}

}
4 changes: 4 additions & 0 deletions src/FioReportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ public function boot()
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/fio.php', 'fio');

$this->app->singleton('fio-report', function () {
return new FioReport(new FioApiService());
});
}
}
10 changes: 9 additions & 1 deletion tests/Unit/FioReportTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

use Illuminate\Support\Facades\Http;
use Matusstafura\FioApi\FioReport;
use Matusstafura\FioApi\Facades\FioReport;
use function Spatie\PestPluginTestTime\testTime;

it('checks for if response is ok', function () {
Http::fake([
Expand All @@ -24,3 +25,10 @@
]);
FioReport::betweenDates("2000-77-22","2022-03-11")->getReport();
})->throws(Exception::class);

it('gets today\'s date', function () {
testTime()->freeze('2022-03-11 12:34:56');
$todayDate = FioReport::todayDate();

expect($todayDate)->toBe('2022-03-11');
});

0 comments on commit 195a85a

Please sign in to comment.