Completed
Push — master ( a36d59...17b30b )
by Shcherbak
18:25 queued 17:02
created

RecursivePagination   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 107
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addToQueue() 0 13 5
A getNextPage() 0 26 5
A setExpression() 0 18 6
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 27
    public function __construct(Parser $parser, $expression) {
36 27
      $this->parser = $parser;
37 27
      $this->setExpression($expression);
38 18
    }
39
40
41
    /**
42
     * @param array|string $links
43
     * @param bool $state
44
     * @throws \InvalidArgumentException
45
     * @return $this
46
     */
47 18
    public function addToQueue($links, $state = false) {
48 18
      if (!is_string($links) and !is_array($links)) {
49 3
        throw new \InvalidArgumentException('Links should be an array or a string');
50
      }
51 15
      foreach ((array) $links as $url) {
52 15
        if (!is_string($url)) {
53 3
          throw new \InvalidArgumentException('url should be a string');
54
        }
55 15
        $this->queue[$url] = $state;
56
      }
57
58 12
      return $this;
59
    }
60
61
62
    /**
63
     * @return ElementFinder|null
64
     * @throws \Exception
65
     * @throws \InvalidArgumentException
66
     */
67 12
    public function getNextPage() {
68
69 12
      $link = array_search(false, $this->queue, true);
70 12
      if ($link === false) {
71 12
        return null;
72
      }
73
74 12
      $this->queue[$link] = true;
75 12
      $page = $this->parser->get($link);
76
77 12
      if ($page === null) {
78
        return null;
79
      }
80
81 12
      foreach ($this->elementSelector as $expression => $state) {
82 12
        $queueLinks = $page->value($expression)->all();
83 12
        $countQueueLinks = count($queueLinks);
84 12
        if ($countQueueLinks > 0) {
85 12
          $queueLinks = array_combine($queueLinks, array_fill(0, $countQueueLinks, false));
86
            /** @noinspection SlowArrayOperationsInLoopInspection */
87 12
            $this->queue = array_merge($queueLinks, $this->queue);
88
        }
89
      }
90
91 12
      return $page;
92
    }
93
94
95
    /**
96
     * @param string|array $expression
97
     * @throws \InvalidArgumentException
98
     */
99 27
    private function setExpression($expression) {
100
101 27
      if (!is_string($expression) and !is_array($expression)) {
102 3
        throw new \InvalidArgumentException('Invalid expression, should be array or string');
103
      }
104
105 24
      $expression = array_filter((array) $expression);
106
107 24
      if (count($expression) === 0) {
108 3
        throw new \InvalidArgumentException('Expression might be not empty');
109
      }
110 21
      foreach ($expression as $path) {
111 21
        if (!is_string($path)) {
112 3
          throw new \InvalidArgumentException('Invalid expression, should be a string');
113
        }
114 21
        $this->elementSelector[$path] = true;
115
      }
116 18
    }
117
118
  }