Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Bundler
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 sort
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 bundle
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace MediaWiki\Extension\Notifications;
4
5class Bundler {
6
7    private function sort( &$array ) {
8        usort( $array, static function ( Bundleable $a, Bundleable $b ) {
9            return strcmp( $b->getSortingKey(), $a->getSortingKey() );
10        } );
11    }
12
13    /**
14     * Bundle bundleable elements that can be bundled by their bundling keys
15     *
16     * @param Bundleable[] $bundleables
17     * @return Bundleable[] Grouped notifications sorted by timestamp DESC
18     */
19    public function bundle( array $bundleables ) {
20        $groups = [];
21        $bundled = [];
22
23        /** @var Bundleable $element */
24        foreach ( $bundleables as $element ) {
25            if ( $element->canBeBundled() && $element->getBundlingKey() ) {
26                $groups[ $element->getBundlingKey() ][] = $element;
27            } else {
28                $bundled[] = $element;
29            }
30        }
31
32        foreach ( $groups as $bundlingKey => $group ) {
33            $this->sort( $group );
34            /** @var Bundleable $base */
35            $base = array_shift( $group );
36            $base->setBundledElements( $group );
37            $bundled[] = $base;
38        }
39
40        $this->sort( $bundled );
41        return $bundled;
42    }
43
44}