Completed
Push — master ( dbcf6b...d53ab6 )
by Peter
44:13 queued 37:37
created

ArrayRequestLogger::logRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0185
1
<?php
2
3
namespace TreeHouse\IoBundle\Scrape\Crawler\Log;
4
5
/**
6
 * In-memory request logger. Use for very basic or testing purposes.
7
 *
8
 * CAUTION: it's not a good idea to use this when crawling large amounts of pages,
9
 * since memory usage keeps increasing, as well as the time consumed in this class.
10
 * Also this class does not work when working with multiple processes.
11
 *
12
 * You should configure one of the other loggers.
13
 */
14
class ArrayRequestLogger implements RequestLoggerInterface
15
{
16
    /**
17
     * @var array<integer, string[]>
18
     */
19
    protected $requests = [];
20
21
    /**
22
     * @inheritdoc
23
     */
24 6
    public function logRequest($url, \DateTime $date = null)
25
    {
26 6
        if (null === $date) {
27
            $date = new \DateTime();
28
        }
29
30 6
        $hashKey = $date->getTimestamp();
31 6
        $this->requests[$hashKey][] = $url;
32 6
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37 6
    public function getRequestsSince(\DateTime $date = null)
38
    {
39 6
        $start = $date ? $date->getTimestamp() : 0;
40
41 6
        $requests = [];
42 6
        foreach ($this->requests as $time => $reqs) {
43 6
            if ($time >= $start) {
44 4
                foreach ($reqs as $req) {
45 6
                    array_unshift($requests, [$time, $req]);
46
                }
47
            }
48
        }
49
50 6
        return $requests;
51
    }
52
}
53