Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class PaymentsFacade extends Container
{
private $requiredKeys = [
'factory.project',
'price-calculator',
'repository.plan',
'repository.coupon',
'service.authorize-card',
'service.buy-project',
'service.renew-project',
'service.upgrade-project',
];
public function __construct(array $values = [])
{
$diff = array_diff_key(array_combine($this->requiredKeys, $this->requiredKeys), $values);
if (0 !== sizeof($diff))
{
throw new \InvalidArgumentException('Required keys are missing: ' . implode(', ', $diff));
}
parent::__construct($values + [
'country-code' => new PolandCountryProvider,
'notification.email' => new NullEmailNotification,
]);
}
public function setEmailNotification(EmailNotificationInterface $emailNotification)
{
$this->offsetSet('notification.email', $emailNotification);
}
public function setCountryCodeProvider(CountryCodeProviderInterface $countryCodeProvider)
{
$this->offsetSet('country-code', $countryCodeProvider);
}
/**
* @return \Metricso\Payments\PaymentPlanInterface[]
*/
public function getAvailablePlans()
{
/** @var PaymentPlanRepositoryInterface $service */
$service = $this->offsetGet('repository.plan');
return $service->all();
}
public function createProject($name, PaymentPlanInterface $plan, $yearly = false, CouponInterface $coupon = null)
{
/** @var ProjectFactory $factory */
$factory = $this->offsetGet('factory.project');
return $factory->createProject($name, $plan, $yearly, $coupon);
}
public function findValidCoupon($code)
{
/** @var CouponRepository $service */
$service = $this->offsetGet('repository.coupon');
return $service->get($code);
}
public function getPlanPrice(PaymentPlanInterface $plan, $yearly = false)
{
/** @var ProductPriceCalculator $service */
$service = $this->offsetGet('price-calculator');
return $service->getPriceGivenDuration($plan, $yearly ? $plan::DURATION_YEAR : $plan::DURATION_MONTH);
}
/**
* @param UserInterface $user
* @param NewCustomerDetailsInterface $customer
*
* @return bool
*/
public function authorizeCard(UserInterface $user, NewCustomerDetailsInterface $customer)
{
/** @var AuthorizeCardService $service */
$service = $this->offsetGet('service.authorize-card');
return $service->authorizeCard($user, $customer);
}
/**
* @param ProjectInterface $project
* @param PaymentPlanInterface $plan
* @param NewCustomerDetailsInterface $customer
*/
public function buyProject(ProjectInterface $project, PaymentPlanInterface $plan, NewCustomerDetailsInterface $customer = null)
{
/** @var BuyProjectService $service */
$service = $this->offsetGet('service.buy-project');
$service->buyProject($project, $plan, $customer);
}
/**
* @param ProjectInterface $project
*/
public function renewProject(ProjectInterface $project)
{
/** @var RenewProjectService $service */
$service = $this->offsetGet('service.renew-project');
$service->renewProject($project);
}
/**
* @param ProjectInterface $project
* @param PaymentPlanInterface $plan
* @param NewCustomerDetailsInterface $customer
*/
public function upgradeProject(ProjectInterface $project, PaymentPlanInterface $plan, NewCustomerDetailsInterface $customer = null)
{
/** @var UpgradeProjectService $service */
$service = $this->offsetGet('service.upgrade-project');
$service->upgradePlan($project, $plan, $customer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.