Conditions | 11 |
Paths | 14 |
Total Lines | 64 |
Code Lines | 38 |
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 |
||
123 | protected function execute(InputInterface $input, OutputInterface $output) |
||
124 | { |
||
125 | $defaultRedirectType = self::REDIRECT_TYPE_MAPPING[$input->getOption('default-redirect-type')]; |
||
126 | $defaultConflictingPublicUrlStrategy = self::DEFAULT_CONFLICTING_PUBLIC_URL_STRATEGY_MAPPING[$input->getOption('default-conflicting-public-url-strategy')]; |
||
127 | $defaultConflictingInternalUrlStrategy = self::DEFAULT_CONFLICTING_INTERNAL_URL_STRATEGY_MAPPING[$input->getOption('default-conflicting-internal-url-strategy')]; |
||
128 | $csvDelimiter = $input->getOption('csv-delimiter'); |
||
129 | $csvEnclosure = $input->getOption('csv-enclosure'); |
||
130 | |||
131 | $aliasingService = $this->getContainer()->get('zicht_url.aliasing'); |
||
132 | $flush = $aliasingService->setIsBatch(true); |
||
133 | |||
134 | $handle = fopen($input->getArgument('file'), 'r'); |
||
135 | if (false === $handle) { |
||
136 | throw new \Exception('Can not open input file'); |
||
137 | } |
||
138 | |||
139 | $lineNumber = 0; |
||
140 | |||
141 | if ($input->getOption('skip-header')) { |
||
142 | $lineNumber++; |
||
143 | $data = fgetcsv($handle, null, $csvDelimiter, $csvEnclosure); |
||
144 | if (false === $data) { |
||
145 | throw new \Exception(sprintf('Can not read line %s in input file', $lineNumber)); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | while ($data = fgetcsv($handle, null, $csvDelimiter, $csvEnclosure)) { |
||
150 | $lineNumber++; |
||
151 | |||
152 | if (false === $data) { |
||
153 | throw new \Exception(sprintf('Can not read line %s in input file', $lineNumber)); |
||
154 | } |
||
155 | |||
156 | if (null === $data || [null] === $data) { |
||
157 | // skip empty line |
||
158 | continue; |
||
159 | } |
||
160 | |||
161 | if (sizeof($data) < 2) { |
||
162 | throw new \Exception('Every line in the file must have at least the publicUrl and internalUrl'); |
||
163 | } |
||
164 | |||
165 | $publicUrl = trim($data[0]); |
||
166 | $internalUrl = trim($data[1]); |
||
167 | $type = $this->parseInputToMapping(self::REDIRECT_TYPE_MAPPING, $data, 2, $defaultRedirectType); |
||
168 | $conflictingPublicUrlStrategy = $this->parseInputToMapping(self::DEFAULT_CONFLICTING_PUBLIC_URL_STRATEGY_MAPPING, $data, 3, $defaultConflictingPublicUrlStrategy); |
||
169 | $conflictingInternalUrlStrategy = $this->parseInputToMapping(self::DEFAULT_CONFLICTING_INTERNAL_URL_STRATEGY_MAPPING, $data, 3, $defaultConflictingInternalUrlStrategy); |
||
170 | |||
171 | $realInternalUrl = $aliasingService->hasInternalAlias($internalUrl); |
||
172 | if (null !== $realInternalUrl && $internalUrl !== $realInternalUrl) { |
||
173 | // the $internalUrl given in the csv is actually known as a public url in our system. |
||
174 | // the $publicUrl given in the csv should redirect directly to this known public url. |
||
175 | $redirectDescription = sprintf('%s -> %s -> %s', $publicUrl, $internalUrl, $realInternalUrl); |
||
176 | $internalUrl = $realInternalUrl; |
||
177 | } else { |
||
178 | $redirectDescription = sprintf('%s -> %s', $publicUrl, $internalUrl); |
||
179 | } |
||
180 | |||
181 | // perform aliasing |
||
182 | $aliasingService->addAlias($publicUrl, $internalUrl, $type, $conflictingPublicUrlStrategy, $conflictingInternalUrlStrategy); |
||
183 | $output->writeln(sprintf('%s %s %s', $lineNumber, $type, $redirectDescription)); |
||
184 | } |
||
185 | |||
186 | $flush(); |
||
187 | } |
||
218 |