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

Add math calculator. Revise Rateable interface #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3.1"
services:
php-cli:
build: .docker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

declare(strict_types=1);

namespace Freemium\Event;
namespace Freemium\Application\Event;

abstract class DomainEvent
{
public const NAME = 'default';

public function getName(): string
{
return static::NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Freemium\Event;
namespace Freemium\Application\Event;

class EventProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Freemium\Command;
namespace Freemium\Application\UseCase;

use Freemium\Event\EventProvider;
use Freemium\Application\Event\EventProvider;

abstract class AbstractCommandHandler
{
Expand All @@ -13,7 +13,7 @@ public function __construct(EventProvider $eventProvider)
$this->eventProvider = $eventProvider;
}

public function getEventProvider()
public function getEventProvider(): EventProvider
{
return $this->eventProvider;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Freemium\Command\ChangePlan;
namespace Freemium\Application\UseCase\ChangePlan;

use Freemium\Subscription;
use Freemium\SubscriptionPlan;
use Freemium\Domain\Subscription;
use Freemium\Domain\SubscriptionPlan;

class ChangePlan
{
Expand All @@ -21,12 +21,12 @@ public function __construct(
$this->subscriptionPlan = $plan;
}

public function getSubscriptionPlan()
public function getSubscriptionPlan(): SubscriptionPlan
{
return $this->subscriptionPlan;
}

public function getSubscription()
public function getSubscription(): Subscription
{
return $this->subscription;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Freemium\Command\ChangePlan;
namespace Freemium\Application\UseCase\ChangePlan;

use Throwable;
use Freemium\Event\EventProvider;
use Freemium\Command\AbstractCommandHandler;
use Freemium\Repository\SubscriptionRepository;
use Freemium\Application\Event\EventProvider;
use Freemium\Domain\Repository\SubscriptionRepository;
use Freemium\Application\UseCase\AbstractCommandHandler;

class ChangePlanHandler extends AbstractCommandHandler
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Freemium\Command\ChangePlan\Event;
namespace Freemium\Application\UseCase\ChangePlan\Event;

use Freemium\Subscription;
use Freemium\Event\DomainEvent;
use Freemium\Domain\Subscription;
use Freemium\Application\Event\DomainEvent;

class SubscriptionChanged extends DomainEvent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Freemium\Command\ChangePlan\Event;
namespace Freemium\Application\UseCase\ChangePlan\Event;

use Throwable;
use Freemium\Subscription;
use Freemium\SubscriptionPlan;
use Freemium\Event\DomainEvent;
use Freemium\Domain\Subscription;
use Freemium\Domain\SubscriptionPlan;
use Freemium\Application\Event\DomainEvent;

class SubscriptionNotChanged extends DomainEvent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Freemium\Command\ChargeSubscription;
namespace Freemium\Application\UseCase\ChargeSubscription;

use Freemium\Subscription;
use Freemium\Domain\Subscription;

class ChargeSubscription
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

declare(strict_types=1);

namespace Freemium\Command\ChargeSubscription;
namespace Freemium\Application\UseCase\ChargeSubscription;

use Freemium\Freemium;
use Freemium\Event\EventProvider;
use Freemium\Gateways\GatewayInterface;
use Freemium\Command\AbstractCommandHandler;
use Freemium\Repository\SubscriptionRepository;
use RuntimeException;
use Freemium\Domain\Subscription;
use Freemium\Application\Event\DomainEvent;
use Freemium\Application\Event\EventProvider;
use Freemium\Domain\Gateways\GatewayInterface;
use Freemium\Domain\Repository\SubscriptionRepository;
use Freemium\Application\UseCase\AbstractCommandHandler;
use Freemium\Application\UseCase\ChargeSubscription\Event\SubscriptionPayFailed;

class ChargeSubscriptionHandler extends AbstractCommandHandler
{
Expand Down Expand Up @@ -36,26 +39,48 @@ public function handle(ChargeSubscription $command): void
{
$subscription = $command->getSubscription();

if ($subscription->getSubscribable()->getBillingKey() === null) {
throw new RuntimeException('Customer does not have a billing key setup');
}

$response = $this->gateway->charge(
$subscription->rate(),
$subscription->getSubscribable()->getBillingKey()
);

$transaction = $subscription->createTransaction($response);

$event = new Event\SubscriptionPaid($subscription);
if ($transaction->isSuccess()) {
$subscription->receivePayment();
} elseif ($subscription->isExpired()) {
$event = new Event\SubscriptionPaid($subscription);

$this->finalize($subscription, $event);
return;
}

if ($subscription->isExpired()) {
$subscription->expireNow();
$event = new Event\SubscriptionExpired($subscription);
} elseif (!$subscription->isInGrace()) {

$this->finalize($subscription, $event);
return;
}

if (!$subscription->isInGrace()) {
$subscription->expireAfterGrace();
$event = new Event\SubscriptionGraced($subscription);

$this->finalize($subscription, $event);
return;
}

$this->repository->update($subscription);
$event = new SubscriptionPayFailed($subscription);
$this->finalize($subscription, $event);
}

private function finalize(Subscription $subscription, DomainEvent $event): void
{
$this->repository->update($subscription);
$this->getEventProvider()->raise($event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Freemium\Command\ChargeSubscription\Event;
namespace Freemium\Application\UseCase\ChargeSubscription\Event;

use Freemium\Subscription;
use Freemium\Event\DomainEvent;
use Freemium\Domain\Subscription;
use Freemium\Application\Event\DomainEvent;

class SubscriptionExpired extends DomainEvent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Freemium\Command\ChargeSubscription\Event;
namespace Freemium\Application\UseCase\ChargeSubscription\Event;

use Freemium\Subscription;
use Freemium\Event\DomainEvent;
use Freemium\Domain\Subscription;
use Freemium\Application\Event\DomainEvent;

class SubscriptionGraced extends DomainEvent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Freemium\Command\ChargeSubscription\Event;
namespace Freemium\Application\UseCase\ChargeSubscription\Event;

use Freemium\Subscription;
use Freemium\Event\DomainEvent;
use Freemium\Domain\Subscription;
use Freemium\Application\Event\DomainEvent;

class SubscriptionPaid extends DomainEvent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Freemium\Application\UseCase\ChargeSubscription\Event;

use Freemium\Domain\Subscription;
use Freemium\Application\Event\DomainEvent;

class SubscriptionPayFailed extends DomainEvent
{
public const NAME = 'subscription.paid_failed';

private $subscription;

public function __construct(Subscription $subscription)
{
$this->subscription = $subscription;
}

public function getSubscription()
{
return $this->subscription;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Freemium\Command;
namespace Freemium\Application\UseCase;

use Freemium\Event\EventProvider;
use Freemium\Application\Event\EventProvider;

class CommandBus
{
Expand All @@ -20,12 +20,12 @@ public function __construct(
$this->eventProvider = $eventProvider;
}

public function handle($command)
public function handle(object $command)
{
return $this->resolveHandler($command)->handle($command);
}

private function resolveHandler($command)
private function resolveHandler(object $command)
{
$resolver = $this->resolver;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Freemium\Command\CreateSubscription\Event;
namespace Freemium\Application\UseCase\CreateSubscription\Event;

use Freemium\Subscription;
use Freemium\Event\DomainEvent;
use Freemium\Domain\Subscription;
use Freemium\Application\Event\DomainEvent;

class SubscriptionCreated extends DomainEvent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Freemium\Application\UseCase\CreateSubscription;

class NewSubscription
{
public function __construct(
private readonly string $customerId,
private readonly string $subscriptionPlan
) {
}

public function getCustomerId(): string
{
return $this->customerId;
}

public function getSubscriptionPlan(): string
{
return $this->subscriptionPlan;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Freemium\Application\UseCase\CreateSubscription;

use Freemium\Domain\Subscription;
use Freemium\Application\Event\EventProvider;
use Freemium\Domain\Repository\SubscribableRepository;
use Freemium\Domain\Repository\SubscriptionRepository;
use Freemium\Application\UseCase\AbstractCommandHandler;
use Freemium\Domain\Repository\SubscriptionPlanRepository;

class NewSubscriptionHandler extends AbstractCommandHandler
{
public function __construct(
EventProvider $eventProvider,
private readonly SubscriptionRepository $repository,
private readonly SubscribableRepository $subscribableRepository,
private readonly SubscriptionPlanRepository $subscriptionPlanRepository
) {
parent::__construct($eventProvider);
}

public function handle(NewSubscription $command): void
{
$subscribable = $this->subscribableRepository->findByCustomerId($command->getCustomerId());
$subscriptionPlan = $this->subscriptionPlanRepository->findByName($command->getSubscriptionPlan());

$subscription = new Subscription(
$subscribable,
$subscriptionPlan
);

$this->repository->insert($subscription);

$this->getEventProvider()->raise(new Event\SubscriptionCreated($subscription));
}
}
Loading