Skip to content

Commit

Permalink
Merge pull request #12 from justbetter/feature/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
VincentBean authored Aug 5, 2024
2 parents de532b8 + 4118f7d commit 1e6ae06
Show file tree
Hide file tree
Showing 104 changed files with 1,969 additions and 1,919 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/analyse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ jobs:
matrix:
os: [ubuntu-latest]
php: [8.3]
laravel: [10.*, 11.*]
laravel: [11.*]
stability: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*

Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ jobs:
matrix:
os: [ubuntu-latest]
php: [8.3]
laravel: [10.*, 11.*]
laravel: [11.*]
stability: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*

Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2, 8.3]
laravel: [10.*, 11.*]
php: [8.2, 8.3]
laravel: [11.*]
stability: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 11.*
testbench: 9.*
exclude:
Expand Down
125 changes: 104 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ This package can:
- Only update prices in Magento when are modified. i.e. when you retrieve the same price ten times it only updates once to Magento
- Automatically stop syncing when updating fails
- Logs activities using [Spatie activitylog](https://github.com/spatie/laravel-activitylog)
- Logs errors using [JustBetter Error Logger](https://github.com/justbetter/laravel-error-logger)
- Checks if Magento products exist using [JustBetter Magento Products](https://github.com/justbetter/laravel-magento-products)

> Check out [Laravel Magento Prices](https://github.com/justbetter/laravel-magento-prices) for connecting regular prices to Magneto
Expand Down Expand Up @@ -61,49 +60,133 @@ Add the following commands to your scheduler:

protected function schedule(Schedule $schedule): void
{
$schedule->command(\JustBetter\MagentoCustomerPrices\Commands\SyncCustomerPricesCommand::class)->everyMinute();
$schedule->command(\JustBetter\MagentoCustomerPrices\Commands\ProcessCustomerPricesCommand::class)->everyMinute();

// Retrieve all customer prices weekly
$schedule->command(\JustBetter\MagentoCustomerPrices\Commands\RetrieveAllCustomerPricesCommand::class)->weekly();
// Retrieve all customer prices daily
$schedule->command(\JustBetter\MagentoCustomerPrices\Commands\Retrieval\RetrieveAllCustomerPricesCommand::class)->daily();

// Retrieve updated customer prices daily
$schedule->command(\JustBetter\MagentoCustomerPrices\Commands\RetrieveUpdatedCustomerPricesCommand::class)->daily();
// Retrieve updated customer prices hourly
$schedule->command(\JustBetter\MagentoCustomerPrices\Commands\Retriavel\RetrieveAllCustomerPricesCommand::class, ['from' => 'now - 1 hour'])->hourly();
}
```

### Retrieving Customer Prices

To retrieve prices you have to write a retriever.
A retriever is a class that extends the `\JustBetter\MagentoCustomerPrices\Retriever\CustomerPriceRetriever` class.
This package works with a repository that retrieves prices per SKU which you have to implement.

You'll be required to write three methods:
#### retrieve(string $sku)
#### Repository

Must return an enumerable of `\JustBetter\MagentoCustomerPrices\Data\CustomerPriceData` objects
This class is responsible for retrieving prices for products, retrieving sku's and settings.
Your class must extend `\JustBetter\MagentoCustomerPrices\Repository\Repository` and implement the `retrieve` method.
If there is no price for the SKU you may return `null`. In all other cases you need to return a `CustomerPriceData` object which contains two elements:
- `sku` Required
- `prices` Optional, array of customer prices
- `price` float of the price
- `customer_id` Magento 2 customer id
- `quantity` Minimum quantity

#### retrieveAllSkus()
You can view the rules in the `CustomerPriceData` class to get an idea of what you need to provide.

Must return an enumerable of strings
##### Example

#### retrieveUpdatedSkus()
```php

<?php

namespace App\Integrations\MagentoCustomerPrices;

use JustBetter\MagentoCustomerPrices\Data\CustomerPriceData;
use JustBetter\MagentoCustomerPrices\Repository\Repository;

class MyCustomerPriceRepository extends Repository
{
public function retrieve(string $sku): ?CustomerPriceData
{
return CustomerPriceData::of([
'sku' => $sku,
'prices' => [
[
'customer_id' => 1,
'price' => 10,
'quantity' => 1,
],
[
'customer_id' => 1,
'price' => 8,
'quantity' => 10,
],
],
]);
}
}
```

### Retrieving SKU's

By default, the `Repository` that you are extending will retrieve the SKU's from [justbetter/laravel-magento-products](https://github.com/justbetter/laravel-magento-products).
If you wish to use this you have to add the commands to your scheduler to automatically import products.

If you have another source for your SKU's you may implement the `skus` method yourself.
It accepts an optional carbon instance to only retrieve modified stock.

```php
<?php

namespace App\Integrations\MagentoCustomerPrices;

use JustBetter\MagentoCustomerPrices\Repositories\Repository;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

Must return an enumerable of strings
class MyCustomerPriceRepository implements Repository
{
public function skus(?Carbon $from = null): Collection
{
return collect(['sku_1', 'sku_2']);
}
}
```

### Configuring the repository

The repository class has a couple of settings that you can adjust:

```php
class BaseRepository
{
// How many prices may be retrieved at once when the process job runs
protected int $retrieveLimit = 250;

// How many prices may be updated at once when the process job runs
protected int $updateLimit = 250;

#### Example
See the `\JustBetter\MagentoCustomerPrices\Retriever\DummyCustomerPriceRetriever` class for an example.
// How many times an update to Magento may fail before it stops trying
protected int $failLimit = 3;
}
```

After you've created and configured the repository you have to set it in your configuration file:

```php
<?php

return [
'repository' => \App\Integrations\MagentoCustomerPrices\MyPriceRepository::class,
];
```

## Magento 2 Customer Prices

By default this package uses the [JustBetter Magento 2 Customer Pricing](https://github.com/justbetter/magento2-customer-pricing) module for updating prices to Magento.
By default, this package uses the [JustBetter Magento 2 Customer Pricing](https://github.com/justbetter/magento2-customer-pricing) module for updating prices to Magento.
If you use another Magento 2 module for customer specific pricing you can write your own class that updates prices in Magento.
You can do this by implementing `JustBetter\MagentoCustomerPrices\Contracts\UpdatesMagentoCustomerPrices`.
See `\JustBetter\MagentoCustomerPrices\Actions\UpdateCustomerPrices` for an example.
You can do this by implementing `JustBetter\MagentoCustomerPrices\Contracts\Update\UpdatesCustomerPrice`.
See `\JustBetter\MagentoCustomerPrices\Actions\Update\UpdateCustomerPrice` for an example.

Don't forget to bind your own class!
```
<?php
app()->singleton(UpdatesMagentoCustomerPrices::class, YourCustomUpdater::class);
app()->singleton(\JustBetter\MagentoCustomerPrices\Contracts\Update\UpdatesCustomerPrice::class, YourCustomUpdater::class);
```

## Quality
Expand Down
31 changes: 28 additions & 3 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
# Upgrading from 1.x to 2.x
# Upgrade Guide

## Upgrading from 1.x to 2.x

You do not need to modify code in your price/sku retriever.

If you were using the Mageplaza Better Tier Price module you now have to implement your update class yourself or upgrade to the [JustBetter Magento 2 Customer Pricing](https://github.com/justbetter/magento2-customer-pricing) module.
See the readme on how you can implement your own updater class, you can use the default class from version 1.x as a template.
If you were using the Mageplaza Better Tier Price module you now have to implement your update class yourself or upgrade
to the [JustBetter Magento 2 Customer Pricing](https://github.com/justbetter/magento2-customer-pricing) module.
See the readme on how you can implement your own updater class, you can use the default class from version 1.x as a
template.

## Upgrading from 2.x to 3.x

3.x introduces a complete refactor of the package structure.

A few highlights:

- Simplified implementation
- Support updating via Magento 2 bulk async requests
- Removed error logger, replaced with activity log
- Dropped support for Laravel 10

### Update your project

The price retriever and SKU retriever classes all have been merged into a single repository class.
Refer to the readme on how to implement this.

The configuration file has been stripped, most of the configuration is now done in the repository class.

A lot of classes have been renamed, be sure to update your scheduler and check all classes that you use.
The price model has been renamed from `MagentoCustomerPrice` to `CustomerPrice`.
117 changes: 58 additions & 59 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,62 +1,61 @@
{
"name": "justbetter/laravel-magento-customer-prices",
"description": "Package to push customer specific prices to Magento",
"type": "package",
"license": "MIT",
"require": {
"php": "^8.1",
"brick/money": "^0.7|^0.8",
"justbetter/laravel-magento-client": "^2.4",
"justbetter/laravel-magento-products": "^1.4",
"laravel/framework": "^10.0|^11.0",
"spatie/laravel-activitylog": "^4.8"
},
"require-dev": {
"doctrine/dbal": "^3.4",
"larastan/larastan": "^2.9",
"laravel/pint": "^1.15",
"orchestra/testbench": "^8.0|^9.0",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.0"
},
"authors": [
{
"name": "Vincent Boon",
"email": "[email protected]",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"JustBetter\\MagentoCustomerPrices\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"JustBetter\\MagentoCustomerPrices\\Tests\\": "tests"
}
},
"scripts": {
"test": "phpunit",
"analyse": "phpstan",
"style": "pint --test",
"quality": [
"@test",
"@analyse",
"@style"
"name": "justbetter/laravel-magento-customer-prices",
"description": "Package to push customer specific prices to Magento",
"type": "package",
"license": "MIT",
"require": {
"php": "^8.2",
"justbetter/laravel-magento-client": "^2.4",
"justbetter/laravel-magento-async": "^1.0",
"justbetter/laravel-magento-products": "^1.4",
"laravel/framework": "^11.0",
"spatie/laravel-activitylog": "^4.8"
},
"require-dev": {
"larastan/larastan": "^2.9",
"laravel/pint": "^1.15",
"orchestra/testbench": "^9.0",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.0"
},
"authors": [
{
"name": "Vincent Boon",
"email": "[email protected]",
"role": "Developer"
}
],
"fix-style": "pint"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"JustBetter\\MagentoCustomerPrices\\ServiceProvider"
]
}
},
"minimum-stability": "stable",
"prefer-stable": true
"autoload": {
"psr-4": {
"JustBetter\\MagentoCustomerPrices\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"JustBetter\\MagentoCustomerPrices\\Tests\\": "tests"
}
},
"scripts": {
"test": "phpunit",
"analyse": "phpstan",
"style": "pint --test",
"quality": [
"@test",
"@analyse",
"@style"
],
"fix-style": "pint"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"JustBetter\\MagentoCustomerPrices\\ServiceProvider"
]
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
13 changes: 4 additions & 9 deletions config/magento-customer-prices.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
<?php

use Brick\Math\RoundingMode;
use JustBetter\MagentoCustomerPrices\Retriever\DummyCustomerPriceRetriever;

return [
'retriever' => DummyCustomerPriceRetriever::class,
'repository' => \JustBetter\MagentoCustomerPrices\Repository\Repository::class,

/* Queue for the jobs to run on */
'queue' => 'default',

'fail_count' => 5,

'currency' => 'EUR',
'precision' => 4,
'rounding_mode' => RoundingMode::HALF_UP,
/* Send updates using Magento 2's async bulk endpoints, a configured message queue in Magento is required for this */
'async' => false,
];
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use JustBetter\MagentoCustomerPrices\Models\MagentoCustomerPrice;

return new class extends Migration
{
Expand All @@ -17,7 +16,7 @@ public function up(): void

$table->json('prices')->nullable();

$table->string('state')->default(MagentoCustomerPrice::STATE_IDLE);
$table->string('state')->default('idle');

$table->dateTime('last_retrieved')->nullable();
$table->dateTime('last_updated')->nullable();
Expand Down
Loading

0 comments on commit 1e6ae06

Please sign in to comment.