| Conditions | 16 |
| Paths | 180 |
| Total Lines | 79 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| Bugs | 3 | 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 |
||
| 90 | public function execute(Arguments $args, ConsoleIo $io) |
||
| 91 | { |
||
| 92 | $sitemap = $args->getOption('sitemap'); |
||
| 93 | $content = ''; |
||
| 94 | if (!empty($sitemap)) { |
||
| 95 | if (strpos($sitemap, 'http://') !== 0 && strpos($sitemap, 'https://') !== 0 && !file_exists($sitemap)) { |
||
| 96 | $io->abort(sprintf('File not found: %s', $sitemap)); |
||
| 97 | } |
||
| 98 | $content = file_get_contents($sitemap); |
||
| 99 | if ($content === false) { |
||
| 100 | $io->abort(sprintf('Error reading sitemap URL: %s', $sitemap)); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | $name = $args->getOption('collection'); |
||
| 105 | $response = $this->client->get('/collections', compact('name')); |
||
| 106 | $collectionId = Hash::get($response->getJson(), '0.cmetadata.id'); |
||
| 107 | if (empty($collectionId)) { |
||
| 108 | $io->abort(sprintf('Collection not found: %s', $name)); |
||
| 109 | } |
||
| 110 | $collection = $this->Collections->get($collectionId, ['contain' => ['HasDocuments']]); |
||
| 111 | $currentUrls = array_filter(array_map(function ($link) { |
||
| 112 | $link = $link->getTable()->get($link->id); |
||
| 113 | |||
| 114 | return $link->get('url'); |
||
| 115 | }, |
||
| 116 | (array)$collection->get('has_documents'))); |
||
| 117 | $prefix = $args->getOption('prefix'); |
||
| 118 | |||
| 119 | $blackListPath = (string)$args->getOption('black-list'); |
||
| 120 | $blackList = []; |
||
| 121 | if (!empty($blackListPath)) { |
||
| 122 | if (!file_exists($blackListPath)) { |
||
| 123 | $io->abort(sprintf('Blacklist file not found: %s', $blackListPath)); |
||
| 124 | } |
||
| 125 | $blackList = (array)file($blackListPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
||
| 126 | } |
||
| 127 | |||
| 128 | $xml = simplexml_load_string($content); |
||
| 129 | $json = json_encode($xml); |
||
| 130 | $data = (array)json_decode($json, true); |
||
| 131 | $urls = Hash::extract($data, 'url.{n}.loc'); |
||
| 132 | if (empty($urls)) { |
||
| 133 | $io->abort('No URLs found in sitemap'); |
||
| 134 | } |
||
| 135 | $entities = []; |
||
| 136 | LoggedUser::setUserAdmin(); |
||
| 137 | foreach ($urls as $url) { |
||
| 138 | if ( |
||
| 139 | in_array($url, $currentUrls) || |
||
| 140 | in_array(urldecode($url), $currentUrls) || |
||
| 141 | in_array($url, $blackList) || |
||
| 142 | ($prefix && strpos($url, $prefix) !== 0) |
||
| 143 | ) { |
||
| 144 | continue; |
||
| 145 | } |
||
| 146 | $io->info('Adding link: ' . $url); |
||
| 147 | $data = [ |
||
| 148 | 'status' => 'on', |
||
| 149 | 'title' => $url, |
||
| 150 | 'url' => $url, |
||
| 151 | 'extra' => [ |
||
| 152 | 'brevia' => [ |
||
| 153 | 'metadata' => [ |
||
| 154 | 'type' => 'links', |
||
| 155 | 'url' => $url, |
||
| 156 | ], |
||
| 157 | ], |
||
| 158 | ], |
||
| 159 | ]; |
||
| 160 | $entity = $this->Links->newEntity($data); |
||
| 161 | $entities[] = $this->Links->saveOrFail($entity); |
||
| 162 | } |
||
| 163 | // @phpstan-ignore-next-line |
||
| 164 | $this->Collections->addRelated($collection, 'has_documents', $entities); |
||
| 165 | |||
| 166 | $io->out('Done. Link added successfully: ' . count($entities)); |
||
| 167 | |||
| 168 | return null; |
||
| 169 | } |
||
| 171 |