| Conditions | 8 |
| Paths | 17 |
| Total Lines | 60 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 39 | public function importCharacteristics(string $query, string $type) |
||
| 40 | { |
||
| 41 | $queryPictures = '?action=query&redirects=true&format=json&prop=imageinfo&iiprop=url&titles='; |
||
| 42 | $this->queryPages = $queryPages = '?action=query&redirects=true&prop=info&format=json&titles='; |
||
|
|
|||
| 43 | |||
| 44 | $response = $this->httpClient->get(config('wiki.api_uri').$query); |
||
| 45 | $content = json_decode($response->getBody()->getContents(), true); |
||
| 46 | $characteristics = $content['query']['results']; |
||
| 47 | |||
| 48 | foreach ($characteristics as $key => $characteristic){ |
||
| 49 | $page = key($characteristic); |
||
| 50 | $characteristic = last($characteristic); |
||
| 51 | |||
| 52 | $uuid = Uuid::uuid4(); |
||
| 53 | $path = ''; |
||
| 54 | if(isset($characteristic['printouts']['A un fichier d\'icone de caractéristique'][0]['fulltext'])) { |
||
| 55 | $picture = $characteristic['printouts']['A un fichier d\'icone de caractéristique'][0]['fulltext']; |
||
| 56 | $picturesApiUri = config('wiki.api_uri').$queryPictures.$picture; |
||
| 57 | |||
| 58 | $response = $this->httpClient->get($picturesApiUri); |
||
| 59 | $content = json_decode($response->getBody()->getContents(), true); |
||
| 60 | $picturesInfo = $content['query']['pages']; |
||
| 61 | foreach($picturesInfo as $picture) { |
||
| 62 | if (isset(last($picture['imageinfo'])['url'])) { |
||
| 63 | $response = $this->httpClient->get(last($picture['imageinfo'])['url']); |
||
| 64 | $content = $response->getBody()->getContents(); |
||
| 65 | $path = 'public/characteristics/'.$uuid .'.png'; |
||
| 66 | Storage::put('public/characteristics/' . $uuid . '.png', $content); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | $pagesApiUri = config('wiki.api_uri').$queryPages.$page; |
||
| 72 | $response = $this->httpClient->get($pagesApiUri); |
||
| 73 | $content = json_decode($response->getBody()->getContents(), true); |
||
| 74 | |||
| 75 | $pageInfo = last($content['query']['pages']); |
||
| 76 | |||
| 77 | $main = last($characteristic['printouts']['Doit être affiché par défaut']) == "t" ? true : false; |
||
| 78 | $label = $page; |
||
| 79 | $prettyPage = last($characteristic['printouts']['A un label']) !== false ? last($characteristic['printouts']['A un label']) : $pageInfo['title']; |
||
| 80 | |||
| 81 | $characteristicsToSave = [ |
||
| 82 | 'uuid' => $uuid, |
||
| 83 | 'main' => $main, |
||
| 84 | 'priority' => (int)last($characteristic['printouts']['A une priorité d\'affichage']), |
||
| 85 | 'icon' => $path, |
||
| 86 | 'page_label' => $label, |
||
| 87 | 'pretty_page_label' => $prettyPage, |
||
| 88 | 'page_id' => (int)$pageInfo['pageid'], |
||
| 89 | 'type' => $type, |
||
| 90 | 'code' => $pageInfo['title'] |
||
| 91 | ]; |
||
| 92 | |||
| 93 | $model = CharacteristicsModel::where('page_id', (int)$pageInfo['pageid'])->first(); |
||
| 94 | if(!isset($model)) { |
||
| 95 | $model = new CharacteristicsModel(); |
||
| 96 | } |
||
| 97 | $model->fill($characteristicsToSave); |
||
| 98 | $model->save(); |
||
| 99 | } |
||
| 103 |