Skip to content

Commit

Permalink
Merge pull request #30 from b24io/v4
Browse files Browse the repository at this point in the history
v4.1.0
  • Loading branch information
b24io-sdk authored May 22, 2024
2 parents 35ecccf + afc779f commit aaaac02
Show file tree
Hide file tree
Showing 26 changed files with 191 additions and 115 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/integration-v4.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: "Integration tests"

on:
pull_request:
branches:
- v4
- master
push:
branches:
- v4
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
matrix:
php-version:
- "8.3"
- "8.2"
dependencies: [ highest ]

steps:
Expand All @@ -37,7 +38,7 @@ jobs:
run: "composer update --no-interaction --no-progress --no-suggest"

- name: "PHPStan"
run: "make phpstan"
run: "make lint-phpstan"

- name: "is PHPStan check succeeded"
if: ${{ success() }}
Expand Down
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# loyalty-php-sdk

## 4.1.0 (2024.05.22)
* add `TurnoversItemResult` for `CardItemResult`, contains:
* total purchases count
* total purchases sum
* add `TurnoversItemResult` for `ContactItemResult`, contains:
* total purchases count
* total purchases sum
* add `CardLevelItem` for `ContactItemResult`, contains:
* `id` card level id
* `nextLevelId` next level id
* `name` level name
* `code` level code
* `defaultPercentage` card level default percentage
* `description` card level description
* `externalId` card level external id
* `created` date time card level create
* `modified` date time card level update

## 4.0.0 (2024.05.11)
* bump SDK version for PHP `8.2` and `8.3` branch


## 3.1.0 (2024.05.11)

* add requirements:
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ default:
@echo "make needs target:"
@egrep -e '^\S+' ./Makefile | grep -v default | sed -r 's/://' | sed -r 's/^/ - /'

phpstan:
lint-phpstan:
vendor/bin/phpstan analyse
phpinsights:
lint-phpinsights:
vendor/bin/phpinsights analyse ./src
lint-rector:
vendor/bin/rector process --dry-run
lint-rector-fix:
vendor/bin/rector process

test-integration:
php vendor/bin/phpunit --colors=always --testsuite integration-tests
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# loyalty-php-sdk

[![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) [![Build Status](https://travis-ci.org/b24io/loyalty-php-sdk.svg?branch=master)](https://travis-ci.org/b24io/loyalty-php-sdk)
[![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)

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)
Expand All @@ -24,12 +24,12 @@ $ composer require b24io/loyalty-php-sdk

### Versions

| loyalty-php-sdk version | support status | REST-API version | PHP versions |
|-------------------------|----------------|------------------|--------------|
| **v4** | 🟩 **active** | **2** | `8.2`,`8.3` |
| v3 | 🟨 bugfix only | 2 | `7.4` |
| v2 | 🟥 end of life | 1 | `7.4` |
| v1 | 🟥 end of life | 1 | `7.4` |
| loyalty-php-sdk<br/> version | support status | build <br/> status | REST-API<br/>version | PHP<br/> versions |
|------------------------------|-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------|-------------------|
| **v4.x** | 🟩 **active** | ![integration tests](https://github.com/b24io/loyalty-php-sdk/actions/workflows/integration-v4.yml/badge.svg?branch=v4) <br/> ![phpstan](https://github.com/b24io/loyalty-php-sdk/actions/workflows/phpstan.yml/badge.svg?branch=v4) | **2** | `8.2`,`8.3` |
| v3.x | 🟨 bugfix only | ![integration tests](https://github.com/b24io/loyalty-php-sdk/actions/workflows/integration-v4.yml/badge.svg?branch=v3) <br/> ![phpstan](https://github.com/b24io/loyalty-php-sdk/actions/workflows/phpstan.yml/badge.svg?branch=v3) | 2 | `7.4` |
| v2 | 🟥 end of life ☠️ | | 1 | `7.4` |
| v1 | 🟥 end of life ☠️ | | 1 | `7.4` |

### Requirements

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"loyalty program",
"bonus cards",
"loyalty program SDK",
"b24io",
"REST-API",
"Bitrix24"
],
Expand Down Expand Up @@ -47,7 +46,8 @@
"symfony/stopwatch": "7.*",
"roave/security-advisories": "dev-master",
"nunomaduro/phpinsights": "2.*",
"fakerphp/faker": "1.23.*"
"fakerphp/faker": "1.23.*",
"rector/rector": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 22 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\DowngradeLevelSetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withSets(
[DowngradeLevelSetList::DOWN_TO_PHP_82]
)
->withPhpSets(
php82: true // 8.2
)
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
]);
16 changes: 12 additions & 4 deletions src/Commands/Cards/ExportCards.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,36 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'============',
'',
]);

/**
* @var ?string $apiEndpointUrl
*/
$apiEndpointUrl = $input->getOption('api-endpoint-url');
if ($apiEndpointUrl === null) {
$output->writeln('error: you must set api-endpoint-url option');

return Command::INVALID;
}

/**
* @var ?string $apiClientId
*/
$apiClientId = $input->getOption('api-client-id');
if ($apiClientId === null) {
$output->writeln('error: you must set api-client-id option');

return Command::INVALID;
}

/**
* @var ?string $apiAdminKey
*/
$apiAdminKey = $input->getOption('api-admin-key');
if ($apiAdminKey === null) {
$output->writeln('error: you must set api-admin-key option');

return Command::INVALID;
}

/**
* @var ?string $filename
*/
$filename = $input->getOption('file');
if ($filename === null) {
$output->writeln('error: you must set file option');
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Transactions/BulkTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
description: 'Bulk accrual or payment transaction to all active cards')]
class BulkTransactions extends Command
{
public function __construct(private LoggerInterface $logger)
public function __construct(
private readonly LoggerInterface $logger
)
{
parent::__construct();
}
Expand Down Expand Up @@ -186,9 +188,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$trxHistoryByCard = $admSb->transactionsScope()->transactions()->getByCardNumber($card->number);
$reasonCodeHistory = array_map(static function (Reason $reason) {
return $reason->code;
}, array_column($trxHistoryByCard->getTransactions(), 'reason'));
$reasonCodeHistory = array_map(static fn(Reason $reason) => $reason->code, array_column($trxHistoryByCard->getTransactions(), 'reason'));
if (in_array($reasonCode, $reasonCodeHistory, true)) {
$this->logger->info(sprintf('transaction already processed for card %s', $card->number));
continue;
Expand Down
15 changes: 9 additions & 6 deletions src/Commands/Transactions/BurnBonuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@
description: 'Burning of bonuses accrued before the specified date')]
class BurnBonuses extends Command
{
private const string REASON_ID = 'b24io.loyalty.sdk.cli.util';

public function __construct(private LoggerInterface $logger)
/**
* @var string
*/
private const REASON_ID = 'b24io.loyalty.sdk.cli.util';

public function __construct(
private readonly LoggerInterface $logger
)
{
parent::__construct();
}
Expand Down Expand Up @@ -167,9 +172,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// get last 50 transactions for current card
$trxHistoryByCard = $admSb->transactionsScope()->transactions()->getByCardNumber($card->number);
$reasonCodeHistory = array_map(static function (Reason $reason) {
return $reason->code;
}, array_column($trxHistoryByCard->getTransactions(), 'reason'));
$reasonCodeHistory = array_map(static fn(Reason $reason) => $reason->code, array_column($trxHistoryByCard->getTransactions(), 'reason'));

// if trx with reason-code exists, pass card
if (in_array($reasonCode, $reasonCodeHistory, true)) {
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/Transactions/LoadTransactionsFromFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
class LoadTransactionsFromFile extends Command
{
public function __construct(
private TransactionsReader $transactionsReader,
private DecimalMoneyFormatter $decimalMoneyFormatter,
private LoggerInterface $logger
private readonly TransactionsReader $transactionsReader,
private readonly DecimalMoneyFormatter $decimalMoneyFormatter,
private readonly LoggerInterface $logger
)
{
parent::__construct();
Expand Down Expand Up @@ -166,7 +166,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/**
* @var array{'extension':string,'filename':string} $pathInfo
*/
$pathInfo = pathinfo($filename);
$pathInfo = pathinfo((string) $filename);

$totalTransactionsInFile = $this->transactionsReader->countTransactionsInFile($filename);
$output->writeln(sprintf('transactions count %s in file %s', $totalTransactionsInFile, $filename));
Expand Down
7 changes: 7 additions & 0 deletions src/Common/Result/Cards/CardItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace B24io\Loyalty\SDK\Common\Result\Cards;

use B24io\Loyalty\SDK\Common\Result\Contacts\ContactItemResult;
use B24io\Loyalty\SDK\Common\Result\Turnovers\TurnoversItemResult;
use B24io\Loyalty\SDK\Core\Result\AbstractItem;
use DateTimeImmutable;
use Money\Currency;
Expand All @@ -24,6 +25,7 @@
* @property-read string $externalId
* @property-read ?CardLevelItemResult $level
* @property-read ContactItemResult $contact
* @property-read TurnoversItemResult $turnovers
*/
class CardItemResult extends AbstractItem
{
Expand Down Expand Up @@ -51,6 +53,11 @@ public function __get(int|string $offset)
return null;
}
return new ContactItemResult($this->data[$offset]);
case 'turnovers':
if ($this->data[$offset] === null) {
return null;
}
return new TurnoversItemResult($this->data[$offset]);
default:
return parent::__get($offset);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Common/Result/Contacts/ContactItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use B24io\Loyalty\SDK\Common\FullName;
use B24io\Loyalty\SDK\Common\Gender;
use B24io\Loyalty\SDK\Common\Result\Cards\CardItemResult;
use B24io\Loyalty\SDK\Common\Result\Cards\CardLevelItemResult;
use B24io\Loyalty\SDK\Common\Result\Turnovers\TurnoversItemResult;
use B24io\Loyalty\SDK\Core\Result\AbstractItem;
use DateTimeImmutable;
use DateTimeZone;
Expand All @@ -22,6 +24,8 @@
* @property-read DateTimeImmutable $created
* @property-read DateTimeImmutable $modified
* @property-read ?CardItemResult $card
* @property-read ?CardLevelItemResult $cardLevel
* @property-read ?TurnoversItemResult $turnovers
*/
class ContactItemResult extends AbstractItem
{
Expand Down Expand Up @@ -51,6 +55,16 @@ public function __get(int|string $offset)
return null;
}
return new CardItemResult($this->data['card']);
case 'cardLevel':
if ($this->data['card_level'] === null) {
return null;
}
return new CardLevelItemResult($this->data['card_level']);
case 'turnovers':
if ($this->data['turnovers'] === null) {
return null;
}
return new TurnoversItemResult($this->data['turnovers']);
default:
return parent::__get($offset);
}
Expand Down
25 changes: 10 additions & 15 deletions src/Common/Result/Transactions/TransactionItemResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,16 @@ class TransactionItemResult extends AbstractItem
{
public function __get(int|string $offset)
{
switch ($offset) {
case 'value':
return $this->decimalMoneyParser->parse(
$this->data[$offset]['amount'],
new Currency(($this->data[$offset]['currency']) ?? '')
);
case 'type':
return TransactionType::from(str_replace('_transaction', '', $this->data[$offset]));
case 'cardId':
return Uuid::fromString($this->data['card']['id']);
case 'cardNumber':
return $this->data['card']['number'];
default:
return parent::__get($offset);
}
return match ($offset) {
'value' => $this->decimalMoneyParser->parse(
$this->data[$offset]['amount'],
new Currency(($this->data[$offset]['currency']) ?? '')
),
'type' => TransactionType::from(str_replace('_transaction', '', $this->data[$offset])),
'cardId' => Uuid::fromString($this->data['card']['id']),
'cardNumber' => $this->data['card']['number'],
default => parent::__get($offset),
};

}
}
28 changes: 28 additions & 0 deletions src/Common/Result/Turnovers/TurnoversItemResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace B24io\Loyalty\SDK\Common\Result\Turnovers;

use B24io\Loyalty\SDK\Core\Result\AbstractItem;
use Money\Currency;
use Money\Money;

/**
* @property-read int $totalPurchasesCount
* @property-read Money $totalPurchasesSum
*/
class TurnoversItemResult extends AbstractItem
{
public function __get(int|string $offset)
{
return match ($offset) {
'totalPurchasesCount' => (int)$this->data['total_purchases_count'],
'totalPurchasesSum' => $this->decimalMoneyParser->parse(
$this->data['total_purchases_sum']['amount'],
new Currency($this->data['total_purchases_sum']['currency'] ?? '')
),
default => parent::__get($offset),
};
}
}
Loading

0 comments on commit aaaac02

Please sign in to comment.