Skip to content

Commit e8ac788

Browse files
authored
Merge pull request #5 from b24io/task#4974
Task#4974
2 parents c503941 + 3f2b5c5 commit e8ac788

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1458
-29
lines changed

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
- 7.3
7+
- 7.4
8+
9+
sudo: false
10+
11+
cache:
12+
directories:
13+
- vendor
14+
- $HOME/.composer/cache
15+
16+
before_script:
17+
- composer self-update
18+
- composer install --no-interaction --prefer-source --dev
19+
20+
script:
21+
composer test

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# loyalty-php-sdk
2+
## 1.1.0 (26.01.2020)
3+
* add method `getReportByMetricCode` in `\Metrics\Transport\Admin` transport, return `MetricReportResponse` with `Report` DTO
4+
* add interface `DefaultRequestArgumentsInterface` with default request argument fields
5+
* add SystemJournal transport for role admin
6+
27
## 1.0.0 (8.01.2020)
38
* add OperationsJournal
49
* add operation type `AccrualTransaction`

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# loyalty-php-sdk
2+
[![License](https://poser.pugx.org/b24io/loyalty-php-sdk/license.svg)](https://packagist.org/packages/b24io/loyalty-php-sdk) [![Total Downloads](https://poser.pugx.org/b24io/loyalty-php-sdk/downloads.svg)](https://packagist.org/packages/b24io/loyalty-php-sdk)
3+
24
Loyalty PHP SDK is a tool for work with REST-API Bitrix24 Application [Loyalty Program and bonus cards for Bitrix24 CRM](https://www.bitrix24.ru/apps/?app=b24io.loyalty)
35

46
* Loyalty app adds bonus card for Bitrix24 client profile in CRM

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
"symfony/http-foundation": "^4.2"
3636
},
3737
"require-dev": {
38-
"phpunit/phpunit": "^7",
39-
"jakub-onderka/php-parallel-lint": "0.9",
40-
"jakub-onderka/php-console-highlighter": "~0.3"
38+
"phpunit/phpunit": "^7"
4139
},
4240
"autoload": {
4341
"psr-4": {
@@ -46,7 +44,6 @@
4644
},
4745
"scripts": {
4846
"test": [
49-
"parallel-lint . --exclude vendor --no-colors",
5047
"phpunit --colors=always --verbose"
5148
]
5249
}

examples/metrics/admin-get-metric-collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
foreach ($metricResponse->getMetricCollection() as $metric) {
5959
print(sprintf(
6060
'%s - | %s | %s',
61-
$metric->getCode(),
61+
$metric->getCode()->getValue(),
6262
$metric->getType()->key(),
6363
$metric->getName()
6464
) . PHP_EOL);
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
5+
6+
use \Monolog\Logger;
7+
use \B24io\Loyalty\SDK;
8+
use \B24io\Loyalty\SDK\OperationsJournal\DTO\OperationType;
9+
10+
use GuzzleHttp\HandlerStack;
11+
use GuzzleHttp\Middleware;
12+
use GuzzleHttp\MessageFormatter;
13+
use Ramsey\Uuid\Uuid;
14+
15+
$argv = getopt('', ['clientApiKey::', 'authApiKey::', 'apiEndpoint::', 'dateFrom::', 'dateTo::']);
16+
$fileName = basename(__FILE__);
17+
18+
$clientApiKey = $argv['clientApiKey'];
19+
if ($clientApiKey === null) {
20+
throw new \InvalidArgumentException(sprintf('error: argument «clientApiKey» not found') . PHP_EOL);
21+
}
22+
$authApiKey = $argv['authApiKey'];
23+
if ($authApiKey === null) {
24+
throw new \InvalidArgumentException(sprintf('error: argument «authApiKey» not found') . PHP_EOL);
25+
}
26+
$apiEndpoint = $argv['apiEndpoint'];
27+
if ($apiEndpoint === null) {
28+
throw new \InvalidArgumentException(sprintf('error: argument «apiEndpoint» not found') . PHP_EOL);
29+
}
30+
$dateFrom = new \DateTime($argv['dateFrom']);
31+
$dateTo = new \DateTime($argv['dateTo']);
32+
33+
34+
// check connection to API
35+
$log = new Logger('loyalty-php-sdk');
36+
$log->pushHandler(new \Monolog\Handler\StreamHandler('loyalty-php-sdk-example.log', Logger::DEBUG));
37+
$guzzleHandlerStack = HandlerStack::create();
38+
$guzzleHandlerStack->push(
39+
Middleware::log(
40+
$log,
41+
new MessageFormatter(MessageFormatter::SHORT)
42+
)
43+
);
44+
$httpClient = new \GuzzleHttp\Client();
45+
$log->info('loyalty.apiClient.start');
46+
$token = new SDK\Auth\DTO\Token(
47+
SDK\Transport\DTO\Role::initializeByCode('admin'),
48+
Uuid::fromString($clientApiKey),
49+
Uuid::fromString($authApiKey)
50+
);
51+
$apiClient = new SDK\ApiClient($apiEndpoint, $token, $httpClient, $log);
52+
$apiClient->setGuzzleHandlerStack($guzzleHandlerStack);
53+
54+
// connect to application and read settings
55+
$metricTransport = SDK\Metrics\Transport\Admin\Fabric::getMetricTransport($apiClient, $log);
56+
$decimalMoneyFormatter = new \Money\Formatter\DecimalMoneyFormatter(new \Money\Currencies\ISOCurrencies());
57+
try {
58+
$metricResponse = $metricTransport->getMetricCollection();
59+
foreach ($metricResponse->getMetricCollection() as $metric) {
60+
print(sprintf(
61+
'%s - | %s | %s',
62+
$metric->getCode()->getValue(),
63+
$metric->getType()->key(),
64+
$metric->getName()
65+
) . PHP_EOL);
66+
print(sprintf('%s', $metric->getDescription()) . PHP_EOL);
67+
68+
// get metric values
69+
$reportResponse = $metricTransport->getReportByMetricCode($metric->getCode(), $dateFrom, $dateTo);
70+
foreach ($reportResponse->getReport()->getValues() as $cnt => $value) {
71+
/**
72+
* @var SDK\Metrics\DTO\Values\AbstractValue $value
73+
*/
74+
switch ($reportResponse->getReport()->getMetric()->getType()) {
75+
case SDK\Metrics\DTO\MetricType::FLOAT():
76+
case SDK\Metrics\DTO\MetricType::INTEGER():
77+
print(sprintf(
78+
' %s | %s | %s',
79+
$cnt,
80+
$value->getTimestamp()->format(\DATE_ATOM),
81+
$value->getValue()
82+
) . PHP_EOL);
83+
break;
84+
case SDK\Metrics\DTO\MetricType::MONEY():
85+
/**
86+
* @var SDK\Metrics\DTO\Values\MoneyValue $value
87+
*/
88+
print(sprintf(
89+
' %s | %s | %s',
90+
$cnt,
91+
$value->getTimestamp()->format(\DATE_ATOM),
92+
$decimalMoneyFormatter->format($value->getValue())
93+
) . PHP_EOL);
94+
break;
95+
case SDK\Metrics\DTO\MetricType::PERCENTAGE():
96+
/**
97+
* @var SDK\Metrics\DTO\Values\PercentageValue $value
98+
*/
99+
print(sprintf(
100+
' %s | %s | %s',
101+
$cnt,
102+
$value->getTimestamp()->format(\DATE_ATOM),
103+
$value->getValue()->format()
104+
) . PHP_EOL);
105+
break;
106+
}
107+
}
108+
print('-----------' . PHP_EOL);
109+
}
110+
} catch (SDK\Exceptions\ApiClientException $exception) {
111+
var_dump($exception->getApiProblem()->asArray());
112+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
5+
6+
use \Monolog\Logger;
7+
use \B24io\Loyalty\SDK;
8+
use \B24io\Loyalty\SDK\OperationsJournal\DTO\OperationType;
9+
use Ramsey\Uuid\Uuid;
10+
11+
use GuzzleHttp\HandlerStack;
12+
use GuzzleHttp\Middleware;
13+
use GuzzleHttp\MessageFormatter;
14+
15+
$argv = getopt('', ['clientApiKey::', 'authApiKey::', 'apiEndpoint::', 'dateFrom::', 'dateTo::']);
16+
$fileName = basename(__FILE__);
17+
18+
19+
$clientApiKey = $argv['clientApiKey'];
20+
if ($clientApiKey === null) {
21+
throw new \InvalidArgumentException(sprintf('error: argument «clientApiKey» not found') . PHP_EOL);
22+
}
23+
$authApiKey = $argv['authApiKey'];
24+
if ($authApiKey === null) {
25+
throw new \InvalidArgumentException(sprintf('error: argument «authApiKey» not found') . PHP_EOL);
26+
}
27+
$apiEndpoint = $argv['apiEndpoint'];
28+
if ($apiEndpoint === null) {
29+
throw new \InvalidArgumentException(sprintf('error: argument «apiEndpoint» not found') . PHP_EOL);
30+
}
31+
$dateFrom = new \DateTime($argv['dateFrom']);
32+
$dateTo = new \DateTime($argv['dateTo']);
33+
34+
// check connection to API
35+
$log = new Logger('loyalty-php-sdk');
36+
$log->pushHandler(new \Monolog\Handler\StreamHandler('loyalty-php-sdk-example.log', Logger::DEBUG));
37+
$guzzleHandlerStack = HandlerStack::create();
38+
$guzzleHandlerStack->push(Middleware::log($log, new MessageFormatter(MessageFormatter::SHORT)));
39+
$httpClient = new \GuzzleHttp\Client();
40+
$log->info('loyalty.apiClient.start');
41+
42+
$token = new SDK\Auth\DTO\Token(
43+
SDK\Transport\DTO\Role::initializeByCode('admin'),
44+
Uuid::fromString($clientApiKey),
45+
Uuid::fromString($authApiKey)
46+
);
47+
$apiClient = new SDK\ApiClient($apiEndpoint, $token, $httpClient, $log);
48+
$apiClient->setGuzzleHandlerStack($guzzleHandlerStack);
49+
50+
// connect to application and read settings
51+
$settingsTransport = SDK\Settings\Transport\Admin\Fabric::getSettingsTransport($apiClient, $log);
52+
$systemJournalTransport = SDK\SystemJournal\Transport\Admin\Fabric::getSystemJournalTransport($apiClient, $log);
53+
54+
try {
55+
$result = $settingsTransport->getApplicationSettings();
56+
print(sprintf('currency form application settings: %s' . PHP_EOL, $result->getSettings()->getCurrency()->getCode()));
57+
58+
$journalResponse = $systemJournalTransport->getSystemJournalByPeriod($dateFrom, $dateTo, null);
59+
60+
print (sprintf('system journal response: ' . PHP_EOL));
61+
print (sprintf('- date from: %s' . PHP_EOL, $journalResponse->getSystemJournal()->getDateFrom()->format(\DATE_ATOM)));
62+
print (sprintf('- date to: %s' . PHP_EOL, $journalResponse->getSystemJournal()->getDateTo()->format(\DATE_ATOM)));
63+
print (sprintf('- count: %s' . PHP_EOL, $journalResponse->getSystemJournal()->getItems()->count()));
64+
65+
foreach ($journalResponse->getSystemJournal()->getItems() as $cnt => $journalItem) {
66+
print(sprintf(
67+
'%s | %s | %s | %s | %s |',
68+
$cnt,
69+
$journalItem->getTimestamp()->format(\DATE_ATOM),
70+
$journalItem->getLevel()->key(),
71+
$journalItem->getSource(),
72+
$journalItem->getMessage()
73+
) . PHP_EOL);
74+
}
75+
} catch (\Throwable $exception) {
76+
var_dump($exception->getMessage());
77+
}

phpunit.xml.dist

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
bootstrap="tests/bootstrap.php"
5+
colors="true">
6+
<testsuites>
7+
<testsuite name="loyalty-php-sdk test suite">
8+
<directory>tests/src/</directory>
9+
</testsuite>
10+
</testsuites>
11+
<filter>
12+
<whitelist>
13+
<directory suffix=".php">src/</directory>
14+
</whitelist>
15+
</filter>
16+
</phpunit>

src/ApiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ApiClient
2525
/**
2626
* @var string SDK version
2727
*/
28-
protected const SDK_VERSION = '1.0.0';
28+
protected const SDK_VERSION = '1.1.0';
2929
/**
3030
* @var string user agent
3131
*/

src/Metrics/DTO/Fabric.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
namespace B24io\Loyalty\SDK\Metrics\DTO;
66

7+
use B24io\Loyalty\SDK\Cards\DTO\Percentage;
8+
use B24io\Loyalty\SDK\Metrics\DTO\Values\FloatValue;
9+
use B24io\Loyalty\SDK\Metrics\DTO\Values\IntegerValue;
10+
use B24io\Loyalty\SDK\Metrics\DTO\Values\MetricValueCollection;
11+
use B24io\Loyalty\SDK\Metrics\DTO\Values\MoneyValue;
12+
use B24io\Loyalty\SDK\Metrics\DTO\Values\PercentageValue;
13+
use B24io\Loyalty\SDK\Metrics\Exceptions\UnknownMetricTypeException;
14+
use Money\Currency;
15+
use Money\Money;
716
use Ramsey\Uuid\Uuid;
817

918
/**
@@ -25,12 +34,69 @@ public static function initMetricFromArray(array $metric): Metric
2534
Uuid::fromString($metric['uuid']),
2635
(string)$metric['name'],
2736
(string)$metric['description'],
28-
(string)$metric['code'],
37+
new MetricCode((string)$metric['code']),
2938
MetricType::memberByKey((string)$metric['type']),
3039
new \DateTime($metric['created'])
3140
);
3241
}
3342

43+
/**
44+
* @param array $report
45+
*
46+
* @return Report
47+
* @throws \Exception
48+
*/
49+
public static function initReportFromArray(array $report): Report
50+
{
51+
$metric = self::initMetricFromArray($report['metric']);
52+
53+
return new Report(
54+
$metric,
55+
new \DateTime($report['date_from']),
56+
new \DateTime($report['date_to']),
57+
self::initMetricValueCollectionFromArray($metric, $report['values'])
58+
);
59+
}
60+
61+
/**
62+
* @param Metric $metric
63+
* @param array $metricValueCollection
64+
*
65+
* @return MetricValueCollection
66+
* @throws \Exception
67+
*/
68+
public static function initMetricValueCollectionFromArray(Metric $metric, array $metricValueCollection): MetricValueCollection
69+
{
70+
$metricValues = new MetricValueCollection();
71+
72+
foreach ($metricValueCollection as $item) {
73+
switch ($metric->getType()) {
74+
case MetricType::INTEGER():
75+
$metricValues->attach(new IntegerValue((int)$item['value'], new \DateTime($item['timestamp'])));
76+
break;
77+
case MetricType::FLOAT():
78+
$metricValues->attach(new FloatValue((float)$item['value'], new \DateTime($item['timestamp'])));
79+
break;
80+
case MetricType::PERCENTAGE():
81+
$metricValues->attach(new PercentageValue(new Percentage((string)$item['value']), new \DateTime($item['timestamp'])));
82+
break;
83+
case MetricType::MONEY():
84+
$metricValues->attach(
85+
new MoneyValue(
86+
new Money((string)$item['value']['amount'], new Currency($item['value']['currency'])),
87+
new \DateTime($item['timestamp'])
88+
)
89+
);
90+
break;
91+
default:
92+
throw new UnknownMetricTypeException(sprintf('unknown metric type [%s]', $metric->getType()->key()));
93+
break;
94+
}
95+
}
96+
97+
return $metricValues;
98+
}
99+
34100
/**
35101
* @param array $metricCollection
36102
*

0 commit comments

Comments
 (0)