Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EchoIcon
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 2
210
0.00% covered (danger)
0.00%
0 / 1
 getUrl
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
90
 getRasterizedUrl
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace MediaWiki\Extension\Notifications\Formatters;
4
5use InvalidArgumentException;
6use UnexpectedValueException;
7
8class EchoIcon {
9
10    /**
11     * @param string $icon Name of icon as registered in BeforeCreateEchoEvent hook
12     * @param string $dir either 'ltr' or 'rtl'
13     * @return string
14     */
15    public static function getUrl( $icon, $dir ) {
16        global $wgEchoNotificationIcons, $wgExtensionAssetsPath;
17        if ( !isset( $wgEchoNotificationIcons[$icon] ) ) {
18            throw new InvalidArgumentException( "The $icon icon is not registered" );
19        }
20
21        $iconInfo = $wgEchoNotificationIcons[$icon];
22        $needsPrefixing = true;
23
24        // Now we need to check it has a valid url/path
25        if ( isset( $iconInfo['url'] ) && $iconInfo['url'] ) {
26            $iconUrl = $iconInfo['url'];
27            $needsPrefixing = false;
28        } elseif ( isset( $iconInfo['path'] ) && $iconInfo['path'] ) {
29            $iconUrl = $iconInfo['path'];
30        } else {
31            // Fallback to hardcoded 'placeholder'. This is used if someone
32            // doesn't configure the 'site' icon for example.
33            $icon = 'placeholder';
34            $iconUrl = $wgEchoNotificationIcons['placeholder']['path'];
35        }
36
37        // Might be an array with different icons for ltr/rtl
38        if ( is_array( $iconUrl ) ) {
39            if ( !isset( $iconUrl[$dir] ) ) {
40                throw new UnexpectedValueException( "Icon type $icon doesn't have an icon for $dir directionality" );
41            }
42
43            $iconUrl = $iconUrl[$dir];
44        }
45
46        // And if it was a 'path', stick the assets path in front
47        if ( $needsPrefixing ) {
48            $iconUrl = "$wgExtensionAssetsPath/$iconUrl";
49        }
50
51        return $iconUrl;
52    }
53
54    /**
55     * Get a link to a rasterized version of the icon
56     *
57     * @param string $icon Icon name
58     * @param string $lang
59     * @return string URL to the rasterized version of the icon
60     */
61    public static function getRasterizedUrl( $icon, $lang ) {
62        global $wgEchoNotificationIcons;
63        if ( !isset( $wgEchoNotificationIcons[$icon] ) ) {
64            throw new InvalidArgumentException( "The $icon icon is not registered" );
65        }
66
67        $url = $wgEchoNotificationIcons[ $icon ][ 'url' ] ?? null;
68
69        // If the defined URL is explicitly false, use placeholder
70        if ( $url === false ) {
71            $icon = 'placeholder';
72        }
73
74        // If the URL is null or false call the resource loader
75        // rasterizing module
76        if ( $url === false || $url === null ) {
77            $iconUrl = wfScript( 'load' ) . '?' . wfArrayToCgi( [
78                'modules' => 'ext.echo.emailicons',
79                'image' => $icon,
80                'lang' => $lang,
81                'format' => 'rasterized'
82            ] );
83        } else {
84            // For icons that are defined by URL
85            $iconUrl = $wgEchoNotificationIcons[ $icon ][ 'url' ];
86        }
87
88        return $iconUrl;
89    }
90
91}