CWMultiResultTrait::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 5
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Rawaby88\OpenWeatherMap\Traits;
4
5
use Rawaby88\OpenWeatherMap\Services\Support\CurrentWeather;
6
7
/**
8
 * Trait CWMultiResultTrait.
9
 */
10
trait CWMultiResultTrait
11
{
12
    /**
13
     * @var array Of CurrentWeather objects.
14
     */
15
    public $list;
16
17
    /**
18
     * @var array Hold query parameters for api call.
19
     */
20
    protected $params;
21
22
    /**
23
     * @var string Part of the api link.
24
     */
25
    protected $apiCall;
26
27
    /**
28
     * Return instance of CWMultiResultInterface.
29
     */
30
    public function get()
31
    {
32
        $data = $this->queryOrFail($this->apiCall, $this->params);
0 ignored issues
show
Bug introduced by
It seems like queryOrFail() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        /** @scrutinizer ignore-call */ 
33
        $data = $this->queryOrFail($this->apiCall, $this->params);
Loading history...
33
        $this->list = [];
34
35
        collect($data->list)->map(function ($result, $key) {
36
            $this->list[$key] = new CurrentWeather($result);
37
        });
38
39
        return $this;
40
    }
41
42
    /**
43
     * Return the $index item on the list as CurrentWeather object.
44
     * @param $index
45
     * @return CurrentWeather
46
     */
47
    public function index($index): CurrentWeather
48
    {
49
        return $this->list[$index];
50
    }
51
52
    /**
53
     * Return the first item on the list as CurrentWeather object.
54
     * @return CurrentWeather
55
     */
56
    public function first(): CurrentWeather
57
    {
58
        return reset($this->list);
59
    }
60
61
    /**
62
     * Return count number of the result.
63
     * @return int
64
     */
65
    public function count(): int
66
    {
67
        return count($this->list);
68
    }
69
}
70