Passed
Push — master ( b14536...d7fcc2 )
by Paweł
03:55
created

LocationQuery   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 15
dl 0
loc 32
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findTopPosts() 0 10 2
A findOne() 0 10 1
1
<?php
2
/**
3
 * Created for IG Client.
4
 * User: jakim <[email protected]>
5
 * Date: 2019-02-26
6
 */
7
8
namespace Jakim\Query;
9
10
11
use Jakim\Base\Query;
12
use jakim\ig\Endpoint;
13
use Jakim\Mapper\ExploreLocations;
14
use Jakim\Model\Location;
15
use Jakim\Model\Post;
16
use Jakim\Model\Tag;
17
18
class LocationQuery extends Query
19
{
20
    protected $exploreLocationsMapper;
21
22
    public function __construct($httpClient, ExploreLocations $exploreLocationsMapper = null)
23
    {
24
        parent::__construct($httpClient);
25
        $this->exploreLocationsMapper = $exploreLocationsMapper ?? new ExploreLocations();
26
    }
27
28
    public function findOne(string $id): Location
29
    {
30
        $url = Endpoint::exploreLocations($id);
31
        $data = $this->fetchContentAsArray($url);
32
33
        $this->throwEmptyContentExceptionIfEmpty($data);
34
35
        $data = $this->exploreLocationsMapper->normalizeData(Location::class, $data);
36
37
        return $this->exploreLocationsMapper->populate(Location::class, $data);
38
    }
39
40
    public function findTopPosts(string $id)
41
    {
42
        $url = Endpoint::exploreLocations($id);
43
        $data = $this->fetchContentAsArray($url);
44
45
        $this->throwEmptyContentExceptionIfEmpty($data);
46
47
        $items = $this->exploreLocationsMapper->normalizeData(Post::class, $data);
48
        foreach ($items as $item) {
49
            yield $this->exploreLocationsMapper->populate(Post::class, $item);
50
        }
51
    }
52
}