Passed
Push — analysis-qBJygA ( 146b42 )
by Nuno
09:11 queued 41s
created

Queue::cloneQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Splitters\HtmlSplitterComponent;
15
16
final class Queue
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $queue = [];
22
    protected $cloneQueue = [];
23
24
    /**
25
     * The list of html tags.
26
     *
27
     * @var string[]
28
     */
29
    protected $tags = [
30
        'h1',
31
        'h2',
32
        'h3',
33
        'h4',
34
        'h5',
35
        'h6',
36
        'p',
37
    ];
38
39
    /**
40
     * String for key check purpose.
41
     */
42
    private const IMPORTANCE = 'importance';
43
44
    /**
45
     * String for exception purpose.
46
     */
47
    private const PARAGRAPH = 'p';
48
49
    /**
50
     * @return array
51
     */
52
    public function getCloneQueue(): array
53
    {
54
        return $this->cloneQueue;
55
    }
56
57
    /**
58
     * Add object to queue.
59
     *
60
     * @param ObjectQueue $object
61
     */
62 5
    public function addObjectQueue(ObjectQueue $object): void
63
    {
64 5
        if ($this->lengthQueue() === 0) {
65 5
            $this->queue[] = $object;
66 5
            $this->cloneQueue();
67 5
        } elseif ($this->findWeight($object) > $this->findWeight(end($this->queue))) {
68 3
            $this->queue[] = $object;
69 3
            $this->cloneQueue();
70
        } else {
71 4
            array_pop($this->queue);
72 4
            $this->addObjectQueue($object);
73
        }
74 5
    }
75
76
    /**
77
     * Clean Records to have a correct format.
78
     *
79
     * @return array
80
     */
81 5
    public function sanitizeQueue(): array
82
    {
83 5
        $records = [];
84 5
        foreach ($this->cloneQueue as $queue) {
85 5
            foreach ($queue as $object) {
86 5
                if ($object instanceof ObjectQueue) {
87 5
                    $record[$object->getTag()] = $object->getTagContent();
88
                } else {
89 5
                    $record[self::IMPORTANCE] = $object;
90 5
                    $records[] = $record;
91 5
                    $record = [];
92
                }
93
            }
94
        }
95
96 5
        return $records;
97
    }
98
99
    /**
100
     * Importance need to be add after to avoid polluted queue.
101
     *
102
     * @return void
103
     */
104 5
    private function cloneQueue(): void
105
    {
106 5
        $this->cloneQueue[] = $this->queue;
107 5
        $this->cloneQueue[] = [self::IMPORTANCE => $this->importanceWeight(end($this->queue))];
108 5
    }
109
110
    /**
111
     * Importance formula.
112
     * Give integer from tags ranking.
113
     *
114
     * @param ObjectQueue $objectQueue
115
     *
116
     * @return int
117
     */
118 5
    private function importanceWeight(ObjectQueue $objectQueue): int
119
    {
120 5
        if ($objectQueue->getTag() === self::PARAGRAPH) {
121 3
            $object = prev($this->queue);
122 3
            if (empty(end($this->queue)) || $this->lengthQueue() === 1) {
123
                return 0;
124
            }
125
126 3
            return (count($this->tags) - 1) + $this->findWeight($object);
127
        }
128
129 5
        return $this->findWeight($objectQueue);
130
    }
131
132
    /**
133
     * Find weight of current nodes.
134
     *
135
     * @param ObjectQueue $object
136
     *
137
     * @return int
138
     */
139 5
    private function findWeight(ObjectQueue $object): int
140
    {
141 5
        return (int) array_search($object->getTag(), $this->tags, true);
142
    }
143
144
    /**
145
     * Give the length of the queue.
146
     *
147
     * @return int
148
     */
149 5
    private function lengthQueue(): int
150
    {
151 5
        return count($this->queue);
152
    }
153
}
154