Skip to content

Instantly share code, notes, and snippets.

@pjedrzejewski
Created February 12, 2013 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjedrzejewski/4773000 to your computer and use it in GitHub Desktop.
Save pjedrzejewski/4773000 to your computer and use it in GitHub Desktop.
Adding domains and hosting services.
<?php
namespace App\Bundle\AppBundle\Resolver;
use App\Bundle\AppBundle\Entity\Domain;
use Doctrine\Common\Persistence\ObjectRepository;
use Sylius\Bundle\CartBundle\Model\CartItemInterface;
use Sylius\Bundle\CartBundle\Resolver\ItemResolverInterface;
use Sylius\Bundle\CartBundle\Resolver\ItemResolvingException;
use Symfony\Component\HttpFoundation\Request;
class CartItemResolver implements ItemResolverInterface
{
private $domainRepository;
private $hostingRepository;
public function __construct(ObjectRepository $domainRepository, ObjectRepository $hostingRepository)
{
$this->domainRepository = $domainRepository;
$this->domainRepository = $domainRepository;
}
public function resolve(CartItemInterface $item, Request $request)
{
if (null !== $hostingId = $request->get('hostingId')) {
return $this->resolveHosting($hostingId, $item, $request);
}
if (null !== $domain = $request->get('domain')) {
return $this->resolveDomain($domain, $item, $request);
}
throw new ItemResolvingException('Error occured while adding item to cart');
}
private function resolveHosting($id, CartItemInterface $item, Request $request)
{
if (!$hosting = $this->hostingRepository->find($id)) {
throw new ItemResolvingException('Requested hosting plan is not available');
}
$item->setHosting($hosting);
$item->setUnitPrice($hosting->getPrice());
return $item;
}
private function resolveDomain($name, CartItemInterface $item, Request $request)
{
$domain = $this->domainRepository->findOrCreate($name);
$item->setDomain($domain);
$item->setUnitPrice($this->getDomainPrice($domain);
return $item;
}
private function getDomainPrice(Domain $domain)
{
return 19.99; // Some your own logic here to determine the price for domain.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment