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