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

Reader::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 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