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

ArrayRequestLogger   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 39
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A logRequest() 0 9 2
A getRequestsSince() 0 15 5
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