Skip to content

Commit

Permalink
add financial statement search
Browse files Browse the repository at this point in the history
  • Loading branch information
jordankanta committed Jan 5, 2024
1 parent 4f5d206 commit 146e33c
Show file tree
Hide file tree
Showing 7 changed files with 637 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/RNEClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ protected function requestApi(string $method, string $url, array $options = []):
return $data;
}

protected function requestFileApi(string $method, string $url, array $options = []): string
{
$data = [];
try {
$response = $this->client->request($method, $url, $options);
$data = $response->getBody()->getContents();
} catch (GuzzleException $e) {
$this->catchResponseErrors($e);
}
return $data;
}

/**
* Catch the response errors
*
Expand Down
75 changes: 75 additions & 0 deletions src/SearchFinancialStatements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace RNEClient;

use GuzzleHttp\Exception\GuzzleException;

/**
* Class SearchFinancialStatements
*
* @package RNEClient
*/
class SearchFinancialStatements extends RNEClient implements SearchFinancialStatementsInterface
{

/**
* Search a company financial statements by its siren number
* Exact search only
*
* @param string $siren
*
* @throws GuzzleException
* @return array
*/
public function searchBySiren(string $siren): array
{
// error if the siren is not 9 length number
if (!preg_match('/^\d{9}$/', $siren)) {
throw new \Exception('Invalid input siren, please use a 9 length number.');
}

return $this->requestApi("get", "api/companies/{$siren}/attachments", ['headers' => $this->getAuthorizationHeaderArray()]);
}

/**
* Search balance sheet data by its id
* Exact search only
*
* @param string $id
*
* @throws GuzzleException
* @return array
*/
public function searchById(string $id): array
{
return $this->requestApi("get", "api/bilan-saisis/{$id}", ['headers' => $this->getAuthorizationHeaderArray()]);
}

/**
* Get balance sheet metadata by its id
* Exact search only
*
* @param string $id
*
* @throws GuzzleException
* @return array
*/
public function getMetadataById(string $id): array
{
return $this->requestApi("get", "api/bilans/{$id}", ['headers' => $this->getAuthorizationHeaderArray()]);
}

/**
* Get a balance sheet PDF file by its id
* Exact search only
*
* @param string $id
*
* @throws GuzzleException
* @return resource
*/
public function getFileById(string $id): string
{
return $this->requestFileApi("get", "api/bilans/{$id}/download", ['headers' => $this->getAuthorizationHeaderArray()]);
}
}
11 changes: 11 additions & 0 deletions src/SearchFinancialStatementsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace RNEClient;

interface SearchFinancialStatementsInterface
{
public function searchBySiren(string $siren): array;
public function searchById(string $id): array;
public function getMetadataById(string $id): array;
public function getFileById(string $id): string;
}
50 changes: 50 additions & 0 deletions tests/features/SearchFinancialStatements/SearchBySirenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace RNEClient;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

class SearchBySirenTest extends TestCase
{
private SearchFinancialStatementsInterface $RNEClient;

protected function setUp(): void
{
$this->RNEClient = new SearchFinancialStatements();
}

public function testSearchBySiren(): void
{
// get from file
$fakeResponse = file_get_contents(__DIR__ . '/../../fixtures/SearchFinancialStatements/searchBySiren.json');

$mockHandler = new MockHandler([new Response(200, [], $fakeResponse)]);
$handlerStack = HandlerStack::create($mockHandler);
$mockedClient = new Client(['handler' => $handlerStack]);

$this->RNEClient = new SearchFinancialStatements('fake_token', $mockedClient);

$data = $this->RNEClient->searchBySiren('113277693');

$this->assertIsArray($data);
$this->assertIsArray($data['bilans']);
$this->assertIsArray($data['bilansSaisis']);
}

public function testSearchBySirenWithBadSiren(): void
{
$mockHandler = new MockHandler([new Response(200, [], '[]')]);
$handlerStack = HandlerStack::create($mockHandler);
$mockedClient = new Client(['handler' => $handlerStack]);

$this->RNEClient = new SearchFinancialStatements('fake_token', $mockedClient);

$this->expectException(\Exception::class);
$this->expectExceptionMessage('Invalid input siren, please use a 9 length number.');
$this->RNEClient->searchBySiren('1234567890');
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/SearchFinancialStatements/getMetadataById.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": " idnumberxyz",
"siren": "NNNNNNNNN",
"denomination": "Nom Société",
"dateDepot": "2022-12-25",
"dateCloture": "2022-12-31",
"typeBilan": "C"
}
Loading

0 comments on commit 146e33c

Please sign in to comment.