CurlRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 41
ccs 26
cts 26
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 9 1
A fetchMultiple() 0 27 5
1
<?php
2
namespace Osln\Weather;
3
4
use Anax\Commons\ContainerInjectableInterface;
5
use Anax\Commons\ContainerInjectableTrait;
6
7
class CurlRequest implements ContainerInjectableInterface
8
{
9
    use ContainerInjectableTrait;
10 4
    public function fetch($url)
11
    {
12 4
        $ch = curl_init($url);
13 4
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

13
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
14
15 4
        $json = curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

15
        $json = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
16 4
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

16
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
17 4
        $decoded = json_decode($json, true);
18 4
        return $decoded;
19
    }
20
21 2
    public function fetchMultiple($urls)
22
    {
23 2
        $mh = curl_multi_init();
24 2
        $chAll = [];
25 2
        foreach ($urls as $url) {
26 2
            $ch = curl_init($url);
27 2
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

27
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_RETURNTRANSFER, true);
Loading history...
28 2
            curl_multi_add_handle($mh, $ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_multi_add_handle() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

28
            curl_multi_add_handle($mh, /** @scrutinizer ignore-type */ $ch);
Loading history...
29 2
            $chAll[] = $ch;
30
        }
31
32 2
        $running = null;
33
        do {
34 2
            curl_multi_exec($mh, $running);
35 2
        } while ($running);
36
37 2
        foreach ($chAll as $ch) {
38 2
            curl_multi_remove_handle($mh, $ch);
39
        }
40 2
        curl_multi_close($mh);
41
42 2
        $response = [];
43 2
        foreach ($chAll as $ch) {
44 2
            $data = curl_multi_getcontent($ch);
45 2
            $response[] = json_decode($data, true);
46
        }
47 2
        return $response;
48
    }
49
}
50