SearchStructure   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 19
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 17 3
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\Context\Queries;
5
6
7
use GuzzleHttp\Client;
8
9
/**
10
 * Search structure remotely with the wiki api
11
 */
12
class SearchStructure
13
{
14
    public function execute(string $search):array
15
    {
16
        $client = new Client();
17
        $uri = config('wiki.api_uri').'?action=query&list=search&srwhat=text&srsearch='.$search.'&srqiprofile=classic_noboostlinks&srnamespace=3000&format=json';
18
        try {
19
            $response = $client->get($uri);
20
        }catch (\Throwable $e){
21
            return ['results' => []];
22
        }
23
        $content = json_decode($response->getBody()->getContents(), true);
24
        if(isset($content['query']['search'])){
25
            $results = array_column($content['query']['search'], 'title');
26
            return array_map(function ($item){
27
                return str_replace('Structure:', '', $item);
28
            }, $results);
29
        }
30
        return ['results' => []];
31
    }
32
}
33