GetCoupon.php
1 month ago
GetProduct.php
1 month ago
ListCoupons.php
1 month ago
ListProducts.php
1 month ago
GetCoupon.php
87 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | // THIS FILE IS AUTO-GENERATED. DO NOT EDIT MANUALLY. |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLQueries; |
| 7 | |
| 8 | use Automattic\WooCommerce\Api\Queries\Coupons\GetCoupon as GetCouponCommand; |
| 9 | use Automattic\WooCommerce\Api\Infrastructure\QueryInfoExtractor; |
| 10 | use Automattic\WooCommerce\Api\Infrastructure\ResolverHelpers; |
| 11 | use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Output\Coupon as CouponType; |
| 12 | use Automattic\WooCommerce\Api\Infrastructure\Schema\ResolveInfo; |
| 13 | use Automattic\WooCommerce\Api\Infrastructure\Schema\Type; |
| 14 | |
| 15 | class GetCoupon { |
| 16 | public static function get_field_definition(): array { |
| 17 | return array( |
| 18 | 'type' => CouponType::get(), |
| 19 | 'description' => __( 'Retrieve a single coupon by ID or code. Exactly one of the two arguments must be provided.', 'woocommerce' ), |
| 20 | 'authorization' => array( |
| 21 | array( |
| 22 | 'attribute' => 'RequiredCapability', |
| 23 | 'args' => array( |
| 24 | 0 => 'read_private_shop_coupons', |
| 25 | ), |
| 26 | ), |
| 27 | ), |
| 28 | 'args' => array( |
| 29 | 'id' => array( |
| 30 | 'type' => Type::int(), |
| 31 | 'description' => __( 'The ID of the coupon to retrieve.', 'woocommerce' ), |
| 32 | 'defaultValue' => null, |
| 33 | ), |
| 34 | 'code' => array( |
| 35 | 'type' => Type::string(), |
| 36 | 'description' => __( 'The coupon code to look up.', 'woocommerce' ), |
| 37 | 'defaultValue' => null, |
| 38 | ), |
| 39 | ), |
| 40 | 'resolve' => array( self::class, 'resolve' ), |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public static function resolve( mixed $root, array $args, mixed $context, ResolveInfo $info ): mixed { |
| 45 | // Standalone authorization gate: no authorize() method on the command, |
| 46 | // so the autodiscovered authorization attributes are the sole guard. |
| 47 | if ( ! self::compute_preauthorized( $context['principal'] ) ) { |
| 48 | throw ResolverHelpers::build_authorization_error( $context['principal'] ); |
| 49 | } |
| 50 | |
| 51 | // Publish the root query's metadata so downstream field-level |
| 52 | // authorization gates can read it via `$_metadata['query']`. |
| 53 | // $context is an ArrayObject (see GraphQLController::process_request()) |
| 54 | // so the mutation propagates to nested resolvers. |
| 55 | $context['_query_metadata'] = array(); |
| 56 | |
| 57 | $command = \Automattic\WooCommerce\Api\Infrastructure\ClassResolver::resolve_class( GetCouponCommand::class ); |
| 58 | |
| 59 | $execute_args = array(); |
| 60 | if ( array_key_exists( 'id', $args ) ) { |
| 61 | $execute_args['id'] = $args['id']; |
| 62 | } |
| 63 | if ( array_key_exists( 'code', $args ) ) { |
| 64 | $execute_args['code'] = $args['code']; |
| 65 | } |
| 66 | |
| 67 | $result = ResolverHelpers::execute_command( $command, $execute_args ); |
| 68 | |
| 69 | return $result; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Compute the value `_preauthorized` would carry for a given principal — |
| 74 | * the AND of the autodiscovered authorization attributes' authorize() |
| 75 | * outcomes on this command. Single source of truth for both the resolver's |
| 76 | * own gates and external (code-API) callers asking about authorization |
| 77 | * without going through GraphQL execution. |
| 78 | * |
| 79 | * Returns true vacuously when the command has no authorization attributes |
| 80 | * (in that case authorize() on the command is the sole guard, and that |
| 81 | * method should be consulted instead). |
| 82 | */ |
| 83 | public static function compute_preauthorized( \Automattic\WooCommerce\Api\Infrastructure\Principal $principal ): bool { |
| 84 | return ( new \Automattic\WooCommerce\Api\Attributes\RequiredCapability( 'read_private_shop_coupons' ) )->authorize( $principal ); |
| 85 | } |
| 86 | } |
| 87 |