diff --git a/app/services/solidus_subscriptions/dispatcher.rb b/app/services/solidus_subscriptions/dispatcher.rb deleted file mode 100644 index 0472f792..00000000 --- a/app/services/solidus_subscriptions/dispatcher.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module SolidusSubscriptions - class Dispatcher - attr_reader :installments, :order - - # Returns a new instance of this dispatcher. - # - # @param installments [Array] 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 diff --git a/app/services/solidus_subscriptions/dispatcher/base.rb b/app/services/solidus_subscriptions/dispatcher/base.rb new file mode 100644 index 00000000..84b87101 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/base.rb @@ -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] 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 diff --git a/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb new file mode 100644 index 00000000..286a4c94 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/failure_dispatcher.rb @@ -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 diff --git a/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb similarity index 52% rename from app/services/solidus_subscriptions/out_of_stock_dispatcher.rb rename to app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb index 05484f4b..ef79f843 100644 --- a/app/services/solidus_subscriptions/out_of_stock_dispatcher.rb +++ b/app/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher.rb @@ -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 diff --git a/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb new file mode 100644 index 00000000..74bee883 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher.rb @@ -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 diff --git a/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb new file mode 100644 index 00000000..78563a45 --- /dev/null +++ b/app/services/solidus_subscriptions/dispatcher/success_dispatcher.rb @@ -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 diff --git a/app/services/solidus_subscriptions/failure_dispatcher.rb b/app/services/solidus_subscriptions/failure_dispatcher.rb deleted file mode 100644 index c77d4b02..00000000 --- a/app/services/solidus_subscriptions/failure_dispatcher.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -# Handles failed installments. -module SolidusSubscriptions - class FailureDispatcher < Dispatcher - def dispatch - order.touch(:completed_at) - order.cancel - installments.each do |installment| - installment.failed!(order) - end - end - end -end diff --git a/app/services/solidus_subscriptions/payment_failed_dispatcher.rb b/app/services/solidus_subscriptions/payment_failed_dispatcher.rb deleted file mode 100644 index 29eb2913..00000000 --- a/app/services/solidus_subscriptions/payment_failed_dispatcher.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -# Handles payment failures for subscription installments. -module SolidusSubscriptions - class PaymentFailedDispatcher < Dispatcher - 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 diff --git a/app/services/solidus_subscriptions/success_dispatcher.rb b/app/services/solidus_subscriptions/success_dispatcher.rb deleted file mode 100644 index ce552662..00000000 --- a/app/services/solidus_subscriptions/success_dispatcher.rb +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -# Handles installments that are processed successfully. -module SolidusSubscriptions - class SuccessDispatcher < Dispatcher - def dispatch - installments.each do |installment| - installment.success!(order) - end - - ::Spree::Event.fire( - 'solidus_subscriptions.installments_succeeded', - installments: installments, - order: order, - ) - end - end -end diff --git a/lib/generators/solidus_subscriptions/install/templates/initializer.rb b/lib/generators/solidus_subscriptions/install/templates/initializer.rb index faf03f4b..31e44b54 100644 --- a/lib/generators/solidus_subscriptions/install/templates/initializer.rb +++ b/lib/generators/solidus_subscriptions/install/templates/initializer.rb @@ -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 ===================================== # diff --git a/lib/solidus_subscriptions/configuration.rb b/lib/solidus_subscriptions/configuration.rb index f09740b7..67526dd3 100644 --- a/lib/solidus_subscriptions/configuration.rb +++ b/lib/solidus_subscriptions/configuration.rb @@ -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 diff --git a/spec/services/solidus_subscriptions/dispatcher_spec.rb b/spec/services/solidus_subscriptions/dispatcher/dispatcher_spec.rb similarity index 79% rename from spec/services/solidus_subscriptions/dispatcher_spec.rb rename to spec/services/solidus_subscriptions/dispatcher/dispatcher_spec.rb index 49c3f4b6..7590422d 100644 --- a/spec/services/solidus_subscriptions/dispatcher_spec.rb +++ b/spec/services/solidus_subscriptions/dispatcher/dispatcher_spec.rb @@ -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([]) diff --git a/spec/services/solidus_subscriptions/failure_dispatcher_spec.rb b/spec/services/solidus_subscriptions/dispatcher/failure_dispatcher_spec.rb similarity index 92% rename from spec/services/solidus_subscriptions/failure_dispatcher_spec.rb rename to spec/services/solidus_subscriptions/dispatcher/failure_dispatcher_spec.rb index a7c18ea5..55a883ac 100644 --- a/spec/services/solidus_subscriptions/failure_dispatcher_spec.rb +++ b/spec/services/solidus_subscriptions/dispatcher/failure_dispatcher_spec.rb @@ -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) } diff --git a/spec/services/solidus_subscriptions/out_of_stock_dispatcher_spec.rb b/spec/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher_spec.rb similarity index 82% rename from spec/services/solidus_subscriptions/out_of_stock_dispatcher_spec.rb rename to spec/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher_spec.rb index 6f3825ca..4ed0a53c 100644 --- a/spec/services/solidus_subscriptions/out_of_stock_dispatcher_spec.rb +++ b/spec/services/solidus_subscriptions/dispatcher/out_of_stock_dispatcher_spec.rb @@ -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) } diff --git a/spec/services/solidus_subscriptions/payment_failed_dispatcher_spec.rb b/spec/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher_spec.rb similarity index 94% rename from spec/services/solidus_subscriptions/payment_failed_dispatcher_spec.rb rename to spec/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher_spec.rb index a6a8a0ca..99794489 100644 --- a/spec/services/solidus_subscriptions/payment_failed_dispatcher_spec.rb +++ b/spec/services/solidus_subscriptions/dispatcher/payment_failed_dispatcher_spec.rb @@ -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) } diff --git a/spec/services/solidus_subscriptions/success_dispatcher_spec.rb b/spec/services/solidus_subscriptions/dispatcher/success_dispatcher_spec.rb similarity index 92% rename from spec/services/solidus_subscriptions/success_dispatcher_spec.rb rename to spec/services/solidus_subscriptions/dispatcher/success_dispatcher_spec.rb index ce41638a..aa1f72fe 100644 --- a/spec/services/solidus_subscriptions/success_dispatcher_spec.rb +++ b/spec/services/solidus_subscriptions/dispatcher/success_dispatcher_spec.rb @@ -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) }