Completed
Push — master ( ff82ae...111118 )
by Paweł
02:07
created

TagQuery   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 28
rs 10
c 0
b 0
f 0

3 Methods

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