Ranking::report()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 1
nop 0
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: thiago
5
 * Date: 01/04/18
6
 * Time: 16:33
7
 */
8
9
namespace MyWonderland\Domain\Model;
10
11
12
class Ranking
13
{
14
    /**
15
     * @var
16
     */
17
    private $events;
18
19
    /**
20
     * Ranking constructor.
21
     * @param $events
22
     */
23
    public function __construct($events)
24
    {
25
        $this->events = $events;
26
    }
27
28
    public function report()
29
    {
30
        $report = array_reduce($this->events, function($reportAccumulator, $event) {
31
            /**
32
             * @var $event Event
33
             */
34
            $key = $event->country . $event->city;
35
            if (isset($reportAccumulator[$key])) {
36
                $reportAccumulator[$key]['count'] += 1;
37
                return $reportAccumulator;
38
            }
39
            $reportAccumulator[$key] = [
40
                'country' => $event->country,
41
                'city' => $event->city,
42
                'count' => 1
43
            ];
44
            return $reportAccumulator;
45
        });
46
47
        usort($report, function($a, $b) {
48
            if ($a['count'] == $b['count']) {
49
                return 0;
50
            }
51
            return ($a['count'] > $b['count']) ? -1 : 1;
52
        });
53
54
        return $report;
55
    }
56
}