Skip to content

Commit

Permalink
Reorganize hierarchy of dispatcher classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Nov 27, 2020
1 parent 464fa3a commit 16865d4
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 91 deletions.
23 changes: 0 additions & 23 deletions app/services/solidus_subscriptions/dispatcher.rb

This file was deleted.

25 changes: 25 additions & 0 deletions app/services/solidus_subscriptions/dispatcher/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module SolidusSubscriptions
module Dispatcher
class Base
attr_reader :installments, :order

# Returns a new instance of this dispatcher.
#
# @param installments [Array<SolidusSubscriptions::Installment>] The installments to process
# with this dispatcher
# @param order [Spree::Order] The order that was generated as a result of these installments
#
# @return [SolidusSubscriptions::Dispatcher]
def initialize(installments, order = nil)
@installments = installments
@order = order
end

def dispatch
raise NotImplementedError
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

# Handles failed installments.
module SolidusSubscriptions
module Dispatcher
class FailureDispatcher < Base
def dispatch
order.touch(:completed_at)
order.cancel
installments.each do |installment|
installment.failed!(order)
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

# Handles installments that cannot be processed for lack of stock.
module SolidusSubscriptions
class OutOfStockDispatcher < Dispatcher
def dispatch
installments.each(&:out_of_stock)
module Dispatcher
class OutOfStockDispatcher < Base
def dispatch
installments.each(&:out_of_stock)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

# Handles payment failures for subscription installments.
module SolidusSubscriptions
module Dispatcher
class PaymentFailedDispatcher < Base
def dispatch
order.touch(:completed_at)
order.cancel
installments.each do |installment|
installment.payment_failed!(order)
end

::Spree::Event.fire(
'solidus_subscriptions.installments_failed_payment',
installments: installments,
order: order,
)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

# Handles installments that are processed successfully.
module SolidusSubscriptions
module Dispatcher
class SuccessDispatcher < Base
def dispatch
installments.each do |installment|
installment.success!(order)
end

::Spree::Event.fire(
'solidus_subscriptions.installments_succeeded',
installments: installments,
order: order,
)
end
end
end
end
14 changes: 0 additions & 14 deletions app/services/solidus_subscriptions/failure_dispatcher.rb

This file was deleted.

20 changes: 0 additions & 20 deletions app/services/solidus_subscriptions/payment_failed_dispatcher.rb

This file was deleted.

18 changes: 0 additions & 18 deletions app/services/solidus_subscriptions/success_dispatcher.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
# subclass from the the dispatcher you are replacing and call `super` from within `#dispatch`.

# This handler is called when a subscription order is successfully placed.
# config.success_dispatcher_class = 'SolidusSubscriptions::SuccessDispatcher'
# config.success_dispatcher_class = 'SolidusSubscriptions::Dispatcher::SuccessDispatcher'

# This handler is called when a group of installments fails to be processed.
# config.failure_dispatcher_class = 'SolidusSubscriptions::FailureDispatcher'
# config.failure_dispatcher_class = 'SolidusSubscriptions::Dispatcher::FailureDispatcher'

# This handler is called when a payment fails on a subscription order.
# config.payment_failed_dispatcher_class = 'SolidusSubscriptions::PaymentFailedDispatcher'
# config.payment_failed_dispatcher_class = 'SolidusSubscriptions::Dispatcher::PaymentFailedDispatcher'

# This handler is called when there isn't enough stock to fulfill an installment.
# config.out_of_stock_dispatcher = 'SolidusSubscriptions::OutOfStockDispatcher'
# config.out_of_stock_dispatcher = 'SolidusSubscriptions::Dispatcher::OutOfStockDispatcher'

# ===================================== Permitted attributes =====================================
#
Expand Down
8 changes: 4 additions & 4 deletions lib/solidus_subscriptions/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ class Configuration
)

def success_dispatcher_class
@success_dispatcher_class ||= 'SolidusSubscriptions::SuccessDispatcher'
@success_dispatcher_class ||= 'SolidusSubscriptions::Dispatcher::SuccessDispatcher'
@success_dispatcher_class.constantize
end

def failure_dispatcher_class
@failure_dispatcher_class ||= 'SolidusSubscriptions::FailureDispatcher'
@failure_dispatcher_class ||= 'SolidusSubscriptions::Dispatcher::FailureDispatcher'
@failure_dispatcher_class.constantize
end

def payment_failed_dispatcher_class
@payment_failed_dispatcher_class ||= 'SolidusSubscriptions::PaymentFailedDispatcher'
@payment_failed_dispatcher_class ||= 'SolidusSubscriptions::Dispatcher::PaymentFailedDispatcher'
@payment_failed_dispatcher_class.constantize
end

def out_of_stock_dispatcher_class
@out_of_stock_dispatcher_class ||= 'SolidusSubscriptions::OutOfStockDispatcher'
@out_of_stock_dispatcher_class ||= 'SolidusSubscriptions::Dispatcher::OutOfStockDispatcher'
@out_of_stock_dispatcher_class.constantize
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe SolidusSubscriptions::Dispatcher do
RSpec.describe SolidusSubscriptions::Dispatcher::Base do
describe '#dispatch' do
it 'raises a NotImplementedError' do
dispatcher = described_class.new([])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe SolidusSubscriptions::FailureDispatcher do
RSpec.describe SolidusSubscriptions::Dispatcher::FailureDispatcher do
describe '#dispatch' do
it 'marks all the installments as failed' do
installments = Array.new(2) { instance_spy(SolidusSubscriptions::Installment) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe SolidusSubscriptions::OutOfStockDispatcher do
RSpec.describe SolidusSubscriptions::Dispatcher::OutOfStockDispatcher do
describe '#dispatch' do
it 'marks all the installments as out of stock' do
installments = Array.new(2) { instance_spy(SolidusSubscriptions::Installment) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe SolidusSubscriptions::PaymentFailedDispatcher do
RSpec.describe SolidusSubscriptions::Dispatcher::PaymentFailedDispatcher do
describe '#dispatch' do
it 'marks all the installments as payment_failed' do
installments = Array.new(2) { instance_spy(SolidusSubscriptions::Installment) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe SolidusSubscriptions::SuccessDispatcher do
RSpec.describe SolidusSubscriptions::Dispatcher::SuccessDispatcher do
describe '#dispatch' do
it 'marks all the installments as success' do
installments = Array.new(2) { instance_spy(SolidusSubscriptions::Installment) }
Expand Down

0 comments on commit 16865d4

Please sign in to comment.