From 4bf00e824012530bf7f95bfac26c23748a16b9aa Mon Sep 17 00:00:00 2001 From: mesilov Date: Wed, 22 May 2024 02:29:10 +0600 Subject: [PATCH 1/6] Add 'Turnovers' feature to CardItemResult This commit introduces the 'Turnovers' functionality within the CardItemResult class. By doing so, it now provides additional information, such as the total purchases count and sum. Necessary tests and documentation have also been updated accordingly. --- CHANGELOG.md | 9 ++++++ src/Commands/Cards/ExportCards.php | 16 +++++++--- src/Common/Result/Cards/CardItemResult.php | 7 +++++ .../Result/Turnovers/TurnoversItemResult.php | 31 +++++++++++++++++++ .../Services/Admin/Cards/CardsTest.php | 7 ++++- 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/Common/Result/Turnovers/TurnoversItemResult.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9154ee4..9c80fa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # loyalty-php-sdk +## 4.1.0 (2024.05.22) +* add `Turnovers` for `CardItemResult`, contains: + * total purchases count + * total purchases sum + +## 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: diff --git a/src/Commands/Cards/ExportCards.php b/src/Commands/Cards/ExportCards.php index 66c8be2..6638edf 100644 --- a/src/Commands/Cards/ExportCards.php +++ b/src/Commands/Cards/ExportCards.php @@ -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'); diff --git a/src/Common/Result/Cards/CardItemResult.php b/src/Common/Result/Cards/CardItemResult.php index bbe8545..d3a1e07 100644 --- a/src/Common/Result/Cards/CardItemResult.php +++ b/src/Common/Result/Cards/CardItemResult.php @@ -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; @@ -24,6 +25,7 @@ * @property-read string $externalId * @property-read ?CardLevelItemResult $level * @property-read ContactItemResult $contact + * @property-read TurnoversItemResult $turnovers */ class CardItemResult extends AbstractItem { @@ -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); } diff --git a/src/Common/Result/Turnovers/TurnoversItemResult.php b/src/Common/Result/Turnovers/TurnoversItemResult.php new file mode 100644 index 0000000..7a6a3d4 --- /dev/null +++ b/src/Common/Result/Turnovers/TurnoversItemResult.php @@ -0,0 +1,31 @@ +data['total_purchases_count']; + case 'totalPurchasesSum': + return $this->decimalMoneyParser->parse( + $this->data['total_purchases_sum']['amount'], + new Currency($this->data['total_purchases_sum']['currency'] ?? '') + ); + default: + return parent::__get($offset); + } + } +} \ No newline at end of file diff --git a/tests/Integration/Services/Admin/Cards/CardsTest.php b/tests/Integration/Services/Admin/Cards/CardsTest.php index 87ec406..f7c4ea8 100644 --- a/tests/Integration/Services/Admin/Cards/CardsTest.php +++ b/tests/Integration/Services/Admin/Cards/CardsTest.php @@ -105,6 +105,11 @@ public function testAddCard(): void $cardStatus, $addedCard->getCard()->status ); + $this->assertEquals( + 0, + $addedCard->getCard()->turnovers->totalPurchasesCount + ); + $this->assertTrue($addedCard->getCard()->turnovers->totalPurchasesSum->isZero()); } /** @@ -356,7 +361,7 @@ static function ( * @covers Cards::count() * @testdox Test count cards method */ - public function testCount():void + public function testCount(): void { $cnt = $this->sb->cardsScope()->cards()->count(); $this->assertGreaterThan(0, $cnt); From 43e88d4e6a18345cd812a415ca034cbced68c7d6 Mon Sep 17 00:00:00 2001 From: mesilov Date: Wed, 22 May 2024 02:34:35 +0600 Subject: [PATCH 2/6] Enable integration tests on pull requests for v4 and master branches In this update, the GitHub Actions workflow for integration tests has been modified. Now, the workflow will be triggered not only on push events, but also when a pull request is made against the v4 or master branches. --- .github/workflows/integration-v4.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/integration-v4.yml b/.github/workflows/integration-v4.yml index 216b12d..1a0e274 100644 --- a/.github/workflows/integration-v4.yml +++ b/.github/workflows/integration-v4.yml @@ -1,6 +1,10 @@ name: "Integration tests" on: + pull_request: + branches: + - v4 + - master push: branches: - v4 From 92e6e3b5b38ba8935286ac3b74be441f6bdb26f2 Mon Sep 17 00:00:00 2001 From: mesilov Date: Wed, 22 May 2024 19:43:41 +0600 Subject: [PATCH 3/6] Update linting targets and integrate Rector In this commit, linting targets in the Makefile were updated and Rector was added as a new one. Additionally, package "rector/rector" was added to composer.json dependencies and the workflow was updated to accommodate the new PHPStan linting target name. --- .github/workflows/phpstan.yml | 3 ++- Makefile | 8 ++++++-- composer.json | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 471afeb..4c17036 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -14,6 +14,7 @@ jobs: matrix: php-version: - "8.3" + - "8.2" dependencies: [ highest ] steps: @@ -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() }} diff --git a/Makefile b/Makefile index dcfbe70..f65f1d7 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/composer.json b/composer.json index c677ad0..8258ea4 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,6 @@ "loyalty program", "bonus cards", "loyalty program SDK", - "b24io", "REST-API", "Bitrix24" ], @@ -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": { From 1586844aade15583930c7a9eef39f9b7135c082a Mon Sep 17 00:00:00 2001 From: B24io Date: Wed, 22 May 2024 19:55:04 +0600 Subject: [PATCH 4/6] Add rector configuration file This commit introduces a new rector.php file to manage the rector configuration. With this update, paths to the /src and /tests directories are defined, PHP sets are specified, and a rule to add void return types to methods with no return is set up. Signed-off-by: B24io --- rector.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 rector.php diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..bd97da9 --- /dev/null +++ b/rector.php @@ -0,0 +1,19 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withPhpSets( + false, // 8.3 + true // 8.2 + ) + ->withRules([ + AddVoidReturnTypeWhereNoReturnRector::class, + ]); From 1c6d26b6d5041348441bcc5387739db239512106 Mon Sep 17 00:00:00 2001 From: B24io Date: Wed, 22 May 2024 19:57:50 +0600 Subject: [PATCH 5/6] Refactor code to use PHP 8.0 features and improve README Performed housekeeping tasks on the codebase. Refactored existing code to make use of PHP 8.0 features such as constructor property promotion, match expression, and get_debug_type. Enhanced README.md to include more comprehensive package information, and updated the SDK version in ApiClient. Added support for viewing turnovers and card level in card and contact results. Signed-off-by: B24io --- CHANGELOG.md | 15 +++++++- README.md | 14 ++++---- .../Transactions/BulkTransactions.php | 8 ++--- src/Commands/Transactions/BurnBonuses.php | 8 ++--- .../Transactions/LoadTransactionsFromFile.php | 8 ++--- .../Result/Contacts/ContactItemResult.php | 14 ++++++++ .../Transactions/TransactionItemResult.php | 25 ++++++------- .../Result/Turnovers/TurnoversItemResult.php | 19 +++++----- src/Core/ApiClient.php | 15 ++++---- src/Core/Core.php | 12 ++----- src/Core/Exceptions/BadRequestException.php | 5 +-- .../InternalServerErrorException.php | 5 +-- src/Core/Result/AbstractItem.php | 36 +++++++------------ src/Core/Result/AbstractResult.php | 5 +-- src/Services/AbstractService.php | 6 +--- src/Services/AbstractServiceBuilder.php | 10 ++---- src/Services/Admin/Cards/CardsFetcher.php | 4 +-- .../Services/Admin/Contacts/ContactsTest.php | 3 ++ 18 files changed, 97 insertions(+), 115 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c80fa3..5a637e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,22 @@ # loyalty-php-sdk ## 4.1.0 (2024.05.22) -* add `Turnovers` for `CardItemResult`, contains: +* 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 diff --git a/README.md b/README.md index 4138ee2..a90535f 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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
version | support status | build
status | REST-API
version | PHP
versions | +|------------------------------|-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------|-------------------| +| **v4.x** | 🟩 **active** | ![integration tests](https://github.com/b24io/loyalty-php-sdk/actions/workflows/integration-v4.yml/badge.svg?branch=v4)
![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)
![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 diff --git a/src/Commands/Transactions/BulkTransactions.php b/src/Commands/Transactions/BulkTransactions.php index a1816cf..66bd7ee 100644 --- a/src/Commands/Transactions/BulkTransactions.php +++ b/src/Commands/Transactions/BulkTransactions.php @@ -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(); } @@ -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; diff --git a/src/Commands/Transactions/BurnBonuses.php b/src/Commands/Transactions/BurnBonuses.php index 6d27355..b8b2239 100644 --- a/src/Commands/Transactions/BurnBonuses.php +++ b/src/Commands/Transactions/BurnBonuses.php @@ -34,7 +34,9 @@ class BurnBonuses extends Command { private const string REASON_ID = 'b24io.loyalty.sdk.cli.util'; - public function __construct(private LoggerInterface $logger) + public function __construct( + private readonly LoggerInterface $logger + ) { parent::__construct(); } @@ -167,9 +169,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)) { diff --git a/src/Commands/Transactions/LoadTransactionsFromFile.php b/src/Commands/Transactions/LoadTransactionsFromFile.php index fa7f157..b26f362 100644 --- a/src/Commands/Transactions/LoadTransactionsFromFile.php +++ b/src/Commands/Transactions/LoadTransactionsFromFile.php @@ -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(); @@ -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)); diff --git a/src/Common/Result/Contacts/ContactItemResult.php b/src/Common/Result/Contacts/ContactItemResult.php index 73ce845..ab8e078 100644 --- a/src/Common/Result/Contacts/ContactItemResult.php +++ b/src/Common/Result/Contacts/ContactItemResult.php @@ -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; @@ -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 { @@ -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); } diff --git a/src/Common/Result/Transactions/TransactionItemResult.php b/src/Common/Result/Transactions/TransactionItemResult.php index 5fa28e8..62b5e2e 100644 --- a/src/Common/Result/Transactions/TransactionItemResult.php +++ b/src/Common/Result/Transactions/TransactionItemResult.php @@ -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), + }; } } \ No newline at end of file diff --git a/src/Common/Result/Turnovers/TurnoversItemResult.php b/src/Common/Result/Turnovers/TurnoversItemResult.php index 7a6a3d4..543d79e 100644 --- a/src/Common/Result/Turnovers/TurnoversItemResult.php +++ b/src/Common/Result/Turnovers/TurnoversItemResult.php @@ -16,16 +16,13 @@ class TurnoversItemResult extends AbstractItem { public function __get(int|string $offset) { - switch ($offset) { - case 'totalPurchasesCount': - return (int)$this->data['total_purchases_count']; - case 'totalPurchasesSum': - return $this->decimalMoneyParser->parse( - $this->data['total_purchases_sum']['amount'], - new Currency($this->data['total_purchases_sum']['currency'] ?? '') - ); - default: - return parent::__get($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), + }; } } \ No newline at end of file diff --git a/src/Core/ApiClient.php b/src/Core/ApiClient.php index abc5ae2..2c6d7d4 100644 --- a/src/Core/ApiClient.php +++ b/src/Core/ApiClient.php @@ -15,21 +15,18 @@ class ApiClient implements ApiClientInterface { - protected HttpClientInterface $client; - protected LoggerInterface $logger; - protected Credentials\Credentials $credentials; - protected const SDK_VERSION = '3.0.0'; + protected const SDK_VERSION = '4.1.0'; protected const SDK_USER_AGENT = 'b24io-loyalty-php-sdk'; - public function __construct(Credentials\Credentials $credentials, HttpClientInterface $client, LoggerInterface $logger) + public function __construct( + protected Credentials\Credentials $credentials, + protected HttpClientInterface $client, + protected LoggerInterface $logger) { - $this->credentials = $credentials; - $this->client = $client; - $this->logger = $logger; $this->logger->debug( 'ApiClient.init', [ - 'httpClientType' => get_class($client), + 'httpClientType' => $this->client::class, ] ); } diff --git a/src/Core/Core.php b/src/Core/Core.php index fc360a4..bec196c 100644 --- a/src/Core/Core.php +++ b/src/Core/Core.php @@ -21,16 +21,10 @@ class Core implements CoreInterface { - protected ApiClientInterface $apiClient; - protected LoggerInterface $logger; - public function __construct( - ApiClientInterface $apiClient, - LoggerInterface $logger - ) + protected ApiClientInterface $apiClient, + protected LoggerInterface $logger) { - $this->apiClient = $apiClient; - $this->logger = $logger; } /** @@ -203,7 +197,7 @@ public function call(Command $cmd): Response 'call.unknownException', [ 'message' => $exception->getMessage(), - 'exceptionType' => get_class($exception), + 'exceptionType' => $exception::class, 'trace' => $exception->getTrace(), ] ); diff --git a/src/Core/Exceptions/BadRequestException.php b/src/Core/Exceptions/BadRequestException.php index f5d40c7..b491df5 100644 --- a/src/Core/Exceptions/BadRequestException.php +++ b/src/Core/Exceptions/BadRequestException.php @@ -9,17 +9,14 @@ class BadRequestException extends BaseException { - protected ApiProblem $apiProblem; - public function __construct( - ApiProblem $apiProblem, + protected ApiProblem $apiProblem, string $message = "", int $code = 0, ?Throwable $previous = null, ) { parent::__construct($message, $code, $previous); - $this->apiProblem = $apiProblem; } public function getApiProblem(): ApiProblem diff --git a/src/Core/Exceptions/InternalServerErrorException.php b/src/Core/Exceptions/InternalServerErrorException.php index 18ac772..21eb035 100644 --- a/src/Core/Exceptions/InternalServerErrorException.php +++ b/src/Core/Exceptions/InternalServerErrorException.php @@ -9,17 +9,14 @@ class InternalServerErrorException extends BaseException { - protected ApiProblem $apiProblem; - public function __construct( - ApiProblem $apiProblem, + protected ApiProblem $apiProblem, string $message = "", int $code = 0, ?Throwable $previous = null, ) { parent::__construct($message, $code, $previous); - $this->apiProblem = $apiProblem; } public function getApiProblem(): ApiProblem diff --git a/src/Core/Result/AbstractItem.php b/src/Core/Result/AbstractItem.php index 8365b89..60af3b4 100644 --- a/src/Core/Result/AbstractItem.php +++ b/src/Core/Result/AbstractItem.php @@ -20,18 +20,13 @@ */ abstract class AbstractItem implements IteratorAggregate { - /** - * @var array - */ - protected array $data; protected DecimalMoneyParser $decimalMoneyParser; /** * @param array $data */ - public function __construct(array $data) + public function __construct(protected array $data) { - $this->data = $data; $this->decimalMoneyParser = new DecimalMoneyParser(new ISOCurrencies()); } @@ -48,29 +43,22 @@ public function __isset(int|string $offset): bool */ public function __get(int|string $offset) { - switch ($offset) { - case 'id': - return Uuid::fromString($this->data[$offset]); - case 'externalId': - return (string)$this->data['external_id']; - case 'created': - case 'modified': - return new DateTimeImmutable($this->data[$offset]); - case 'reason': - return new Reason( - $this->data[$offset]['id'], - $this->data[$offset]['code'], - $this->data[$offset]['comment'] - ); - default: - return $this->data[$offset] ?? null; - } + return match ($offset) { + 'id' => Uuid::fromString($this->data[$offset]), + 'externalId' => (string)$this->data['external_id'], + 'created', 'modified' => new DateTimeImmutable($this->data[$offset]), + 'reason' => new Reason( + $this->data[$offset]['id'], + $this->data[$offset]['code'], + $this->data[$offset]['comment'] + ), + default => $this->data[$offset] ?? null, + }; } /** * @param int|string $offset - * @param mixed $value * * @return void * @throws ImmutableResultViolationException diff --git a/src/Core/Result/AbstractResult.php b/src/Core/Result/AbstractResult.php index c0ab813..5a736e1 100644 --- a/src/Core/Result/AbstractResult.php +++ b/src/Core/Result/AbstractResult.php @@ -8,11 +8,8 @@ abstract class AbstractResult { - protected Response $coreResponse; - - public function __construct(Response $coreResponse) + public function __construct(protected Response $coreResponse) { - $this->coreResponse = $coreResponse; } public function getCoreResponse(): Response diff --git a/src/Services/AbstractService.php b/src/Services/AbstractService.php index b4b0874..e1be4d8 100644 --- a/src/Services/AbstractService.php +++ b/src/Services/AbstractService.php @@ -12,15 +12,11 @@ abstract class AbstractService { - public readonly CoreInterface $core; - protected readonly LoggerInterface $log; protected readonly DecimalMoneyFormatter $decimalMoneyFormatter; protected readonly PhoneNumberUtil $phoneNumberUtil; - public function __construct(CoreInterface $core, LoggerInterface $log) + public function __construct(public readonly CoreInterface $core, protected readonly LoggerInterface $log) { - $this->core = $core; - $this->log = $log; $this->decimalMoneyFormatter = new DecimalMoneyFormatter(new ISOCurrencies()); $this->phoneNumberUtil = PhoneNumberUtil::getInstance(); } diff --git a/src/Services/AbstractServiceBuilder.php b/src/Services/AbstractServiceBuilder.php index 5688d17..44b7a77 100644 --- a/src/Services/AbstractServiceBuilder.php +++ b/src/Services/AbstractServiceBuilder.php @@ -10,18 +10,12 @@ abstract class AbstractServiceBuilder { - protected CoreInterface $core; - protected LoggerInterface $log; /** * @var array */ protected array $serviceCache; - public function __construct( - CoreInterface $core, - LoggerInterface $log - ) { - $this->core = $core; - $this->log = $log; + public function __construct(protected CoreInterface $core, protected LoggerInterface $log) + { } } \ No newline at end of file diff --git a/src/Services/Admin/Cards/CardsFetcher.php b/src/Services/Admin/Cards/CardsFetcher.php index bbc8ce3..3cf8117 100644 --- a/src/Services/Admin/Cards/CardsFetcher.php +++ b/src/Services/Admin/Cards/CardsFetcher.php @@ -13,8 +13,8 @@ class CardsFetcher { public function __construct( - private Cards $cards, - private LoggerInterface $logger + private readonly Cards $cards, + private readonly LoggerInterface $logger ) { } diff --git a/tests/Integration/Services/Admin/Contacts/ContactsTest.php b/tests/Integration/Services/Admin/Contacts/ContactsTest.php index 1d90fe5..3e4b3c4 100644 --- a/tests/Integration/Services/Admin/Contacts/ContactsTest.php +++ b/tests/Integration/Services/Admin/Contacts/ContactsTest.php @@ -56,6 +56,9 @@ public function testAdd(): void StatusCodeInterface::STATUS_OK, $addedContact->getCoreResponse()->httpResponse->getStatusCode() ); + $this->assertNull($addedContact->getContact()->turnovers); + $this->assertNull($addedContact->getContact()->card); + $this->assertNull($addedContact->getContact()->cardLevel); } /** From 07b24a476f16708c5e0215ef7e31f93e854841ee Mon Sep 17 00:00:00 2001 From: B24io Date: Wed, 22 May 2024 20:13:13 +0600 Subject: [PATCH 6/6] Downgrade PHP version and refactor code The codebase has been modified for compatibility with PHP 8.2. A DowngradeLevelSetList was imported and utilized in rector.php to downgrade the PHP version. In BurnBonuses.php, the REASON_ID constant was refactored for clarity. Signed-off-by: B24io --- rector.php | 7 +++++-- src/Commands/Transactions/BurnBonuses.php | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/rector.php b/rector.php index bd97da9..48f25ed 100644 --- a/rector.php +++ b/rector.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Rector\Config\RectorConfig; +use Rector\Set\ValueObject\DowngradeLevelSetList; use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector; return RectorConfig::configure() @@ -10,9 +11,11 @@ __DIR__ . '/src', __DIR__ . '/tests', ]) + ->withSets( + [DowngradeLevelSetList::DOWN_TO_PHP_82] + ) ->withPhpSets( - false, // 8.3 - true // 8.2 + php82: true // 8.2 ) ->withRules([ AddVoidReturnTypeWhereNoReturnRector::class, diff --git a/src/Commands/Transactions/BurnBonuses.php b/src/Commands/Transactions/BurnBonuses.php index b8b2239..908e764 100644 --- a/src/Commands/Transactions/BurnBonuses.php +++ b/src/Commands/Transactions/BurnBonuses.php @@ -32,7 +32,10 @@ description: 'Burning of bonuses accrued before the specified date')] class BurnBonuses extends Command { - private const string REASON_ID = 'b24io.loyalty.sdk.cli.util'; + /** + * @var string + */ + private const REASON_ID = 'b24io.loyalty.sdk.cli.util'; public function __construct( private readonly LoggerInterface $logger