forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request magento#3678 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
- Loading branch information
Showing
13 changed files
with
738 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
app/code/Magento/CustomerGraphQl/Model/Customer/CreateAccount.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Customer\Api\AccountManagementInterface; | ||
use Magento\Customer\Api\Data\CustomerInterface; | ||
use Magento\Customer\Api\Data\CustomerInterfaceFactory; | ||
use Magento\Framework\Api\DataObjectHelper; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Class CreateAccount creates new customer account | ||
*/ | ||
class CreateAccount | ||
{ | ||
/** | ||
* @var DataObjectHelper | ||
*/ | ||
private $dataObjectHelper; | ||
|
||
/** | ||
* @var CustomerInterfaceFactory | ||
*/ | ||
private $customerFactory; | ||
|
||
/** | ||
* @var AccountManagementInterface | ||
*/ | ||
private $accountManagement; | ||
|
||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @param DataObjectHelper $dataObjectHelper | ||
* @param CustomerInterfaceFactory $customerFactory | ||
* @param StoreManagerInterface $storeManager | ||
* @param AccountManagementInterface $accountManagement | ||
*/ | ||
public function __construct( | ||
DataObjectHelper $dataObjectHelper, | ||
CustomerInterfaceFactory $customerFactory, | ||
StoreManagerInterface $storeManager, | ||
AccountManagementInterface $accountManagement | ||
) { | ||
$this->dataObjectHelper = $dataObjectHelper; | ||
$this->customerFactory = $customerFactory; | ||
$this->accountManagement = $accountManagement; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
* Creates new customer account | ||
* | ||
* @param array $args | ||
* @return CustomerInterface | ||
* @throws LocalizedException | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function execute(array $args): CustomerInterface | ||
{ | ||
$customerDataObject = $this->customerFactory->create(); | ||
$this->dataObjectHelper->populateWithArray( | ||
$customerDataObject, | ||
$args['input'], | ||
CustomerInterface::class | ||
); | ||
$store = $this->storeManager->getStore(); | ||
$customerDataObject->setWebsiteId($store->getWebsiteId()); | ||
$customerDataObject->setStoreId($store->getId()); | ||
|
||
$password = array_key_exists('password', $args['input']) ? $args['input']['password'] : null; | ||
|
||
return $this->accountManagement->createAccount($customerDataObject, $password); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/code/Magento/CustomerGraphQl/Model/Customer/SetUpUserContext.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Customer\Api\Data\CustomerInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Authorization\Model\UserContextInterface; | ||
|
||
/** | ||
* Set up user context after creating new customer account | ||
*/ | ||
class SetUpUserContext | ||
{ | ||
/** | ||
* Set up user context after creating new customer account | ||
* | ||
* @param ContextInterface $context | ||
* @param CustomerInterface $customer | ||
*/ | ||
public function execute(ContextInterface $context, CustomerInterface $customer) | ||
{ | ||
$context->setUserId((int)$customer->getId()); | ||
$context->setUserType(UserContextInterface::USER_TYPE_CUSTOMER); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
app/code/Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Resolver; | ||
|
||
use Magento\CustomerGraphQl\Model\Customer\ChangeSubscriptionStatus; | ||
use Magento\CustomerGraphQl\Model\Customer\CreateAccount; | ||
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider; | ||
use Magento\CustomerGraphQl\Model\Customer\SetUpUserContext; | ||
use Magento\Framework\Exception\State\InputMismatchException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\Validator\Exception as ValidatorException; | ||
|
||
/** | ||
* Create customer account resolver | ||
*/ | ||
class CreateCustomer implements ResolverInterface | ||
{ | ||
/** | ||
* @var CustomerDataProvider | ||
*/ | ||
private $customerDataProvider; | ||
|
||
/** | ||
* @var ChangeSubscriptionStatus | ||
*/ | ||
private $changeSubscriptionStatus; | ||
|
||
/** | ||
* @var CreateAccount | ||
*/ | ||
private $createAccount; | ||
|
||
/** | ||
* @var SetUpUserContext | ||
*/ | ||
private $setUpUserContext; | ||
|
||
/** | ||
* @param CustomerDataProvider $customerDataProvider | ||
* @param ChangeSubscriptionStatus $changeSubscriptionStatus | ||
* @param SetUpUserContext $setUpUserContext | ||
* @param CreateAccount $createAccount | ||
*/ | ||
public function __construct( | ||
CustomerDataProvider $customerDataProvider, | ||
ChangeSubscriptionStatus $changeSubscriptionStatus, | ||
SetUpUserContext $setUpUserContext, | ||
CreateAccount $createAccount | ||
) { | ||
$this->customerDataProvider = $customerDataProvider; | ||
$this->changeSubscriptionStatus = $changeSubscriptionStatus; | ||
$this->createAccount = $createAccount; | ||
$this->setUpUserContext = $setUpUserContext; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) { | ||
throw new GraphQlInputException(__('"input" value should be specified')); | ||
} | ||
try { | ||
$customer = $this->createAccount->execute($args); | ||
$customerId = (int)$customer->getId(); | ||
$this->setUpUserContext->execute($context, $customer); | ||
if (array_key_exists('is_subscribed', $args['input'])) { | ||
if ($args['input']['is_subscribed']) { | ||
$this->changeSubscriptionStatus->execute($customerId, true); | ||
} | ||
} | ||
$data = $this->customerDataProvider->getCustomerById($customerId); | ||
} catch (ValidatorException $e) { | ||
throw new GraphQlInputException(__($e->getMessage())); | ||
} catch (InputMismatchException $e) { | ||
throw new GraphQlInputException(__($e->getMessage())); | ||
} | ||
|
||
return ['customer' => $data]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.