CrawlObserverCollection   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 91
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addObserver() 0 4 1
A crawled() 0 10 2
A crawlFailed() 0 10 2
A current() 0 4 1
A offsetGet() 0 4 2
A offsetSet() 0 8 2
A offsetExists() 0 4 1
A offsetUnset() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
1
<?php
2
3
namespace Spatie\Crawler;
4
5
use ArrayAccess;
6
use GuzzleHttp\Exception\RequestException;
7
use Iterator;
8
use Psr\Http\Message\ResponseInterface;
9
10
class CrawlObserverCollection implements ArrayAccess, Iterator
11
{
12
    /** @var \Spatie\Crawler\CrawlObserver[] */
13
    protected $observers;
14
15
    /** @var int */
16
    protected $position;
17
18
    public function __construct(array $observers = [])
19
    {
20
        $this->observers = $observers;
21
22
        $this->position = 0;
23
    }
24
25
    public function addObserver(CrawlObserver $observer)
26
    {
27
        $this->observers[] = $observer;
28
    }
29
30
    public function crawled(CrawlUrl $crawlUrl, ResponseInterface $response)
31
    {
32
        foreach ($this->observers as $crawlObserver) {
33
            $crawlObserver->crawled(
34
                $crawlUrl->url,
35
                $response,
36
                $crawlUrl->foundOnUrl
37
            );
38
        }
39
    }
40
41
    public function crawlFailed(CrawlUrl $crawlUrl, RequestException $exception)
42
    {
43
        foreach ($this->observers as $crawlObserver) {
44
            $crawlObserver->crawlFailed(
45
                $crawlUrl->url,
46
                $exception,
47
                $crawlUrl->foundOnUrl
48
            );
49
        }
50
    }
51
52
    public function current()
53
    {
54
        return $this->observers[$this->position];
55
    }
56
57
    public function offsetGet($offset)
58
    {
59
        return isset($this->observers[$offset]) ? $this->observers[$offset] : null;
60
    }
61
62
    public function offsetSet($offset, $value)
63
    {
64
        if (is_null($offset)) {
65
            $this->observers[] = $value;
66
        } else {
67
            $this->observers[$offset] = $value;
68
        }
69
    }
70
71
    public function offsetExists($offset)
72
    {
73
        return isset($this->observers[$offset]);
74
    }
75
76
    public function offsetUnset($offset)
77
    {
78
        unset($this->observers[$offset]);
79
    }
80
81
    public function next()
82
    {
83
        $this->position++;
84
    }
85
86
    public function key()
87
    {
88
        return $this->position;
89
    }
90
91
    public function valid()
92
    {
93
        return isset($this->observers[$this->position]);
94
    }
95
96
    public function rewind()
97
    {
98
        $this->position = 0;
99
    }
100
}
101