| Total Complexity | 44 |
| Total Lines | 223 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MutationFormResolverPlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MutationFormResolverPlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class MutationFormResolverPlugin extends AbstractDefinitionPlugin |
||
| 37 | { |
||
| 38 | protected $formFactory; |
||
| 39 | |||
| 40 | public function __construct(FormFactory $formFactory) |
||
| 41 | { |
||
| 42 | $this->formFactory = $formFactory; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritDoc} |
||
| 47 | */ |
||
| 48 | public function getName(): string |
||
| 49 | { |
||
| 50 | return 'form'; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritDoc} |
||
| 55 | */ |
||
| 56 | public function buildConfig(ArrayNodeDefinition $root): void |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritDoc} |
||
| 85 | */ |
||
| 86 | public function configure(DefinitionInterface $definition, Endpoint $endpoint, array $config): void |
||
| 87 | { |
||
| 88 | if (!$definition instanceof MutationDefinition || !isset($config['enabled'])) { |
||
| 89 | return; |
||
| 90 | } |
||
| 91 | |||
| 92 | $formType = $config['type'] ?? null; |
||
| 93 | |||
| 94 | //the related class is used to match a form using naming conventions |
||
| 95 | $relatedClass = null; |
||
| 96 | if ($definition instanceof NodeAwareDefinitionInterface && $definition->getNode()) { |
||
| 97 | $relatedClass = $definition->getNode(); |
||
| 98 | if ($class = $endpoint->getClassForType($relatedClass)) { |
||
| 99 | $relatedClass = $class; |
||
| 100 | } |
||
| 101 | } elseif ($definition->getResolver()) { |
||
| 102 | $relatedClass = $definition->getResolver(); |
||
| 103 | } |
||
| 104 | |||
| 105 | //try find the form using a related class |
||
| 106 | if ($relatedClass && (!$formType || true === $formType)) { |
||
| 107 | $bundleNamespace = ClassUtils::relatedBundleNamespace($relatedClass); |
||
| 108 | $formClass = ClassUtils::applyNamingConvention( |
||
| 109 | $bundleNamespace, |
||
| 110 | 'Form\Input', |
||
| 111 | $definition->getNode(), |
||
| 112 | ucfirst($definition->getName()), |
||
| 113 | 'Input' |
||
| 114 | ); |
||
| 115 | if (class_exists($formClass)) { |
||
| 116 | $formType = $formClass; |
||
| 117 | } elseif (true === $formType) { |
||
| 118 | $error = sprintf( |
||
| 119 | 'Can`t find a valid input form type to use in "%s". |
||
| 120 | Create the form "%s" or specify a custom form', |
||
| 121 | $definition->getName(), |
||
| 122 | $formClass |
||
| 123 | ); |
||
| 124 | throw new \RuntimeException($error); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($formType) { |
||
| 129 | $config['type'] = $formType; |
||
| 130 | |||
| 131 | $form = $this->formFactory->create($formType, null, $config['options'] ?? []); |
||
| 132 | $inputObject = $this->createFormInputObject($endpoint, $form, ucfirst($definition->getName())); |
||
| 133 | $endpoint->addType($inputObject); |
||
| 134 | |||
| 135 | $input = new ArgumentDefinition(); |
||
| 136 | $input->setName($config['argument']); |
||
| 137 | $input->setType($inputObject->getName()); |
||
| 138 | |||
| 139 | if ($config['client_mutation_id']) { |
||
| 140 | $clientMutationId = new FieldDefinition(); |
||
| 141 | $clientMutationId->setName('clientMutationId'); |
||
| 142 | $clientMutationId->setType(Types::STRING); |
||
| 143 | $clientMutationId->setDescription('A unique identifier for the client performing the mutation.'); |
||
| 144 | $inputObject->prependField($clientMutationId); |
||
| 145 | } |
||
| 146 | |||
| 147 | $definition->addArgument($input); |
||
| 148 | $definition->setMeta('form', $config); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | public function createFormInputObject(Endpoint $endpoint, FormInterface $form, string $name): InputObjectDefinition |
||
| 196 | } |
||
| 197 | |||
| 198 | public function resolveFormFieldDefinition(FieldDefinition $field, FormInterface $form) |
||
| 259 | } |
||
| 260 | } |
||
| 261 |