Skip to content

Commit 78015e0

Browse files
committed
magento/graphql-ce#892: [Checkout] Improve strict typing in CartAddressInterface
1 parent 49cfa88 commit 78015e0

File tree

3 files changed

+79
-44
lines changed

3 files changed

+79
-44
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\GraphQl\Schema\Type\TypeRegistry;
11+
12+
/**
13+
* Validates address against required fields from schema
14+
*/
15+
class ValidateAddressFromSchema
16+
{
17+
/**
18+
* @var TypeRegistry
19+
*/
20+
private $typeRegistry;
21+
22+
/**
23+
* @param TypeRegistry $typeRegistry
24+
*/
25+
public function __construct(
26+
TypeRegistry $typeRegistry
27+
) {
28+
$this->typeRegistry = $typeRegistry;
29+
}
30+
31+
32+
/**
33+
* Validate data from address against mandatory fields from graphql schema for address
34+
*
35+
* @param array $address
36+
* @return bool
37+
*/
38+
public function execute(array $address = []) : bool
39+
{
40+
/** @var \Magento\Framework\GraphQL\Schema\Type\Input\InputObjectType $cartAddressInput */
41+
$cartAddressInput = $this->typeRegistry->get('CartAddressInput');
42+
$fields = $cartAddressInput->getFields();
43+
44+
foreach ($fields as $field) {
45+
if ($field->getType() instanceof \Magento\Framework\GraphQL\Schema\Type\NonNull) {
46+
// an array key has to exist but it's value should not be null
47+
if (array_key_exists($field->name, $address)
48+
&& !is_array($address[$field->name])
49+
&& !isset($address[$field->name])) {
50+
return false;
51+
}
52+
}
53+
}
54+
return true;
55+
}
56+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\Quote\Api\Data\CartInterface;
1515
use Magento\QuoteGraphQl\Model\Cart\ExtractQuoteAddressData;
16+
use Magento\QuoteGraphQl\Model\Cart\ValidateAddressFromSchema;
1617

1718
/**
1819
* @inheritdoc
@@ -24,12 +25,22 @@ class BillingAddress implements ResolverInterface
2425
*/
2526
private $extractQuoteAddressData;
2627

28+
/**
29+
* @var ValidateAddressFromSchema
30+
*/
31+
private $validateAddressFromSchema;
32+
2733
/**
2834
* @param ExtractQuoteAddressData $extractQuoteAddressData
35+
* @param ValidateAddressFromSchema $validateAddressFromSchema
2936
*/
30-
public function __construct(ExtractQuoteAddressData $extractQuoteAddressData)
37+
public function __construct(
38+
ExtractQuoteAddressData $extractQuoteAddressData,
39+
ValidateAddressFromSchema $validateAddressFromSchema
40+
)
3141
{
3242
$this->extractQuoteAddressData = $extractQuoteAddressData;
43+
$this->validateAddressFromSchema = $validateAddressFromSchema;
3344
}
3445

3546
/**
@@ -44,17 +55,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4455
$cart = $value['model'];
4556

4657
$billingAddress = $cart->getBillingAddress();
47-
if (null === $billingAddress ||
48-
null === $billingAddress->getFirstname() ||
49-
null === $billingAddress->getLastname() ||
50-
null === $billingAddress->getCity() ||
51-
null === $billingAddress->getCountry() ||
52-
null === $billingAddress->getTelephone() ||
53-
null === $billingAddress->getStreet()) {
58+
$addressData = $this->extractQuoteAddressData->execute($billingAddress);
59+
if (!$this->validateAddressFromSchema->execute($addressData)) {
5460
return null;
5561
}
56-
57-
$addressData = $this->extractQuoteAddressData->execute($billingAddress);
5862
return $addressData;
5963
}
6064
}

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddresses.php

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\Quote\Model\Quote;
1515
use Magento\QuoteGraphQl\Model\Cart\ExtractQuoteAddressData;
16-
use Magento\Framework\GraphQl\Schema\Type\TypeRegistry;
17-
use Magento\Framework\App\ObjectManager;
16+
use Magento\QuoteGraphQl\Model\Cart\ValidateAddressFromSchema;
1817

1918
/**
2019
* @inheritdoc
@@ -27,20 +26,21 @@ class ShippingAddresses implements ResolverInterface
2726
private $extractQuoteAddressData;
2827

2928
/**
30-
* @var TypeRegistry
29+
* @var ValidateAddressFromSchema
3130
*/
32-
private $typeRegistry;
31+
private $validateAddressFromSchema;
3332

3433
/**
3534
* @param ExtractQuoteAddressData $extractQuoteAddressData
36-
* @param TypeRegistry|null $typeRegistry
35+
* @param ValidateAddressFromSchema
3736
*/
3837
public function __construct(
3938
ExtractQuoteAddressData $extractQuoteAddressData,
40-
TypeRegistry $typeRegistry = null
41-
) {
39+
ValidateAddressFromSchema $validateAddressFromSchema
40+
)
41+
{
4242
$this->extractQuoteAddressData = $extractQuoteAddressData;
43-
$this->typeRegistry = $typeRegistry ?: ObjectManager::getInstance()->get(TypeRegistry::class);
43+
$this->validateAddressFromSchema = $validateAddressFromSchema;
4444
}
4545

4646
/**
@@ -61,36 +61,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6161
foreach ($shippingAddresses as $shippingAddress) {
6262
$address = $this->extractQuoteAddressData->execute($shippingAddress);
6363

64-
if ($this->validateAddressFromSchema($address)) {
64+
if ($this->validateAddressFromSchema->execute($address)) {
6565
$addressesData[] = $address;
6666
}
6767
}
6868
}
6969
return $addressesData;
7070
}
71-
72-
/**
73-
* Validate data from address against mandatory fields from graphql schema for address
74-
*
75-
* @param array $address
76-
* @return bool
77-
*/
78-
private function validateAddressFromSchema(array $address) : bool
79-
{
80-
/** @var \Magento\Framework\GraphQL\Schema\Type\Input\InputObjectType $cartAddressInput */
81-
$cartAddressInput = $this->typeRegistry->get('CartAddressInput');
82-
$fields = $cartAddressInput->getFields();
83-
84-
foreach ($fields as $field) {
85-
if ($field->getType() instanceof \Magento\Framework\GraphQL\Schema\Type\NonNull) {
86-
// an array key has to exist but it's value should not be null
87-
if (array_key_exists($field->name, $address)
88-
&& !is_array($address[$field->name])
89-
&& !isset($address[$field->name])) {
90-
return false;
91-
}
92-
}
93-
}
94-
return true;
95-
}
9671
}

0 commit comments

Comments
 (0)