CollectionCrawlQueue   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUrls() 0 4 1
A getPendingUrls() 0 4 1
A add() 0 13 2
A hasPendingUrls() 0 4 1
A getUrlById() 0 8 2
A hasAlreadyBeenProcessed() 0 4 2
A markAsProcessed() 0 7 1
A has() 0 12 3
A getFirstPendingUrl() 0 4 1
A contains() 0 10 3
1
<?php
2
3
namespace Spatie\Crawler\CrawlQueue;
4
5
use Spatie\Crawler\CrawlUrl;
6
use Spatie\Crawler\Exception\UrlNotFoundByIndex;
7
8
/**
9
 * @deprecated Use \Spatie\Crawler\CrawlQueue\ArrayQueue
10
 */
11
class CollectionCrawlQueue implements CrawlQueue
12
{
13
    /** @var \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection */
14
    protected $urls;
15
16
    /** @var \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection */
17
    protected $pendingUrls;
18
19
    public function __construct()
20
    {
21
        $this->urls = collect();
22
23
        $this->pendingUrls = collect();
24
    }
25
26
    public function getUrls()
27
    {
28
        return $this->urls;
29
    }
30
31
    public function getPendingUrls()
32
    {
33
        return $this->pendingUrls;
34
    }
35
36
    public function add(CrawlUrl $url): CrawlQueue
37
    {
38
        if ($this->has($url)) {
39
            return $this;
40
        }
41
42
        $this->urls->push($url);
43
44
        $url->setId($this->urls->keys()->last());
45
        $this->pendingUrls->push($url);
46
47
        return $this;
48
    }
49
50
    public function hasPendingUrls(): bool
51
    {
52
        return (bool) $this->pendingUrls->count();
53
    }
54
55
    /**
56
     * @param mixed $id
57
     *
58
     * @return \Spatie\Crawler\CrawlUrl|null
59
     */
60
    public function getUrlById($id): CrawlUrl
61
    {
62
        if (! isset($this->urls->values()[$id])) {
63
            throw new UrlNotFoundByIndex("#{$id} crawl url not found in collection");
64
        }
65
66
        return $this->urls->values()[$id];
67
    }
68
69
    public function hasAlreadyBeenProcessed(CrawlUrl $url): bool
70
    {
71
        return ! $this->contains($this->pendingUrls, $url) && $this->contains($this->urls, $url);
72
    }
73
74
    public function markAsProcessed(CrawlUrl $crawlUrl)
75
    {
76
        $this->pendingUrls = $this->pendingUrls
77
            ->reject(function (CrawlUrl $crawlUrlItem) use ($crawlUrl) {
78
                return (string) $crawlUrlItem->url === (string) $crawlUrl->url;
79
            });
80
    }
81
82
    /**
83
     * @param CrawlUrl|\Psr\Http\Message\UriInterface $crawlUrl
84
     *
85
     * @return bool
86
     */
87
    public function has($crawlUrl): bool
88
    {
89
        if (! $crawlUrl instanceof CrawlUrl) {
90
            $crawlUrl = CrawlUrl::create($crawlUrl);
91
        }
92
93
        if ($this->contains($this->urls, $crawlUrl)) {
94
            return true;
95
        }
96
97
        return false;
98
    }
99
100
    /** @return \Spatie\Crawler\CrawlUrl|null */
101
    public function getFirstPendingUrl()
102
    {
103
        return $this->pendingUrls->first();
104
    }
105
106
    /**
107
     * @param \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection $collection
108
     * @param \Spatie\Crawler\CrawlUrl                                             $searchCrawlUrl
109
     *
110
     * @return bool
111
     */
112
    protected function contains($collection, CrawlUrl $searchCrawlUrl): bool
113
    {
114
        foreach ($collection as $crawlUrl) {
115
            if ((string) $crawlUrl->url === (string) $searchCrawlUrl->url) {
116
                return true;
117
            }
118
        }
119
120
        return false;
121
    }
122
}
123