Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services;
- use App\Domain\Enumerators\Session\SessionVariablesEnum;
- use Cache\Adapter\Predis\PredisCachePool;
- use Commercetools\Core\Builder\Request\RequestBuilder;
- use Commercetools\Core\Client;
- use Commercetools\Core\Config;
- use Commercetools\Core\Model\Cart\Cart;
- use Commercetools\Core\Model\Cart\CartDraft;
- use Commercetools\Core\Model\Cart\LineItem;
- use Commercetools\Core\Model\Cart\LineItemCollection;
- use Commercetools\Core\Model\Cart\LineItemDraft;
- use Commercetools\Core\Model\Cart\LineItemDraftCollection;
- use Commercetools\Core\Model\Category\Category;
- use Commercetools\Core\Model\Category\CategoryCollection;
- use Commercetools\Core\Model\Common\Address;
- use Commercetools\Core\Model\Common\Context;
- use Commercetools\Core\Model\Common\LocalizedString;
- use Commercetools\Core\Model\Product\ProductProjectionCollection;
- use Commercetools\Core\Model\Product\Product;
- use Commercetools\Core\Model\Product\Search\Filter;
- use Commercetools\Core\Model\ShoppingList\ShoppingList;
- use Commercetools\Core\Model\ShoppingList\ShoppingListDraft;
- use Commercetools\Core\Request\Categories\CategoryByKeyGetRequest;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Str;
- use Ramsey\Uuid\Uuid;
- class CommerceToolsService
- {
- private $config;
- /**
- * CommerceToolsService constructor.
- * This constructs the service for building the initial client config and fetching of data from commercetools api
- */
- public function __construct()
- {
- $this->config = [
- 'client_id' => env('COMMERCE_TOOLS_CLIENT_ID'),
- 'client_secret' => env('COMMERCE_TOOLS_CLIENT_SECRET'),
- 'project' => env('COMMERCE_TOOLS_PROJECT_ID'),
- ];
- $context = Context::of()->setLanguages(['en'])->setLocale('en_US')->setGraceful(true);
- $this->config = Config::fromArray($this->config)->setContext($context);
- }
- /**
- * @param RequestBuilder $request
- * @return mixed
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- private function makeClientCallWithRequestObject($request)
- {
- $predisClient = new \Predis\Client('tcp:/127.0.0.1:6379');
- $pool = new PredisCachePool($predisClient);
- $client = Client\ClientFactory::of()->createClient($this->config, null, $pool);
- try {
- $response = $client->execute($request);
- if ($response->getStatusCode() == 400) {
- throw new \Exception($response->getBody()->getContents());
- }
- return $request->mapFromResponse($response);
- } catch (ApiException $exception) {
- throw new \Exception(
- "Ooops! Something happened while making a call to ecommerce:" . $exception->getMessage(),
- 0,
- $exception
- );
- }
- }
- /**
- * @param array $parameters
- * @return ProductProjectionCollection
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getProducts(array $parameters): ProductProjectionCollection
- {
- $request = RequestBuilder::of()->productProjections()->search();
- foreach ($parameters as $parameter => $value) {
- $filter = new Filter($parameter, $value);
- $request->addFilter($filter);
- }
- return $this->makeClientCallWithRequestObject($request);
- }
- /**
- * @param string $categoryName
- * @return array
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getCategoryByName(?string $categoryName): ?Category
- {
- $request = RequestBuilder::of()->categories()->getByKey($categoryName);
- return $this->makeClientCallWithRequestObject($request);
- }
- /**
- * @return array
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getAllCategories(): CategoryCollection
- {
- $request = RequestBuilder::of()->categories()->query();
- return $this->makeClientCallWithRequestObject($request);
- }
- /**
- * @param int $limit
- * @return CategoryCollection
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getNCategories(int $limit): CategoryCollection
- {
- $request = RequestBuilder::of()->categories()->query()->addParam('limit', $limit);
- return $this->makeClientCallWithRequestObject($request);
- }
- /**
- * @param string $productId
- * @param string|null $activeCartId
- * @return array
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function addProductToCart(string $productId, ?string $cartId = null): Cart
- {
- if (!is_null($cartId)) {
- $cart = $this->getCartById($cartId);
- $lineItemDraftCollection = new LineItemDraftCollection();
- foreach ($cart->getLineItems() as $lineItem) {
- $lineItemDraft = new LineItemDraft();
- $lineItemDraft->setProductId($lineItem->getProductId());
- $lineItemDraft->setQuantity($lineItem->getQuantity());
- $lineItemDraftCollection->add($lineItemDraft);
- }
- $lineItemDraft = new LineItemDraft();
- $lineItemDraft->setProductId($productId);
- $lineItemDraft->setQuantity(1);
- $lineItemDraftCollection->add($lineItemDraft);
- $cartDraft = new CartDraft();
- $cartDraft->setLineItems($lineItemDraftCollection);
- $cartDraft->setCurrency('EUR');
- } else {
- $lineItemDraft = new LineItemDraft();
- $lineItemDraft->setProductId($productId);
- $lineItemDraft->setQuantity(1);
- $lineItemDraftCollection = new LineItemDraftCollection();
- $lineItemDraftCollection->add($lineItemDraft);
- $cartDraft = new CartDraft();
- $cartDraft->setLineItems($lineItemDraftCollection);
- $cartDraft->setCurrency('EUR');
- }
- $request = RequestBuilder::of()->carts()->create($cartDraft);
- return $this->makeClientCallWithRequestObject($request);
- }
- /**
- * @param string $productId
- * @param string|null $wishlistId
- * @return ShoppingList
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function addProductToWishlist(string $productId, ?string $wishlistId = null): ShoppingList
- {
- if (!is_null($wishlistId)) {
- $cart = $this->getWishlistById($wishlistId);
- $lineItemDraftCollection = new \Commercetools\Core\Model\ShoppingList\LineItemDraftCollection();
- foreach ($cart->getLineItems() as $lineItem) {
- $lineItemDraft = new \Commercetools\Core\Model\ShoppingList\LineItemDraft();
- $lineItemDraft->setProductId($lineItem->getProductId());
- $lineItemDraft->setQuantity($lineItem->getQuantity());
- $lineItemDraftCollection->add($lineItemDraft);
- }
- $lineItemDraft = new \Commercetools\Core\Model\ShoppingList\LineItemDraft();
- $lineItemDraft->setProductId($productId);
- $lineItemDraft->setQuantity(1);
- $lineItemDraftCollection->add($lineItemDraft);
- $shoppingListDraft = new ShoppingListDraft();
- $shoppingListDraft->setLineItems($lineItemDraftCollection);
- } else {
- $lineItemDraft = new \Commercetools\Core\Model\ShoppingList\LineItemDraft();
- $lineItemDraft->setProductId($productId);
- $lineItemDraft->setQuantity(1);
- $lineItemDraftCollection = new \Commercetools\Core\Model\ShoppingList\LineItemDraftCollection();
- $lineItemDraftCollection->add($lineItemDraft);
- $shoppingListDraft = new ShoppingListDraft();
- $localisedString = new LocalizedString(['en-US', 'Anonimus Shopping List']);
- $shoppingListDraft->setName($localisedString);
- $shoppingListDraft->setLineItems($lineItemDraftCollection);
- }
- $request = RequestBuilder::of()->shoppingLists()->create($shoppingListDraft);
- return $this->makeClientCallWithRequestObject($request);
- }
- public function deleteCart(string $cartId)
- {
- $cart = $this->getCartById($cartId);
- $request = RequestBuilder::of()->carts()->delete($cart);
- }
- /**
- * @param Collection $lineItems
- * @param string $cartId
- * @return string
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function updateCart(Collection $lineItems, string $cartId): string
- {
- $this->deleteCart($cartId);
- $lineItemDraftCollection = new LineItemDraftCollection();
- foreach ($lineItems as $lineItem) {
- $lineItemDraft = new LineItemDraft();
- $lineItemDraft->setProductId($lineItem->id);
- $lineItemDraft->setQuantity($lineItem->quantity);
- $lineItemDraftCollection->add($lineItemDraft);
- }
- $cart = new CartDraft();
- $cart->setLineItems($lineItemDraftCollection);
- $cart->setCurrency('EUR');
- $request = RequestBuilder::of()->carts()->create($cart);
- return $this->makeClientCallWithRequestObject($request)->getId();
- }
- /**
- * @param Collection $lineItems
- * @param string $cartId
- * @return string
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function updateWishlist(Collection $lineItems, string $cartId): string
- {
- $this->deleteCart($cartId);
- $lineItemDraftCollection = new \Commercetools\Core\Model\ShoppingList\LineItemDraftCollection();
- foreach ($lineItems as $lineItem) {
- $lineItemDraft = new \Commercetools\Core\Model\ShoppingList\LineItemDraft();
- $lineItemDraft->setProductId($lineItem->id);
- $lineItemDraft->setQuantity($lineItem->quantity);
- $lineItemDraftCollection->add($lineItemDraft);
- }
- $shoppingList = new ShoppingListDraft();
- $shoppingList->setLineItems($lineItemDraftCollection);
- $request = RequestBuilder::of()->shoppingLists()->create($shoppingList);
- return $this->makeClientCallWithRequestObject($request)->getId();
- }
- /**
- * @param string $cartId
- * @return Cart
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getCartById(string $cartId): Cart
- {
- $request = RequestBuilder::of()->carts()->getById($cartId);
- return $this->makeClientCallWithRequestObject($request);
- }
- /**
- * @param string $wishlistId
- * @return ShoppingList
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getWishlistById(string $wishlistId): ShoppingList
- {
- $request = RequestBuilder::of()->shoppingLists()->getById($wishlistId);
- return $this->makeClientCallWithRequestObject($request);
- }
- /**
- * @param int $productId
- * @return Cart
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getProductById(string $productId): Product
- {
- $request = RequestBuilder::of()->products()->getById($productId);
- return $this->makeClientCallWithRequestObject($request);
- }
- /**
- * @param string $cartId
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function convertCartToOrder(string $cartId)
- {
- $cart = $this->getCartById($cartId);
- $shippingAddress = new Address();
- $shippingAddress->setStreetName('Bojmija');
- $cart->setShippingAddress($shippingAddress);
- $request = RequestBuilder::of()->orders()->createFromCart($cart);
- $this->makeClientCallWithRequestObject($request);
- }
- }
Add Comment
Please, Sign In to add comment