Skip to content

Commit

Permalink
Merge pull request #3892 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Mar 13, 2019
2 parents ab66a14 + a738086 commit 6cc68c6
Show file tree
Hide file tree
Showing 54 changed files with 1,041 additions and 789 deletions.
12 changes: 7 additions & 5 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
Expand Down Expand Up @@ -72,11 +73,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

$rootCategoryId = $this->getCategoryId($args);
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);
if (!empty($categoriesTree)) {
$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
return current($result);
} else {
return null;

if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
}

$result = $this->extractDataFromCategoryTree->execute($categoriesTree);
return current($result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ public function __construct(
*/
public function addParentProduct(Product $product) : void
{
if (isset($this->parentProducts[$product->getId()])) {
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$productId = $product->getData($linkField);

if (isset($this->parentProducts[$productId])) {
return;
}

if (!empty($this->childrenMap)) {
$this->childrenMap = [];
}
$this->parentProducts[$product->getId()] = $product;
$this->parentProducts[$productId] = $product;
}

/**
Expand Down Expand Up @@ -140,16 +143,12 @@ private function fetch() : array
return $this->childrenMap;
}

$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
foreach ($this->parentProducts as $product) {
$attributeData = $this->getAttributesCodes($product);
/** @var ChildCollection $childCollection */
$childCollection = $this->childCollectionFactory->create();
$childCollection->addAttributeToSelect($attributeData);

/** @var Product $product */
$product->setData($linkField, $product->getId());
$childCollection->setProductFilter($product);
$childCollection->addAttributeToSelect($attributeData);

/** @var Product $childProduct */
foreach ($childCollection->getItems() as $childProduct) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer\Address;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Api\DataObjectHelper;

/**
* Create customer address
*/
class CreateCustomerAddress
{
/**
* @var GetAllowedAddressAttributes
*/
private $getAllowedAddressAttributes;

/**
* @var AddressInterfaceFactory
*/
private $addressFactory;

/**
* @var AddressRepositoryInterface
*/
private $addressRepository;

/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
* @param AddressInterfaceFactory $addressFactory
* @param AddressRepositoryInterface $addressRepository
* @param DataObjectHelper $dataObjectHelper
*/
public function __construct(
GetAllowedAddressAttributes $getAllowedAddressAttributes,
AddressInterfaceFactory $addressFactory,
AddressRepositoryInterface $addressRepository,
DataObjectHelper $dataObjectHelper
) {
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
$this->addressFactory = $addressFactory;
$this->addressRepository = $addressRepository;
$this->dataObjectHelper = $dataObjectHelper;
}

/**
* Create customer address
*
* @param int $customerId
* @param array $data
* @return AddressInterface
* @throws GraphQlInputException
*/
public function execute(int $customerId, array $data): AddressInterface
{
$this->validateData($data);

/** @var AddressInterface $address */
$address = $this->addressFactory->create();
$this->dataObjectHelper->populateWithArray($address, $data, AddressInterface::class);

if (isset($data['region']['region_id'])) {
$address->setRegionId($address->getRegion()->getRegionId());
}
$address->setCustomerId($customerId);

try {
$this->addressRepository->save($address);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
return $address;
}

/**
* Validate customer address create data
*
* @param array $addressData
* @return void
* @throws GraphQlInputException
*/
public function validateData(array $addressData): void
{
$attributes = $this->getAllowedAddressAttributes->execute();
$errorInput = [];

foreach ($attributes as $attributeName => $attributeInfo) {
if ($attributeInfo->getIsRequired()
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
) {
$errorInput[] = $attributeName;
}
}

if ($errorInput) {
throw new GraphQlInputException(
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer\Address;

use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;

/**
* Delete customer address
*/
class DeleteCustomerAddress
{
/**
* @var AddressRepositoryInterface
*/
private $addressRepository;

/**
* @param AddressRepositoryInterface $addressRepository
*/
public function __construct(
AddressRepositoryInterface $addressRepository
) {
$this->addressRepository = $addressRepository;
}

/**
* Delete customer address
*
* @param AddressInterface $address
* @return void
* @throws GraphQlInputException
*/
public function execute(AddressInterface $address): void
{
if ($address->isDefaultBilling()) {
throw new GraphQlInputException(
__('Customer Address %1 is set as default billing address and can not be deleted', [$address->getId()])
);
}
if ($address->isDefaultShipping()) {
throw new GraphQlInputException(
__('Customer Address %1 is set as default shipping address and can not be deleted', [$address->getId()])
);
}

try {
$this->addressRepository->delete($address);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Loading

0 comments on commit 6cc68c6

Please sign in to comment.