Skip to content

Commit

Permalink
Updated client and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
vasildakov committed Dec 27, 2023
1 parent ab2c031 commit 5e665b6
Show file tree
Hide file tree
Showing 16 changed files with 468 additions and 15 deletions.
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"prefer-stable": true,
"minimum-stability": "dev",
"require": {
"php": ">=8.1",
"php": ">=8.2",
"psr/http-message": "^2.0",
"psr/http-factory": "^1.0",
"psr/http-client": "^1.0",
Expand All @@ -30,14 +30,15 @@
"guzzlehttp/guzzle": "^7.7"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "dev-master",
"php-coveralls/php-coveralls": "dev-master",
"phpmd/phpmd": "dev-master",
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "dev-master",
"squizlabs/php_codesniffer": "4.0.x-dev",
"symfony/var-dumper": "^7.0",
"theseer/phpdox": "dev-master",
"vimeo/psalm": "4.x-dev",
"psalm/plugin-phpunit": "dev-master",
"friendsofphp/php-cs-fixer": "dev-master",
"phpmd/phpmd": "dev-master"
"vimeo/psalm": "4.x-dev"
},
"license": "MIT",
"autoload": {
Expand Down
95 changes: 89 additions & 6 deletions composer.lock

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

35 changes: 35 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use GuzzleHttp\Client;
use Laminas\Diactoros\RequestFactory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use VasilDakov\Econt\Configuration;
use VasilDakov\Econt\Econt;

chdir(dirname(__DIR__));

// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server') {
$path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
if (is_string($path) && __FILE__ !== $path && is_file($path)) {
return false;
}
unset($path);
}

// Composer autoloading
include __DIR__ . '/../vendor/autoload.php';


$configuration = new Configuration('iasp-dev','1Asp-dev');

/** @var ClientInterface $client */
$client = new Client();

/** @var RequestFactoryInterface $factory */
$factory = new RequestFactory();

$econt = new Econt($configuration, $client, $factory);

dump(json_decode($econt->getClientProfiles()));
12 changes: 12 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace VasilDakov\Econt;

final class Configuration
{
public function __construct(
public string $username,
public string $password,
) {
}
}
19 changes: 19 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace VasilDakov\Econt;

final class Constants
{
public const CITY = 'city';
public const CITY_ID = 'cityID';
public const ADDRESS = 'address';
public const DATE = 'date';
public const COUNTRY_CODE = 'countryCode';
public const SHIPMENT_TYPE = 'shipmentType';
public const SERVICE_OFFICE = 'serviceOffice';
public const SERVICE_OFFICE_LATITUDE = 'serviceOfficeLatitude';
public const SERVICE_OFFICE_LONGITUDE = 'serviceOfficeLongitude';
public const SERVICE_OFFICE_CLIENTS_WORK_TIME = 'serviceOfficeClientsWorkTimes';
public const SERVICE_OFFICE_COURIER_WORK_TIME = 'serviceOfficeCourierWorkTimes';
public const SERVICE_OFFICE_TIME = 'serviceOfficeTime';
}
144 changes: 142 additions & 2 deletions src/Econt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,154 @@

namespace VasilDakov\Econt;

use Fig\Http\Message\RequestMethodInterface;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;

/**
* Class Econt
*
* @author Vasil Dakov <[email protected]>
*/
final class Econt
final class Econt implements EcontInterface
{
public function __construct()
public const API_URL = 'https://demo.econt.com';

public function __construct(
private readonly Configuration $configuration,
private readonly ClientInterface $client,
private readonly RequestFactoryInterface $factory
) {
}

/**
* @throws ClientExceptionInterface
*/
public function getClientProfiles(): string
{
$request = $this->createRequest(
RequestMethodInterface::METHOD_POST,
self::API_URL . '/ee/services/Profile/ProfileService.getClientProfiles.json',
[]
);

$response = $this->client->sendRequest($request);

return $response->getBody()->getContents();
}

/**
* @throws ClientExceptionInterface
*/
public function getCountries(): string
{
$request = $this->createRequest(
RequestMethodInterface::METHOD_POST,
self::API_URL . '/ee/services/Nomenclatures/NomenclaturesService.getCountries.json',
[]
);

$response = $this->client->sendRequest($request);

return $response->getBody()->getContents();
}

/**
* @throws ClientExceptionInterface
*/
public function getCities(array $data): string
{
$request = $this->createRequest(
RequestMethodInterface::METHOD_POST,
self::API_URL . '/ee/services/Nomenclatures/NomenclaturesService.getCities.json',
[
Constants::COUNTRY_CODE => $data['countryCode']
]
);

$response = $this->client->sendRequest($request);

return $response->getBody()->getContents();
}

public function getOffices(array $data): string
{
$request = $this->createRequest(
RequestMethodInterface::METHOD_POST,
self::API_URL . '/ee/services/Nomenclatures/NomenclaturesService.getOffices.json',
[
Constants::COUNTRY_CODE => $data['countryCode'],
Constants::CITY_ID => $data['cityId'],
]
);

$response = $this->client->sendRequest($request);

return $response->getBody()->getContents();
}


public function getShipmentStatuses(array $data): string
{
$request = $this->createRequest(
RequestMethodInterface::METHOD_POST,
self::API_URL . '/ee/services/Shipments/ShipmentService.getShipmentStatuses.json',
$data
);

$response = $this->client->sendRequest($request);

return $response->getBody()->getContents();
}


/**
* @param string $method
* @param string $uri
* @param array $data
* @return RequestInterface
*/
private function createRequest(string $method, string $uri, array $data): RequestInterface
{
$request = $this->factory->createRequest($method, $uri);

$request = $request->withAddedHeader('Content-Type', 'application/json');
$request = $request->withAddedHeader(
'Authorization',
'Basic ' . base64_encode(
$this->configuration->username . ":" . $this->configuration->password
)
);

$request->getBody()->write(json_encode($data));

return $request;
}

public function getStreets(array $data): string
{
return '';
}

public function getQuarters(array $data): string
{
return '';
}

public function createLabel(array $data): string
{
return '';
}

public function createLabels(array $data): string
{
return '';
}

public function deleteLabels(array $data): string
{
return '';
}
}
Loading

0 comments on commit 5e665b6

Please sign in to comment.