| Conditions | 14 |
| Paths | 160 |
| Total Lines | 69 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 75 | public function generator(Request $request, ?LabelProfile $profile = null) |
||
| 76 | { |
||
| 77 | $this->denyAccessUnlessGranted('@labels.create_labels'); |
||
| 78 | |||
| 79 | //If we inherit a LabelProfile, the user need to have access to it... |
||
| 80 | if ($profile !== null) { |
||
| 81 | $this->denyAccessUnlessGranted('read', $profile); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($profile) { |
||
| 85 | $label_options = $profile->getOptions(); |
||
| 86 | } else { |
||
| 87 | $label_options = new LabelOptions(); |
||
| 88 | } |
||
| 89 | |||
| 90 | //We have to disable the options, if twig mode is selected and user is not allowed to use it. |
||
| 91 | $disable_options = $label_options->getLinesMode() === 'twig' && !$this->isGranted("@labels.use_twig"); |
||
| 92 | |||
| 93 | $form = $this->createForm(LabelDialogType::class, null, [ |
||
| 94 | 'disable_options' => $disable_options, |
||
| 95 | ]); |
||
| 96 | |||
| 97 | //Try to parse given target_type and target_id |
||
| 98 | $target_type = $request->query->get('target_type', null); |
||
| 99 | $target_id = $request->query->get('target_id', null); |
||
| 100 | $generate = $request->query->getBoolean('generate', false); |
||
| 101 | |||
| 102 | if ($profile === null && is_string($target_type)) { |
||
| 103 | $label_options->setSupportedElement($target_type); |
||
| 104 | } |
||
| 105 | if (is_string($target_id)) { |
||
| 106 | $form['target_id']->setData($target_id); |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | $form['options']->setData($label_options); |
||
| 111 | $form->handleRequest($request); |
||
| 112 | |||
| 113 | /** @var LabelOptions $form_options */ |
||
| 114 | $form_options = $form['options']->getData(); |
||
| 115 | |||
| 116 | $pdf_data = null; |
||
| 117 | $filename = 'invalid.pdf'; |
||
| 118 | |||
| 119 | //Generate PDF either when the form is submitted and valid, or the form was not submit yet, and generate is set |
||
| 120 | if (($form->isSubmitted() && $form->isValid()) || ($generate && !$form->isSubmitted() && $profile !== null)) { |
||
| 121 | $target_id = (string) $form->get('target_id')->getData(); |
||
| 122 | $targets = $this->findObjects($form_options->getSupportedElement(), $target_id); |
||
| 123 | if (!empty($targets)) { |
||
| 124 | try { |
||
| 125 | $pdf_data = $this->labelGenerator->generateLabel($form_options, $targets); |
||
| 126 | $filename = $this->getLabelName($targets[0], $profile); |
||
| 127 | } catch (TwigModeException $exception) { |
||
| 128 | $form->get('options')->get('lines')->addError(new FormError($exception->getMessage())); |
||
| 129 | } |
||
| 130 | |||
| 131 | } else { |
||
| 132 | //$this->addFlash('warning', 'label_generator.no_entities_found'); |
||
| 133 | $form->get('target_id')->addError( |
||
| 134 | new FormError($this->translator->trans('label_generator.no_entities_found')) |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return $this->render('LabelSystem/dialog.html.twig', [ |
||
| 140 | 'form' => $form->createView(), |
||
| 141 | 'pdf_data' => $pdf_data, |
||
| 142 | 'filename' => $filename, |
||
| 143 | 'profile' => $profile, |
||
| 144 | ]); |
||
| 167 | } |