src/EventSubscriber/BuildReportMutationSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\BuildReportService;
  4. use GraphQL\Type\Definition\Type;
  5. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\MutationTypeEvent;
  6. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\MutationEvents;
  7. use Pimcore\Bundle\DataHubBundle\GraphQL\ElementDescriptor;
  8. use Pimcore\Bundle\DataHubBundle\GraphQL\FieldHelper\DataObjectFieldHelper;
  9. use Pimcore\Model\DataObject\ClassDefinition;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class BuildReportMutationSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var BuildReportService
  15.      */
  16.     protected $buildReportService;
  17.     /**
  18.      * @var DataObjectFieldHelper
  19.      */
  20.     protected $dataObjectFieldHelper;
  21.     public function __construct(BuildReportService $buildReportServiceDataObjectFieldHelper $dataObjectFieldHelper)
  22.     {
  23.         $this->buildReportService $buildReportService;
  24.         $this->dataObjectFieldHelper $dataObjectFieldHelper;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             MutationEvents::PRE_BUILD => 'onPreBuild',
  30.         ];
  31.     }
  32.     /**
  33.      * @param MutationTypeEvent $event
  34.      *
  35.      * @throws \Exception
  36.      */
  37.     public function onPreBuild(MutationTypeEvent $event)
  38.     {
  39.         $config $event->getConfig();
  40.         $opName "makeBuildReport";
  41.         $inputType = new \GraphQL\Type\Definition\InputObjectType([
  42.             'name' => "makeBuildReportInput",
  43.             'fields' => [
  44.                 'buildingMaterialIds' => [
  45.                     'type' => Type::listOf(Type::id())
  46.                 ],
  47.                 'numberOfHouseHoldMembers' => [
  48.                     'type' => Type::int()
  49.                 ],
  50.                 'buildSquareFootage' => [
  51.                     'type' => Type::int()
  52.                 ],
  53.                 'budget' => [
  54.                     'type' => Type::int()
  55.                 ],
  56.                 'region' => [
  57.                     'type' => Type::id()
  58.                 ],
  59.             ]
  60.         ]);
  61.         $operation = [
  62.             'type' => \Pimcore\Bundle\DataHubBundle\GraphQL\ClassTypeDefinitions::get(ClassDefinition::getByName('BuildReport')),           // the result type
  63.             'args' => [
  64.                 'input' => ['type' => $inputType],
  65.             ], 'resolve' => function ($source$args$context\GraphQL\Type\Definition\ResolveInfo $info) {
  66.                 $inputData $args['input'];
  67.                 $buildReport $this->buildReportService->createBuildReportFromMutationParams(
  68.                     $inputData['buildingMaterialIds'],
  69.                     $inputData['numberOfHouseHoldMembers'],
  70.                     $inputData['buildSquareFootage'],
  71.                     $inputData['budget'],
  72.                     $inputData['region']
  73.                 );
  74.                 $data = new ElementDescriptor($buildReport);
  75.                 $data['id'] = $buildReport->getId();
  76.                 return $this->dataObjectFieldHelper->extractData($data$buildReport$args$context$info);
  77.             }
  78.         ];
  79.         $config['fields'][$opName] = $operation;
  80.         $event->setConfig($config);
  81.     }
  82. }