Completed
Push — master ( b1f56f...51e831 )
by Shcherbak
09:02 queued 07:55
created

RecursivePagination::getNextPage()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 25
ccs 0
cts 20
cp 0
rs 8.439
cc 5
eloc 15
nc 5
nop 0
crap 30
1
<?php
2
3
  namespace Xparse\RecursivePagination;
4
5
6
  use Xparse\ElementFinder\ElementFinder;
7
  use Xparse\Parser\Parser;
8
9
  /**
10
   *
11
   */
12
  class RecursivePagination {
13
14
    /**
15
     * @var array
16
     */
17
    protected $queue = [];
18
19
    /**
20
     * @var Parser
21
     */
22
    protected $parser;
23
24
    /**
25
     * @var array
26
     */
27
    protected $elementSelector = [];
28
29
30
    /**
31
     * @param Parser $parser
32
     * @param string|array $expression
33
     * @throws \Exception
34
     */
35
    public function __construct(Parser $parser, $expression) {
36
      $this->parser = $parser;
37
38
      $this->setExpression($expression);
39
    }
40
41
42
    /**
43
     * @param array|string $links
44
     * @param bool $state
45
     * @throws \InvalidArgumentException
46
     * @return $this
47
     */
48
    public function addToQueue($links, $state = false) {
49
      if (!is_string($links) and !is_array($links)) {
50
        throw new \InvalidArgumentException('Links should be an array or a string');
51
      }
52
      foreach ((array) $links as $url) {
53
        if (!is_string($url)) {
54
          throw new \InvalidArgumentException('url should be a string');
55
        }
56
        $this->queue[$url] = $state;
57
      }
58
59
      return $this;
60
    }
61
62
63
    /**
64
     * @return ElementFinder|null
65
     * @throws \Exception
66
     * @throws \InvalidArgumentException
67
     */
68
    public function getNextPage() {
69
70
      $link = array_search(false, $this->queue, true);
71
      if ($link === false) {
72
        return null;
73
      }
74
75
      $this->queue[$link] = true;
76
      $page = $this->parser->get($link);
77
78
      if ($page === null) {
79
        return null;
80
      }
81
82
      foreach ($this->elementSelector as $expression => $state) {
83
        $queueLinks = $page->value($expression)->all();
84
        $countQueueLinks = count($queueLinks);
85
        if ($countQueueLinks > 0) {
86
          $queueLinks = array_combine($queueLinks, array_fill(0, $countQueueLinks, false));
87
          $this->queue = array_merge($queueLinks, $this->queue);
88
        }
89
      }
90
91
      return $page;
92
    }
93
94
95
    /**
96
     * @param string|array $expression
97
     * @throws \InvalidArgumentException
98
     */
99
    private function setExpression($expression) {
100
101
      if (!is_string($expression) and !is_array($expression)) {
102
        throw new \InvalidArgumentException('Invalid expression, should be array or string');
103
      }
104
105
      $expression = array_filter((array) $expression);
106
107
      if (count($expression) === 0) {
108
        throw new \InvalidArgumentException('Expression might be not empty');
109
      }
110
111
      foreach ($expression as $path) {
112
        if (!is_string($path)) {
113
          throw new \InvalidArgumentException('Invalid expression, should be a string');
114
        }
115
        $this->elementSelector[$path] = true;
116
      }
117
    }
118
119
  }