Conditions | 7 |
Paths | 10 |
Total Lines | 88 |
Code Lines | 53 |
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 |
||
81 | protected function importEvent(array $data) |
||
82 | { |
||
83 | $baseUrl = $this->baseUrl; |
||
84 | |||
85 | if ($data['organizer'] == "") { |
||
86 | $this->logger->info(sprintf( |
||
87 | "Event \"%s\" not imported. Missing organizer.", |
||
88 | $data['title'] |
||
89 | )); |
||
90 | |||
91 | return; |
||
92 | } |
||
93 | |||
94 | $data['organizer'] = urldecode(html_entity_decode($data['organizer'])); |
||
95 | $organizer = null; |
||
96 | |||
97 | $parts = explode("LC ", $data['organizer']); |
||
98 | |||
99 | if (count($parts) < 2) { |
||
100 | return; |
||
101 | } |
||
102 | |||
103 | $data['organizer'] = "LC " . $parts[1]; |
||
104 | |||
105 | try { |
||
106 | $organizer = $this->lcs->getByTitle($data['organizer']); |
||
107 | } catch (ResourceNotFoundException $e) { |
||
108 | $this->logger->info(sprintf( |
||
109 | "Event \"%s\" not imported. Organizer \"%s\" not found.", |
||
110 | $data['title'], |
||
111 | $data['organizer'] |
||
112 | )); |
||
113 | |||
114 | return; |
||
115 | } |
||
116 | |||
117 | if ($data['title'] == "") { |
||
118 | $this->logger->info("Event not imported. Missing title."); |
||
119 | return; |
||
120 | } |
||
121 | |||
122 | $title = $data['title']; |
||
123 | $description = html_entity_decode($data['details']); |
||
124 | $hostId = $organizer->getId(); |
||
125 | $url = $baseUrl . $data['url']; |
||
126 | |||
127 | // @todo refactor |
||
128 | try { |
||
129 | $imgRaw = imagecreatefromstring(file_get_contents($baseUrl . $data['image'])); |
||
130 | |||
131 | } catch (\Exception $e) { |
||
132 | $this->logger->info("could not fetch image for ". $data['url']); |
||
133 | return; |
||
134 | } |
||
135 | |||
136 | imagejpeg($imgRaw, '/tmp/tmp.jpg',100); |
||
137 | imagedestroy($imgRaw); |
||
138 | $filename = rand(0, 1000) .".jpg"; |
||
139 | $image = new EventImage($filename, '/tmp/tmp.jpg'); |
||
140 | $startDate = new Carbon($data['start']); |
||
141 | $endDate = new Carbon($data['end']); |
||
142 | $deadline = new Carbon($data['deadline']); |
||
143 | |||
144 | try { |
||
145 | |||
146 | $event = $this->events->getByTitle($title); |
||
147 | $id = $event->getId(); |
||
148 | |||
149 | $command = new UpdateEventCommand($id, $title, $description, $hostId, $url, $image, $startDate, $endDate, $deadline); |
||
150 | |||
151 | $this->logger->info(sprintf( |
||
152 | "Event \"%s\" already exists. Will be updated if needed.", |
||
153 | $title |
||
154 | )); |
||
155 | |||
156 | } catch (ResourceNotFoundException $e) { |
||
157 | |||
158 | $command = new StoreEventCommand($title, $description, $hostId, $url, $image, $startDate, $endDate, $deadline); |
||
159 | |||
160 | $this->logger->info(sprintf( |
||
161 | "Event \"%s\" imported", |
||
162 | $title |
||
163 | )); |
||
164 | |||
165 | } |
||
166 | |||
167 | $this->commandBus->dispatch($command); |
||
168 | } |
||
169 | } |
||
170 |