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

TagQuery::findTopPosts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 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
}