Conditions | 22 |
Paths | 15 |
Total Lines | 87 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
72 | public function handleAction(string $action, array $selected_parts, ?int $target_id, ?string $redirect_url = null): ?RedirectResponse |
||
73 | { |
||
74 | if ($action === 'add_to_project') { |
||
75 | return new RedirectResponse( |
||
76 | $this->urlGenerator->generate('project_add_parts', [ |
||
77 | 'id' => $target_id, |
||
78 | 'parts' => implode(',', array_map(static fn (Part $part) => $part->getID(), $selected_parts)), |
||
79 | '_redirect' => $redirect_url |
||
80 | ]) |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | if ($action === 'generate_label' || $action === 'generate_label_lot') { |
||
85 | //For parts we can just use the comma separated part IDs |
||
86 | if ($action === 'generate_label') { |
||
87 | $targets = implode(',', array_map(static fn (Part $part) => $part->getID(), $selected_parts)); |
||
88 | } else { //For lots we have to extract the part lots |
||
89 | $targets = implode(',', array_map(static function (Part $part) { |
||
90 | //We concat the lot IDs of every part with a comma (which are later concated with a comma too per part) |
||
91 | return implode(',', array_map(static fn (PartLot $lot) => $lot->getID(), $part->getPartLots()->toArray())); |
||
92 | }, $selected_parts)); |
||
93 | } |
||
94 | |||
95 | return new RedirectResponse( |
||
96 | $this->urlGenerator->generate($target_id !== 0 && $target_id !== null ? 'label_dialog_profile' : 'label_dialog', [ |
||
97 | 'profile' => $target_id, |
||
98 | 'target_id' => $targets, |
||
99 | 'generate' => '1', |
||
100 | 'target_type' => $action === 'generate_label_lot' ? 'part_lot' : 'part', |
||
101 | ]) |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | |||
106 | //Iterate over the parts and apply the action to it: |
||
107 | foreach ($selected_parts as $part) { |
||
108 | if (!$part instanceof Part) { |
||
109 | throw new InvalidArgumentException('$selected_parts must be an array of Part elements!'); |
||
110 | } |
||
111 | |||
112 | //We modify parts, so you have to have the permission to modify it |
||
113 | $this->denyAccessUnlessGranted('edit', $part); |
||
114 | |||
115 | switch ($action) { |
||
116 | case 'favorite': |
||
117 | $this->denyAccessUnlessGranted('change_favorite', $part); |
||
118 | $part->setFavorite(true); |
||
119 | break; |
||
120 | case 'unfavorite': |
||
121 | $this->denyAccessUnlessGranted('change_favorite', $part); |
||
122 | $part->setFavorite(false); |
||
123 | break; |
||
124 | case 'set_needs_review': |
||
125 | $this->denyAccessUnlessGranted('edit', $part); |
||
126 | $part->setNeedsReview(true); |
||
127 | break; |
||
128 | case 'unset_needs_review': |
||
129 | $this->denyAccessUnlessGranted('edit', $part); |
||
130 | $part->setNeedsReview(false); |
||
131 | break; |
||
132 | case 'delete': |
||
133 | $this->denyAccessUnlessGranted('delete', $part); |
||
134 | $this->entityManager->remove($part); |
||
135 | break; |
||
136 | case 'change_category': |
||
137 | $this->denyAccessUnlessGranted('@categories.read'); |
||
138 | $part->setCategory($this->entityManager->find(Category::class, $target_id)); |
||
139 | break; |
||
140 | case 'change_footprint': |
||
141 | $this->denyAccessUnlessGranted('@footprints.read'); |
||
142 | $part->setFootprint(null === $target_id ? null : $this->entityManager->find(Footprint::class, $target_id)); |
||
143 | break; |
||
144 | case 'change_manufacturer': |
||
145 | $this->denyAccessUnlessGranted('@manufacturers.read'); |
||
146 | $part->setManufacturer(null === $target_id ? null : $this->entityManager->find(Manufacturer::class, $target_id)); |
||
147 | break; |
||
148 | case 'change_unit': |
||
149 | $this->denyAccessUnlessGranted('@measurement_units.read'); |
||
150 | $part->setPartUnit(null === $target_id ? null : $this->entityManager->find(MeasurementUnit::class, $target_id)); |
||
151 | break; |
||
152 | |||
153 | default: |
||
154 | throw new InvalidArgumentException('The given action is unknown! ('.$action.')'); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | return null; |
||
159 | } |
||
178 |