Conditions | 11 |
Paths | 62 |
Total Lines | 83 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
62 | public function exportEntityFromRequest($entity, Request $request) : Response |
||
63 | { |
||
64 | $format = $request->get('format') ?? "json"; |
||
65 | |||
66 | //Check if we have one of the supported formats |
||
67 | if (!in_array($format, ['json', 'csv', 'yaml', 'xml'])) { |
||
68 | throw new \InvalidArgumentException("Given format is not supported!"); |
||
69 | } |
||
70 | |||
71 | //Check export verbosity level |
||
72 | $level = $request->get('level') ?? 'extended'; |
||
73 | if (!in_array($level, ['simple', 'extended', 'full'])) { |
||
74 | throw new \InvalidArgumentException('Given level is not supported!'); |
||
75 | } |
||
76 | |||
77 | //Check for include children option |
||
78 | $include_children = $request->get('include_children') ?? false; |
||
79 | |||
80 | //Check which groups we need to export, based on level and include_children |
||
81 | $groups = array($level); |
||
82 | if ($include_children) { |
||
83 | $groups[] = 'include_children'; |
||
84 | } |
||
85 | |||
86 | //Plain text should work for all types |
||
87 | $content_type = "text/plain"; |
||
88 | |||
89 | //Try to use better content types based on the format |
||
90 | switch ($format) { |
||
91 | case 'xml': |
||
92 | $content_type = "application/xml"; |
||
93 | break; |
||
94 | case 'json': |
||
95 | $content_type = "application/json"; |
||
96 | break; |
||
97 | } |
||
98 | |||
99 | //Ensure that we always serialize an array. This makes it easier to import the data again. |
||
100 | if(is_array($entity)) { |
||
101 | $entity_array = $entity; |
||
102 | } else { |
||
103 | $entity_array = [$entity]; |
||
104 | } |
||
105 | |||
106 | $response = new Response($this->serializer->serialize($entity_array, $format, |
||
107 | [ |
||
108 | 'groups' => $groups, |
||
109 | 'as_collection' => true, |
||
110 | 'csv_delimiter' => ';', //Better for Excel |
||
111 | 'xml_root_node_name' => 'PartDBExport' |
||
112 | ])); |
||
113 | |||
114 | $response->headers->set('Content-Type', $content_type); |
||
115 | |||
116 | //If view option is not specified, then download the file. |
||
117 | if (!$request->get('view')) { |
||
118 | if ($entity instanceof NamedDBElement) { |
||
119 | $entity_name = $entity->getName(); |
||
120 | } elseif (is_array($entity)) { |
||
121 | if (empty($entity)) { |
||
122 | throw new \InvalidArgumentException('$entity must not be empty!'); |
||
123 | } |
||
124 | |||
125 | //Use the class name of the first element for the filename |
||
126 | $reflection = new \ReflectionClass($entity[0]); |
||
127 | $entity_name = $reflection->getShortName(); |
||
128 | } else { |
||
129 | throw new \InvalidArgumentException('$entity type is not supported!'); |
||
130 | } |
||
131 | |||
132 | |||
133 | $filename = "export_" . $entity_name . "_" . $level . "." . $format; |
||
134 | |||
135 | // Create the disposition of the file |
||
136 | $disposition = $response->headers->makeDisposition( |
||
137 | ResponseHeaderBag::DISPOSITION_ATTACHMENT, |
||
138 | $filename |
||
139 | ); |
||
140 | // Set the content disposition |
||
141 | $response->headers->set('Content-Disposition', $disposition); |
||
142 | } |
||
143 | |||
144 | return $response; |
||
145 | } |
||
146 | } |