Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Calculator with Pipelines #873

Merged
merged 16 commits into from May 26, 2023
Merged

Refactor Calculator with Pipelines #873

merged 16 commits into from May 26, 2023

Conversation

duncanmcclean
Copy link
Owner

@duncanmcclean duncanmcclean commented May 26, 2023

This pull request refactors the "order calculator" to use Laravel Pipelines, allowing for logic to be better split up rather than being in one big class.

All of the existing tests are passing. I've just had to change how information is fetched from the calculations as we're no longer passing around the $data array like we were before.

The code for this PR was written almost exclusively in CLT/BFS airports on the way back from Flat Camp. ✈️

Change: extending the Calculator

Overriding the Calculator class was documented up until v5.x. This could have been useful if you need to do your own kinds of calculations.

I don't think anyone is doing this from support requests & bug reports. However, if you are, you will need to move any custom logic into a pipeline class, like this one, then override the static calculate method and include your pipe class.

// app/SimpleCommerce/MyCustomCalculator.php

namespace App\SimpleCommerce;

use DoubleThreeDigital\SimpleCommerce\Contracts\Calculator as CalculatorContract;

class MyCustomCalculator implements CalculatorContract
{
    public static function calculate(Order $order): Order
        {
            return Pipeline::send($order)
                ->through([
                    LineItemCalculator::class,
                    LineItemTaxCalculator::class,
                    CalculateItemsTotal::class,
                    CouponCalculator::class,
                    ShippingCalculator::class,
                    CalculateGrandTotal::class,
                    MyCustomCalculations::class, // Added for this example
                ])
                ->thenReturn();
        }
    }
// app/SimpleCommerce/MyCustomCalculations.php

namespace App\SimpleCommerce;

use Closure;
use DoubleThreeDigital\SimpleCommerce\Contracts\Order;

class MyCustomCalculations
{
    public function handle(Order $order, Closure $next)
    {
        // Do whatever you want to here... you can touch the $order if you need to.

        return $next($order);
    }
}

@duncanmcclean duncanmcclean marked this pull request as ready for review May 26, 2023 22:45
@duncanmcclean duncanmcclean merged commit e52eac7 into 5.x May 26, 2023
3 checks passed
@duncanmcclean duncanmcclean deleted the refactor-calculator branch May 26, 2023 22:57
@duncanmcclean duncanmcclean mentioned this pull request May 27, 2023
2 tasks
@github-actions
Copy link

Released as part of v5.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant