| Conditions | 8 |
| Paths | 24 |
| Total Lines | 61 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | 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 |
||
| 76 | public function execute(Arguments $args, ConsoleIo $io) |
||
| 77 | { |
||
| 78 | $sitemap = $args->getOption('sitemap'); |
||
| 79 | $content = file_get_contents($sitemap); |
||
| 80 | if (!$content) { |
||
| 81 | $io->abort(sprintf('Sitemap content not found: %s', $sitemap)); |
||
| 82 | } |
||
| 83 | |||
| 84 | $name = $args->getOption('collection'); |
||
| 85 | $response = $this->client->get('/collections', compact('name')); |
||
| 86 | $collectionId = Hash::get($response->getJson(), '0.cmetadata.id'); |
||
| 87 | if (empty($collectionId)) { |
||
| 88 | $io->abort(sprintf('Collection not found: %s', $name)); |
||
| 89 | } |
||
| 90 | $collection = $this->Collections->get($collectionId, ['contain' => ['HasDocuments']]); |
||
| 91 | $currentUrls = array_filter(array_map(function ($link) { |
||
| 92 | $link = $link->getTable()->get($link->id); |
||
| 93 | |||
| 94 | return $link->get('url'); |
||
| 95 | }, |
||
| 96 | $collection->has_documents)); |
||
| 97 | $prefix = $args->getOption('prefix'); |
||
| 98 | |||
| 99 | $xml = simplexml_load_string($content); |
||
| 100 | $json = json_encode($xml); |
||
| 101 | $data = (array)json_decode($json, true); |
||
| 102 | $urls = Hash::extract($data, 'url.{n}.loc'); |
||
| 103 | if (empty($urls)) { |
||
| 104 | $io->abort('No URLs found in sitemap'); |
||
| 105 | } |
||
| 106 | $entities = []; |
||
| 107 | LoggedUser::setUserAdmin(); |
||
| 108 | $this->Links = $this->fetchTable('Links'); |
||
| 109 | foreach ($urls as $url) { |
||
| 110 | if (in_array($url, $currentUrls) || ($prefix && strpos($url, $prefix) !== 0)) { |
||
| 111 | continue; |
||
| 112 | } |
||
| 113 | $io->info('Adding link: ' . $url); |
||
| 114 | $data = [ |
||
| 115 | 'status' => 'on', |
||
| 116 | 'title' => $url, |
||
| 117 | 'url' => $url, |
||
| 118 | 'extra' => [ |
||
| 119 | 'brevia' => [ |
||
| 120 | 'metadata' => [ |
||
| 121 | 'type' => 'links', |
||
| 122 | 'url' => $url, |
||
| 123 | ], |
||
| 124 | 'options' => $this->linkOptions($url, (array)$collection->get('link_load_options')), |
||
| 125 | ], |
||
| 126 | ], |
||
| 127 | ]; |
||
| 128 | $entity = $this->Links->newEntity($data); |
||
| 129 | $entities[] = $this->Links->saveOrFail($entity); |
||
| 130 | } |
||
| 131 | /** @phpstan-ignore-next-line */ |
||
| 132 | $this->Collections->addRelated($collection, 'has_documents', $entities); |
||
| 133 | |||
| 134 | $io->out('Done. Link added successfully: ' . count($entities)); |
||
| 135 | |||
| 136 | return null; |
||
| 137 | } |
||
| 162 |