Passed
Push — master ( 8f8314...6e7f23 )
by Gabriel Felipe
04:42 queued 10s
created

Search::search()   A

Complexity

Conditions 4
Paths 17

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 53
c 0
b 0
f 0
ccs 28
cts 28
cp 1
rs 9.0254
cc 4
nc 17
nop 1
crap 4

How to fix   Long Method   

Long Method

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:

1
<?php declare(strict_types=1);
2
3
namespace GSoares\GoogleTrends\Search\Psr7;
4
5
use DateTimeImmutable;
6
use GSoares\GoogleTrends\Search\SearchFilter;
7
use GSoares\GoogleTrends\Search\SearchInterface;
8
use GuzzleHttp\Psr7\Response;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Throwable;
12
13
/**
14
 * @author Gabriel Felipe Soares <[email protected]>
15
 */
16
class Search
17
{
18
    /**
19
     * @var SearchInterface
20
     */
21
    private $search;
22
23 8
    public function __construct(SearchInterface $search)
24
    {
25 8
        $this->search = $search;
26 8
    }
27
28 8
    public function search(ServerRequestInterface $request): ResponseInterface
29
    {
30
        try {
31 8
            $params = $request->getQueryParams();
32
33 8
            $searchFilter = (new SearchFilter())
34 8
                ->withCategory((int)($params["categoryId"] ?? 0))
35 8
                ->withSearchTerm($params["searchTerm"] ?? 'google')
36 8
                ->withLocation($params["location"] ?? 'US')
37 8
                ->withinInterval(
38 8
                    new DateTimeImmutable($params["intervalFrom"] ?? 'now -7 days'),
39 8
                    new DateTimeImmutable($params["intervalTo"] ?? 'yesterday')
40
                )
41 8
                ->withLanguage($params["language"] ?? 'en-US');
42
43 8
            $this->configureSearchSource($params["searchSource"] ?? 'web', $searchFilter);
44
45 8
            if (filter_var($params["withTopMetrics"] ?? false, FILTER_VALIDATE_BOOLEAN)) {
46 1
                $searchFilter->withTopMetrics();
47
            }
48
49 8
            if (filter_var($params["withRisingMetrics"] ?? false, FILTER_VALIDATE_BOOLEAN)) {
50 1
                $searchFilter->withRisingMetrics();
51
            }
52
53 8
            return new Response(
54 8
                200,
55
                [
56 8
                    'Content-Type' => 'application/json',
57
                ],
58 8
                json_encode($this->search->search($searchFilter)->jsonSerialize())
59
            );
60 2
        } catch (Throwable $exception) {
61 2
            return new Response(
62 2
                400,
63
                [
64 2
                    'Content-Type' => 'application/json',
65
                ],
66 2
                json_encode(
67
                    [
68
                        'errors' => [
69
                            [
70 2
                                'status' => 400,
71 2
                                'code' => $exception->getCode(),
72 2
                                'title' => $exception->getMessage(),
73 2
                                'details' => $exception->getMessage(),
74
                            ]
75
                        ]
76
                    ]
77
                )
78
            );
79
        }
80
    }
81
82 8
    private function configureSearchSource(string $searchSource, SearchFilter $searchFilter): void
83
    {
84 8
        if ($searchSource === SearchFilter::SEARCH_SOURCE_NEWS) {
85 1
            $searchFilter->considerNewsSearch();
86
87 1
            return;
88
        }
89
90 7
        if ($searchSource === SearchFilter::SEARCH_SOURCE_IMAGES) {
91 1
            $searchFilter->considerImageSearch();
92
93 1
            return;
94
        }
95
96 6
        if ($searchSource === SearchFilter::SEARCH_SOURCE_YOUTUBE) {
97 2
            $searchFilter->considerYoutubeSearch();
98
99 2
            return;
100
        }
101
102 4
        if ($searchSource === SearchFilter::SEARCH_SOURCE_GOOGLE_SHOPPING) {
103 1
            $searchFilter->considerGoogleShoppingSearch();
104
105 1
            return;
106
        }
107
108 3
        $searchFilter->considerWebSearch();
109 3
    }
110
}
111