Completed
Push — issue/127 ( 4cd54c...7fe4f9 )
by Alex
05:00
created

Reader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 68
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A process() 0 7 1
A handle() 0 12 2
A newFeed() 0 6 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Async;
12
13
use \GuzzleHttp\Client as GuzzleClient;
14
use \GuzzleHttp\Promise\EachPromise;
15
use \FeedIo\Reader as MainReader;
16
use \FeedIo\Reader\Result;
17
use \FeedIo\FeedInterface;
18
19
class Reader
20
{
21
    /**
22
     * @var \FeedIo\Reader
23
     */
24
    protected $reader;
25
26
    /**
27
     * @var CallbackInterface
28
     */
29
    protected $callback;
30
31
    /**
32
     * @var string
33
     */
34
    protected $feedClass;
35
36
37
    /**
38
     * Reader constructor.
39
     * @param MainReader $reader
40
     * @param CallbackInterface $callback
41
     * @param string $feedClass
42
     */
43 2
    public function __construct(MainReader $reader, CallbackInterface $callback, string $feedClass)
44
    {
45 2
        $this->reader = $reader;
46 2
        $this->callback = $callback;
47 2
        $this->feedClass = $feedClass;
48 2
    }
49
50
    /**
51
     * @param iterable $requests
52
     */
53 1
    public function process(iterable $requests) : void
54
    {
55 1
        $client = new Client(new GuzzleClient(), $this);
56 1
        $promises = $client->getPromises($requests);
57
58 1
        (new EachPromise($promises))->promise()->wait();
59 1
    }
60
61
    /**
62
     * @param Request $request
63
     */
64 2
    public function handle(Request $request) : void
65
    {
66
        try {
67 2
            $feed = $this->newFeed();
68 2
            $document = $this->reader->handleResponse($request->getResponse(), $feed);
69 1
            $result = new Result($document, $feed, $request->getModifiedSince(), $request->getResponse(), $request->getUrl());
70 1
            $this->callback->process($result);
71 1
        } catch (\Exception $e) {
72
            error_log("an error occured during {$request->getUrl()} processing : {$e->getMessage()}");
73
            error_log($e->getTraceAsString());
74
        }
75 1
    }
76
77
    /**
78
     * @return FeedInterface
79
     */
80 2
    public function newFeed() : FeedInterface
81
    {
82 2
        $reflection = new \ReflectionClass($this->feedClass);
83
84 2
        return $reflection->newInstanceArgs();
85
    }
86
}
87