Completed
Push — master ( 4ffc8b...8e0049 )
by Gabriel Felipe
02:04 queued 10s
created

ExploreResultCollection::getResultByMethod()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
2
3
namespace GSoares\GoogleTrends\Result;
4
5
use GSoares\GoogleTrends\Error\GoogleTrendsException;
6
7
/**
8
 * @author Gabriel Felipe Soares <[email protected]>
9
 */
10
class ExploreResultCollection
11
{
12
    /**
13
     * @var ExploreResult[]
14
     */
15
    public $results;
16
17 18
    public function __construct(ExploreResult ...$terms)
18
    {
19 18
        $this->results = $terms;
20 18
    }
21
22
    /**
23
     * @return RelatedResult[]
24
     */
25 1
    public function getResults(): array
26
    {
27 1
        return $this->results;
28
    }
29
30
    /**
31
     * @return ExploreResult
32
     *
33
     * @throws GoogleTrendsException
34
     */
35 6
    public function getRelatedQueriesResult(): ExploreResult
36
    {
37 6
        return $this->getResultByMethod(
38 6
            'isRelatedQueriesSearch',
39 6
            'No explore result available for related queries!'
40
        );
41
    }
42
43
    /**
44
     * @return ExploreResult
45
     *
46
     * @throws GoogleTrendsException
47
     */
48 6
    public function getRelatedTopicsResult(): ExploreResult
49
    {
50 6
        return $this->getResultByMethod(
51 6
            'isRelatedTopicsSearch',
52 6
            'No explore result available for related topics!'
53
        );
54
    }
55
56
    /**
57
     * @return ExploreResult
58
     *
59
     * @throws GoogleTrendsException
60
     */
61 4
    public function getInterestOverTimeResult(): ExploreResult
62
    {
63 4
        return $this->getResultByMethod(
64 4
            'isInterestOverTimeSearch',
65 4
            'No explore result available for interest over time!'
66
        );
67
    }
68
69
    /**
70
     * @return ExploreResult
71
     *
72
     * @throws GoogleTrendsException
73
     */
74 4
    public function getInterestByRegionResult(): ExploreResult
75
    {
76 4
        return $this->getResultByMethod(
77 4
            'isInterestByRegionSearch',
78 4
            'No explore result available for interest by region!'
79
        );
80
    }
81
82
    /**
83
     * @param string $method
84
     * @param string $exceptionMessage
85
     *
86
     * @return ExploreResult
87
     *
88
     * @throws GoogleTrendsException
89
     */
90 18
    private function getResultByMethod(string $method, string $exceptionMessage): ExploreResult
91
    {
92 18
        foreach ($this->results as $result) {
93 14
            if ($result->$method()) {
94 14
                return $result;
95
            }
96
        }
97
98 4
        throw new GoogleTrendsException($exceptionMessage);
99
    }
100
}
101