Merger   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getMergedItems() 0 23 6
A setData() 0 3 1
1
<?php
2
3
namespace PhpEarth\Stats\Util;
4
5
/**
6
 * Merges sequential comments or replies.
7
 */
8
class Merger
9
{
10
    /**
11
     * Set comments or replies to merge.
12
     *
13
     * @param array $data
14
     */
15
    public function setData($data)
16
    {
17
        $this->data = $data;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
    }
19
20
    /**
21
     * Merges sequential comments or replies and return merged array for a given user.
22
     *
23
     * @param int $userId
24
     * @return array
25
     */
26
    public function getMergedItems($userId)
27
    {
28
        $items = array_values($this->data);
29
        $mergedItems = [];
30
        $i = 0;
31
        foreach ($items as $item) {
32
            if ($item->getUser()->getId() == $userId) {
33
                if (isset($items[$i-1]) && $items[$i-1]->getUser()->getId() == $userId) {
34
                    $item->setMessage($items[$i-1]->getMessage().$item->getMessage());
35
                    $item->setReactionsCount(max($item->getReactionsCount(), $items[$i-1]->getReactionsCount()));
36
                    unset($mergedItems[$i-1]);
37
                }
38
                $mergedItems[$i] = $item;
39
            }
40
            $i++;
41
        }
42
43
        $items = [];
44
        foreach ($mergedItems as $item) {
45
            $items[$item->getId()] = $item;
46
        }
47
48
        return $items;
49
    }
50
}
51