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