FetchSubscriptionHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace PersonalGalaxy\RSS\Handler;
5
6
use PersonalGalaxy\RSS\{
7
    Command\FetchSubscription,
8
    Repository\SubscriptionRepository,
9
    Repository\ArticleRepository,
10
    Entity\Article,
11
};
12
use Innmind\Crawler\Crawler;
13
use Innmind\Http\{
14
    Message\Request\Request,
15
    Message\Method\Method,
16
    ProtocolVersion\ProtocolVersion,
17
};
18
19
final class FetchSubscriptionHandler
20
{
21
    private $subscriptions;
22
    private $articles;
23
    private $crawler;
24
25 1
    public function __construct(
26
        SubscriptionRepository $subscriptions,
27
        ArticleRepository $articles,
28
        Crawler $crawler
29
    ) {
30 1
        $this->subscriptions = $subscriptions;
31 1
        $this->articles = $articles;
32 1
        $this->crawler = $crawler;
33 1
    }
34
35 1
    public function __invoke(FetchSubscription $wished): void
36
    {
37 1
        $subscription = $this->subscriptions->get($wished->identity());
38 1
        $resource = $this->crawler->execute(new Request(
39 1
            $subscription->location(),
40 1
            new Method(Method::GET),
41 1
            new ProtocolVersion(2, 0)
42
        ));
43
        $resource
44 1
            ->attributes()
45 1
            ->get('articles')
46 1
            ->content()
47 1
            ->filter(function(Article $article): bool {
48 1
                return !$this->articles->has($article->link());
49 1
            })
50 1
            ->foreach(function(Article $article) use ($subscription): void {
51 1
                $article->bindTo($subscription->identity());
52 1
                $this->articles->add($article);
53 1
            });
54 1
    }
55
}
56