Skip to content

Commit d2bacc2

Browse files
authored
Merge pull request #8199 from magento-lynx/quick-order-graphql
2 parents 176e388 + 6299a8e commit d2bacc2

File tree

11 files changed

+408
-32
lines changed

11 files changed

+408
-32
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ContactGraphQl\Model;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Framework\Validator\EmailAddress;
12+
13+
class ContactUsValidator
14+
{
15+
/**
16+
* @var EmailAddress
17+
*/
18+
private EmailAddress $emailValidator;
19+
20+
/**
21+
* @param EmailAddress $emailValidator
22+
*/
23+
public function __construct(
24+
EmailAddress $emailValidator
25+
) {
26+
$this->emailValidator = $emailValidator;
27+
}
28+
29+
/**
30+
* Validate input data
31+
*
32+
* @param string[] $input
33+
* @return void
34+
* @throws GraphQlInputException
35+
*/
36+
public function execute(array $input): void
37+
{
38+
if (!$this->emailValidator->isValid($input['email'])) {
39+
throw new GraphQlInputException(
40+
__('The email address is invalid. Verify the email address and try again.')
41+
);
42+
}
43+
44+
if ($input['name'] === '') {
45+
throw new GraphQlInputException(__('Name field is required.'));
46+
}
47+
48+
if ($input['comment'] === '') {
49+
throw new GraphQlInputException(__('Comment field is required.'));
50+
}
51+
}
52+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\ContactGraphQl\Model\Resolver;
11+
12+
use Magento\Contact\Model\ConfigInterface;
13+
use Magento\Contact\Model\MailInterface;
14+
use Magento\Framework\GraphQl\Config\Element\Field;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16+
use Magento\Framework\GraphQl\Query\ResolverInterface;
17+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
18+
use Psr\Log\LoggerInterface;
19+
use Magento\ContactGraphQl\Model\ContactUsValidator;
20+
21+
class ContactUs implements ResolverInterface
22+
{
23+
/**
24+
* @var MailInterface
25+
*/
26+
private MailInterface $mail;
27+
28+
/**
29+
* @var ConfigInterface
30+
*/
31+
private ConfigInterface $contactConfig;
32+
33+
/**
34+
* @var LoggerInterface
35+
*/
36+
private LoggerInterface $logger;
37+
38+
/**
39+
* @var ContactUsValidator
40+
*/
41+
private ContactUsValidator $validator;
42+
43+
/**
44+
* @param MailInterface $mail
45+
* @param ConfigInterface $contactConfig
46+
* @param LoggerInterface $logger
47+
* @param ContactUsValidator $validator
48+
*/
49+
public function __construct(
50+
MailInterface $mail,
51+
ConfigInterface $contactConfig,
52+
LoggerInterface $logger,
53+
ContactUsValidator $validator
54+
) {
55+
$this->mail = $mail;
56+
$this->contactConfig = $contactConfig;
57+
$this->logger = $logger;
58+
$this->validator = $validator;
59+
}
60+
61+
/**
62+
* @inheritDoc
63+
*/
64+
public function resolve(
65+
Field $field,
66+
$context,
67+
ResolveInfo $info,
68+
array $value = null,
69+
array $args = null
70+
) {
71+
if (!$this->contactConfig->isEnabled()) {
72+
throw new GraphQlInputException(
73+
__('The contact form is unavailable.')
74+
);
75+
}
76+
77+
$input = array_map(function ($field) {
78+
return $field === null ? '' : trim($field);
79+
}, $args['input']);
80+
$this->validator->execute($input);
81+
82+
try {
83+
$this->mail->send($input['email'], ['data' => $input]);
84+
} catch (\Exception $e) {
85+
$this->logger->critical($e);
86+
throw new GraphQlInputException(
87+
__('An error occurred while processing your form. Please try again later.')
88+
);
89+
}
90+
91+
return [
92+
'status' => true
93+
];
94+
}
95+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ContactGraphQlPwa
2+
3+
**ContactGraphQlPwa** provides GraphQL support for `magento/module-contact`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "magento/module-contact-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"config": {
6+
"sort-packages": true
7+
},
8+
"require": {
9+
"php": "~8.1.0||~8.2.0",
10+
"magento/framework": "*",
11+
"magento/module-contact": "*"
12+
},
13+
"suggest": {
14+
"magento/module-graph-ql": "*"
15+
},
16+
"license": [
17+
"OSL-3.0",
18+
"AFL-3.0"
19+
],
20+
"autoload": {
21+
"files": [
22+
"registration.php"
23+
],
24+
"psr-4": {
25+
"Magento\\ContactGraphQl\\": ""
26+
}
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider">
10+
<arguments>
11+
<argument name="extendedConfigData" xsi:type="array">
12+
<item name="contact_enabled" xsi:type="string">contact/contact/enabled</item>
13+
</argument>
14+
</arguments>
15+
</type>
16+
</config>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_ContactGraphQl" />
10+
</config>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Mutation {
5+
contactUs(
6+
input: ContactUsInput! @doc(description: "An input object that defines shopper information.")
7+
): ContactUsOutput @doc(description: "Send a 'Contact Us' email to the merchant.") @resolver(class: "Magento\\ContactGraphQl\\Model\\Resolver\\ContactUs")
8+
}
9+
10+
input ContactUsInput {
11+
email: String! @doc(description: "The email address of the shopper.")
12+
name: String! @doc(description: "The full name of the shopper.")
13+
telephone: String @doc(description: "The shopper's telephone number.")
14+
comment: String! @doc(description: "The shopper's comment to the merchant.")
15+
}
16+
17+
type ContactUsOutput @doc(description: "Contains the status of the request."){
18+
status: Boolean! @doc(description: "Indicates whether the request was successful.")
19+
}
20+
21+
type StoreConfig {
22+
contact_enabled: Boolean! @doc(description: "Indicates whether the Contact Us form in enabled.")
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Component\ComponentRegistrar;
9+
10+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_ContactGraphQl', __DIR__);

app/code/Magento/Customer/Test/Fixture/CustomerGroup.php

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
declare(strict_types=1);
87

98
namespace Magento\Customer\Test\Fixture;
109

1110
use Magento\Customer\Api\Data\GroupInterface;
1211
use Magento\Customer\Api\GroupRepositoryInterface;
13-
use Magento\Framework\Api\SearchCriteriaBuilder;
1412
use Magento\Framework\DataObject;
1513
use Magento\Framework\EntityManager\Hydrator;
16-
use Magento\Framework\Exception\LocalizedException;
17-
use Magento\Framework\Exception\NoSuchEntityException;
18-
use Magento\Tax\Api\TaxClassRepositoryInterface;
14+
use Magento\TestFramework\Fixture\Api\DataMerger;
1915
use Magento\TestFramework\Fixture\Api\ServiceFactory;
16+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
2017
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
2118

2219
/**
@@ -35,50 +32,46 @@ class CustomerGroup implements RevertibleDataFixtureInterface
3532
private ServiceFactory $serviceFactory;
3633

3734
/**
38-
* @var TaxClassRepositoryInterface
35+
* @var Hydrator
3936
*/
40-
private TaxClassRepositoryInterface $taxClassRepository;
41-
42-
/** @var Hydrator */
4337
private Hydrator $hydrator;
4438

39+
/**
40+
* @var DataMerger
41+
*/
42+
private DataMerger $dataMerger;
43+
44+
/**
45+
* @var ProcessorInterface
46+
*/
47+
private ProcessorInterface $dataProcessor;
48+
4549
/**
4650
* @param ServiceFactory $serviceFactory
47-
* @param TaxClassRepositoryInterface $taxClassRepository
48-
* @param SearchCriteriaBuilder $searchCriteriaBuilder
4951
* @param Hydrator $hydrator
52+
* @param DataMerger $dataMerger
53+
* @param ProcessorInterface $dataProcessor
5054
*/
5155
public function __construct(
5256
ServiceFactory $serviceFactory,
53-
TaxClassRepositoryInterface $taxClassRepository,
54-
Hydrator $hydrator
57+
Hydrator $hydrator,
58+
DataMerger $dataMerger,
59+
ProcessorInterface $dataProcessor
5560
) {
5661
$this->serviceFactory = $serviceFactory;
57-
$this->taxClassRepository = $taxClassRepository;
5862
$this->hydrator = $hydrator;
63+
$this->dataMerger = $dataMerger;
64+
$this->dataProcessor = $dataProcessor;
5965
}
6066

6167
/**
62-
* {@inheritdoc}
63-
* @param array $data Parameters. Same format as Customer::DEFAULT_DATA.
64-
* @return DataObject|null
65-
* @throws LocalizedException
66-
* @throws NoSuchEntityException
68+
* @inheritdoc
6769
*/
6870
public function apply(array $data = []): ?DataObject
6971
{
70-
$customerGroupSaveService = $this->serviceFactory->create(
71-
GroupRepositoryInterface::class,
72-
'save'
73-
);
74-
$data = self::DEFAULT_DATA;
75-
if (!empty($data['tax_class_id'])) {
76-
$data[GroupInterface::TAX_CLASS_ID] = $this->taxClassRepository->get($data['tax_class_id'])->getClassId();
77-
}
78-
79-
$customerGroup = $customerGroupSaveService->execute(
72+
$customerGroup = $this->serviceFactory->create(GroupRepositoryInterface::class, 'save')->execute(
8073
[
81-
'group' => $data,
74+
'group' => $this->dataProcessor->process($this, $this->dataMerger->merge(self::DEFAULT_DATA, $data))
8275
]
8376
);
8477

@@ -90,8 +83,7 @@ public function apply(array $data = []): ?DataObject
9083
*/
9184
public function revert(DataObject $data): void
9285
{
93-
$service = $this->serviceFactory->create(GroupRepositoryInterface::class, 'deleteById');
94-
$service->execute(
86+
$this->serviceFactory->create(GroupRepositoryInterface::class, 'deleteById')->execute(
9587
[
9688
'id' => $data->getId()
9789
]

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
"magento/module-configurable-product": "*",
152152
"magento/module-configurable-product-sales": "*",
153153
"magento/module-contact": "*",
154+
"magento/module-contact-graph-ql": "*",
154155
"magento/module-cookie": "*",
155156
"magento/module-cron": "*",
156157
"magento/module-currency-symbol": "*",

0 commit comments

Comments
 (0)