nanorocks

CommerceTools Service Class

May 4th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.80 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services;
  4.  
  5. use App\Domain\Enumerators\Session\SessionVariablesEnum;
  6. use Cache\Adapter\Predis\PredisCachePool;
  7. use Commercetools\Core\Builder\Request\RequestBuilder;
  8. use Commercetools\Core\Client;
  9. use Commercetools\Core\Config;
  10. use Commercetools\Core\Model\Cart\Cart;
  11. use Commercetools\Core\Model\Cart\CartDraft;
  12. use Commercetools\Core\Model\Cart\LineItem;
  13. use Commercetools\Core\Model\Cart\LineItemCollection;
  14. use Commercetools\Core\Model\Cart\LineItemDraft;
  15. use Commercetools\Core\Model\Cart\LineItemDraftCollection;
  16. use Commercetools\Core\Model\Category\Category;
  17. use Commercetools\Core\Model\Category\CategoryCollection;
  18. use Commercetools\Core\Model\Common\Address;
  19. use Commercetools\Core\Model\Common\Context;
  20. use Commercetools\Core\Model\Common\LocalizedString;
  21. use Commercetools\Core\Model\Product\ProductProjectionCollection;
  22. use Commercetools\Core\Model\Product\Product;
  23. use Commercetools\Core\Model\Product\Search\Filter;
  24. use Commercetools\Core\Model\ShoppingList\ShoppingList;
  25. use Commercetools\Core\Model\ShoppingList\ShoppingListDraft;
  26. use Commercetools\Core\Request\Categories\CategoryByKeyGetRequest;
  27. use Illuminate\Support\Collection;
  28. use Illuminate\Support\Facades\Log;
  29. use Illuminate\Support\Str;
  30. use Ramsey\Uuid\Uuid;
  31.  
  32. class CommerceToolsService
  33. {
  34.     private $config;
  35.  
  36.     /**
  37.      * CommerceToolsService constructor.
  38.      * This constructs the service for building the initial client config and fetching of data from commercetools api
  39.      */
  40.     public function __construct()
  41.     {
  42.         $this->config = [
  43.             'client_id' => env('COMMERCE_TOOLS_CLIENT_ID'),
  44.             'client_secret' => env('COMMERCE_TOOLS_CLIENT_SECRET'),
  45.             'project' => env('COMMERCE_TOOLS_PROJECT_ID'),
  46.         ];
  47.  
  48.         $context = Context::of()->setLanguages(['en'])->setLocale('en_US')->setGraceful(true);
  49.  
  50.         $this->config = Config::fromArray($this->config)->setContext($context);
  51.     }
  52.  
  53.     /**
  54.      * @param RequestBuilder $request
  55.      * @return mixed
  56.      * @throws \GuzzleHttp\Exception\GuzzleException
  57.      */
  58.     private function makeClientCallWithRequestObject($request)
  59.     {
  60.         $predisClient = new \Predis\Client('tcp:/127.0.0.1:6379');
  61.  
  62.         $pool = new PredisCachePool($predisClient);
  63.  
  64.         $client = Client\ClientFactory::of()->createClient($this->config, null, $pool);
  65.  
  66.         try {
  67.             $response = $client->execute($request);
  68.  
  69.             if ($response->getStatusCode() == 400) {
  70.                 throw new \Exception($response->getBody()->getContents());
  71.             }
  72.  
  73.             return $request->mapFromResponse($response);
  74.         } catch (ApiException $exception) {
  75.             throw new \Exception(
  76.                 "Ooops! Something happened while making a call to ecommerce:" . $exception->getMessage(),
  77.                 0,
  78.                 $exception
  79.             );
  80.         }
  81.     }
  82.  
  83.     /**
  84.      * @param array $parameters
  85.      * @return ProductProjectionCollection
  86.      * @throws \GuzzleHttp\Exception\GuzzleException
  87.      */
  88.     public function getProducts(array $parameters): ProductProjectionCollection
  89.     {
  90.         $request = RequestBuilder::of()->productProjections()->search();
  91.  
  92.         foreach ($parameters as $parameter => $value) {
  93.             $filter = new Filter($parameter, $value);
  94.  
  95.             $request->addFilter($filter);
  96.         }
  97.  
  98.         return $this->makeClientCallWithRequestObject($request);
  99.     }
  100.  
  101.     /**
  102.      * @param string $categoryName
  103.      * @return array
  104.      * @throws \GuzzleHttp\Exception\GuzzleException
  105.      */
  106.     public function getCategoryByName(?string $categoryName): ?Category
  107.     {
  108.         $request = RequestBuilder::of()->categories()->getByKey($categoryName);
  109.  
  110.         return $this->makeClientCallWithRequestObject($request);
  111.     }
  112.  
  113.     /**
  114.      * @return array
  115.      * @throws \GuzzleHttp\Exception\GuzzleException
  116.      */
  117.     public function getAllCategories(): CategoryCollection
  118.     {
  119.         $request = RequestBuilder::of()->categories()->query();
  120.  
  121.         return $this->makeClientCallWithRequestObject($request);
  122.     }
  123.  
  124.     /**
  125.      * @param int $limit
  126.      * @return CategoryCollection
  127.      * @throws \GuzzleHttp\Exception\GuzzleException
  128.      */
  129.     public function getNCategories(int $limit): CategoryCollection
  130.     {
  131.         $request = RequestBuilder::of()->categories()->query()->addParam('limit', $limit);
  132.  
  133.         return $this->makeClientCallWithRequestObject($request);
  134.     }
  135.  
  136.     /**
  137.      * @param string $productId
  138.      * @param string|null $activeCartId
  139.      * @return array
  140.      * @throws \GuzzleHttp\Exception\GuzzleException
  141.      */
  142.     public function addProductToCart(string $productId, ?string $cartId = null): Cart
  143.     {
  144.         if (!is_null($cartId)) {
  145.             $cart = $this->getCartById($cartId);
  146.  
  147.             $lineItemDraftCollection = new LineItemDraftCollection();
  148.  
  149.             foreach ($cart->getLineItems() as $lineItem) {
  150.                 $lineItemDraft = new LineItemDraft();
  151.                 $lineItemDraft->setProductId($lineItem->getProductId());
  152.                 $lineItemDraft->setQuantity($lineItem->getQuantity());
  153.                 $lineItemDraftCollection->add($lineItemDraft);
  154.             }
  155.  
  156.             $lineItemDraft = new LineItemDraft();
  157.             $lineItemDraft->setProductId($productId);
  158.             $lineItemDraft->setQuantity(1);
  159.             $lineItemDraftCollection->add($lineItemDraft);
  160.  
  161.             $cartDraft = new CartDraft();
  162.             $cartDraft->setLineItems($lineItemDraftCollection);
  163.             $cartDraft->setCurrency('EUR');
  164.         } else {
  165.             $lineItemDraft = new LineItemDraft();
  166.             $lineItemDraft->setProductId($productId);
  167.             $lineItemDraft->setQuantity(1);
  168.  
  169.             $lineItemDraftCollection = new LineItemDraftCollection();
  170.             $lineItemDraftCollection->add($lineItemDraft);
  171.  
  172.             $cartDraft = new CartDraft();
  173.             $cartDraft->setLineItems($lineItemDraftCollection);
  174.             $cartDraft->setCurrency('EUR');
  175.         }
  176.  
  177.         $request = RequestBuilder::of()->carts()->create($cartDraft);
  178.  
  179.         return $this->makeClientCallWithRequestObject($request);
  180.     }
  181.  
  182.     /**
  183.      * @param string $productId
  184.      * @param string|null $wishlistId
  185.      * @return ShoppingList
  186.      * @throws \GuzzleHttp\Exception\GuzzleException
  187.      */
  188.     public function addProductToWishlist(string $productId, ?string $wishlistId = null): ShoppingList
  189.     {
  190.         if (!is_null($wishlistId)) {
  191.             $cart = $this->getWishlistById($wishlistId);
  192.  
  193.             $lineItemDraftCollection = new \Commercetools\Core\Model\ShoppingList\LineItemDraftCollection();
  194.  
  195.             foreach ($cart->getLineItems() as $lineItem) {
  196.                 $lineItemDraft = new \Commercetools\Core\Model\ShoppingList\LineItemDraft();
  197.                 $lineItemDraft->setProductId($lineItem->getProductId());
  198.                 $lineItemDraft->setQuantity($lineItem->getQuantity());
  199.                 $lineItemDraftCollection->add($lineItemDraft);
  200.             }
  201.  
  202.             $lineItemDraft = new \Commercetools\Core\Model\ShoppingList\LineItemDraft();
  203.             $lineItemDraft->setProductId($productId);
  204.             $lineItemDraft->setQuantity(1);
  205.             $lineItemDraftCollection->add($lineItemDraft);
  206.  
  207.             $shoppingListDraft = new ShoppingListDraft();
  208.             $shoppingListDraft->setLineItems($lineItemDraftCollection);
  209.         } else {
  210.             $lineItemDraft = new \Commercetools\Core\Model\ShoppingList\LineItemDraft();
  211.             $lineItemDraft->setProductId($productId);
  212.             $lineItemDraft->setQuantity(1);
  213.  
  214.             $lineItemDraftCollection = new \Commercetools\Core\Model\ShoppingList\LineItemDraftCollection();
  215.             $lineItemDraftCollection->add($lineItemDraft);
  216.  
  217.             $shoppingListDraft = new ShoppingListDraft();
  218.             $localisedString = new LocalizedString(['en-US', 'Anonimus Shopping List']);
  219.             $shoppingListDraft->setName($localisedString);
  220.             $shoppingListDraft->setLineItems($lineItemDraftCollection);
  221.         }
  222.  
  223.         $request = RequestBuilder::of()->shoppingLists()->create($shoppingListDraft);
  224.  
  225.         return $this->makeClientCallWithRequestObject($request);
  226.     }
  227.  
  228.     public function deleteCart(string $cartId)
  229.     {
  230.         $cart = $this->getCartById($cartId);
  231.  
  232.         $request = RequestBuilder::of()->carts()->delete($cart);
  233.     }
  234.  
  235.     /**
  236.      * @param Collection $lineItems
  237.      * @param string $cartId
  238.      * @return string
  239.      * @throws \GuzzleHttp\Exception\GuzzleException
  240.      */
  241.     public function updateCart(Collection $lineItems, string $cartId): string
  242.     {
  243.         $this->deleteCart($cartId);
  244.  
  245.         $lineItemDraftCollection = new LineItemDraftCollection();
  246.  
  247.         foreach ($lineItems as $lineItem) {
  248.             $lineItemDraft = new LineItemDraft();
  249.             $lineItemDraft->setProductId($lineItem->id);
  250.             $lineItemDraft->setQuantity($lineItem->quantity);
  251.             $lineItemDraftCollection->add($lineItemDraft);
  252.         }
  253.  
  254.         $cart = new CartDraft();
  255.         $cart->setLineItems($lineItemDraftCollection);
  256.         $cart->setCurrency('EUR');
  257.  
  258.         $request = RequestBuilder::of()->carts()->create($cart);
  259.  
  260.         return $this->makeClientCallWithRequestObject($request)->getId();
  261.     }
  262.  
  263.     /**
  264.      * @param Collection $lineItems
  265.      * @param string $cartId
  266.      * @return string
  267.      * @throws \GuzzleHttp\Exception\GuzzleException
  268.      */
  269.     public function updateWishlist(Collection $lineItems, string $cartId): string
  270.     {
  271.         $this->deleteCart($cartId);
  272.  
  273.         $lineItemDraftCollection = new \Commercetools\Core\Model\ShoppingList\LineItemDraftCollection();
  274.  
  275.         foreach ($lineItems as $lineItem) {
  276.             $lineItemDraft = new \Commercetools\Core\Model\ShoppingList\LineItemDraft();
  277.             $lineItemDraft->setProductId($lineItem->id);
  278.             $lineItemDraft->setQuantity($lineItem->quantity);
  279.             $lineItemDraftCollection->add($lineItemDraft);
  280.         }
  281.  
  282.         $shoppingList = new ShoppingListDraft();
  283.         $shoppingList->setLineItems($lineItemDraftCollection);
  284.  
  285.         $request = RequestBuilder::of()->shoppingLists()->create($shoppingList);
  286.  
  287.         return $this->makeClientCallWithRequestObject($request)->getId();
  288.     }
  289.  
  290.     /**
  291.      * @param string $cartId
  292.      * @return Cart
  293.      * @throws \GuzzleHttp\Exception\GuzzleException
  294.      */
  295.     public function getCartById(string $cartId): Cart
  296.     {
  297.         $request = RequestBuilder::of()->carts()->getById($cartId);
  298.  
  299.         return $this->makeClientCallWithRequestObject($request);
  300.     }
  301.  
  302.     /**
  303.      * @param string $wishlistId
  304.      * @return ShoppingList
  305.      * @throws \GuzzleHttp\Exception\GuzzleException
  306.      */
  307.     public function getWishlistById(string $wishlistId): ShoppingList
  308.     {
  309.         $request = RequestBuilder::of()->shoppingLists()->getById($wishlistId);
  310.  
  311.         return $this->makeClientCallWithRequestObject($request);
  312.     }
  313.  
  314.     /**
  315.      * @param int $productId
  316.      * @return Cart
  317.      * @throws \GuzzleHttp\Exception\GuzzleException
  318.      */
  319.     public function getProductById(string $productId): Product
  320.     {
  321.         $request = RequestBuilder::of()->products()->getById($productId);
  322.  
  323.         return $this->makeClientCallWithRequestObject($request);
  324.     }
  325.  
  326.     /**
  327.      * @param string $cartId
  328.      * @throws \GuzzleHttp\Exception\GuzzleException
  329.      */
  330.     public function convertCartToOrder(string $cartId)
  331.     {
  332.         $cart = $this->getCartById($cartId);
  333.  
  334.         $shippingAddress = new Address();
  335.         $shippingAddress->setStreetName('Bojmija');
  336.  
  337.         $cart->setShippingAddress($shippingAddress);
  338.  
  339.         $request = RequestBuilder::of()->orders()->createFromCart($cart);
  340.  
  341.         $this->makeClientCallWithRequestObject($request);
  342.     }
  343. }
Add Comment
Please, Sign In to add comment