1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Scrape\EventListener; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8
|
|
|
use TreeHouse\Feeder\Event\FailedItemModificationEvent; |
9
|
|
|
use TreeHouse\Feeder\FeedEvents; |
10
|
|
|
use TreeHouse\IoBundle\Scrape\Event\FailedItemEvent; |
11
|
|
|
use TreeHouse\IoBundle\Scrape\Event\RateLimitEvent; |
12
|
|
|
use TreeHouse\IoBundle\Scrape\Event\ScrapeResponseEvent; |
13
|
|
|
use TreeHouse\IoBundle\Scrape\Event\ScrapeUrlEvent; |
14
|
|
|
use TreeHouse\IoBundle\Scrape\Event\SkippedItemEvent; |
15
|
|
|
use TreeHouse\IoBundle\Scrape\Event\SuccessItemEvent; |
16
|
|
|
use TreeHouse\IoBundle\Scrape\ScraperEvents; |
17
|
|
|
|
18
|
|
|
class ScrapeLoggingSubscriber implements EventSubscriberInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var LoggerInterface |
22
|
|
|
*/ |
23
|
|
|
protected $logger; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param LoggerInterface $logger |
27
|
|
|
*/ |
28
|
4 |
|
public function __construct(LoggerInterface $logger) |
29
|
|
|
{ |
30
|
4 |
|
$this->logger = $logger; |
31
|
4 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
4 |
|
public static function getSubscribedEvents() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
4 |
|
ScraperEvents::ITEM_SUCCESS => 'onItemSuccess', |
40
|
4 |
|
ScraperEvents::ITEM_FAILED => 'onItemFailed', |
41
|
4 |
|
ScraperEvents::ITEM_SKIPPED => 'onItemSkipped', |
42
|
4 |
|
ScraperEvents::SCRAPE_NEXT_URL => 'onScrapeNextUrl', |
43
|
4 |
|
ScraperEvents::RATE_LIMIT_REACHED => 'onRateLimitReached', |
44
|
4 |
|
ScraperEvents::SCRAPE_URL_NOT_OK => 'onScrapeUrlNotOk', |
45
|
4 |
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param SuccessItemEvent $event |
50
|
|
|
*/ |
51
|
|
|
public function onItemSuccess(SuccessItemEvent $event) |
52
|
|
|
{ |
53
|
|
|
$this->logger->info(sprintf('✎ updated: %s', (string) $event->getItem())); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param SkippedItemEvent $event |
58
|
|
|
*/ |
59
|
|
|
public function onItemSkipped(SkippedItemEvent $event) |
60
|
|
|
{ |
61
|
|
|
$this->logger->info(sprintf('# skipped: %s', (string) $event->getItem())); |
62
|
|
|
$this->logger->debug(sprintf(' reason: %s', $event->getReason())); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param FailedItemEvent $event |
67
|
|
|
*/ |
68
|
|
|
public function onItemFailed(FailedItemEvent $event) |
69
|
|
|
{ |
70
|
|
|
$this->logger->warning(sprintf('✘ failed: %s', (string) $event->getItem())); |
71
|
|
|
$this->logger->debug(sprintf(' reason: %s', $event->getReason())); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param ScrapeUrlEvent $event |
76
|
|
|
*/ |
77
|
|
|
public function onScrapeNextUrl(ScrapeUrlEvent $event) |
78
|
|
|
{ |
79
|
|
|
$this->logger->debug(sprintf('⇒ next url: %s', (string) $event->getUrl())); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param RateLimitEvent $event |
84
|
|
|
*/ |
85
|
|
|
public function onRateLimitReached(RateLimitEvent $event) |
86
|
|
|
{ |
87
|
|
|
$seconds = $event->getRetryDate()->getTimestamp() - time(); |
88
|
|
|
$this->logger->debug(sprintf('Rate limit reached, try again after %d seconds', $seconds)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ScrapeResponseEvent $event |
93
|
|
|
*/ |
94
|
|
|
public function onScrapeUrlNotOk(ScrapeResponseEvent $event) |
95
|
|
|
{ |
96
|
|
|
$response = $event->getResponse(); |
97
|
|
|
|
98
|
|
|
$code = $response->getStatusCode(); |
99
|
|
|
$text = (new Response($code))->getReasonPhrase(); |
100
|
|
|
|
101
|
|
|
$this->logger->debug(sprintf('Server replied with response %d (%s)', $code, $text)); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|