Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserPrefSearchProfileOverride
92.31% covered (success)
92.31%
12 / 13
75.00% covered (warning)
75.00%
3 / 4
5.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getOverriddenName
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 priority
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 explain
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Profile;
4
5use MediaWiki\User\Options\UserOptionsLookup;
6use MediaWiki\User\UserIdentity;
7
8/**
9 * Overrider based on user preference.
10 */
11class UserPrefSearchProfileOverride implements SearchProfileOverride {
12    /**
13     * @var UserIdentity
14     */
15    private $user;
16
17    /**
18     * @var UserOptionsLookup
19     */
20    private $userOptionsLookup;
21
22    /**
23     * @var string name of the preference
24     */
25    private $preference;
26
27    /**
28     * @var int
29     */
30    private $priority;
31
32    /**
33     * @param UserIdentity $user
34     * @param UserOptionsLookup $userOptionsLookup
35     * @param string $preference
36     * @param int $priority
37     */
38    public function __construct(
39        UserIdentity $user,
40        UserOptionsLookup $userOptionsLookup,
41        $preference,
42        $priority = SearchProfileOverride::USER_PREF_PRIO
43    ) {
44        $this->user = $user;
45        $this->userOptionsLookup = $userOptionsLookup;
46        $this->preference = $preference;
47        $this->priority = $priority;
48    }
49
50    /**
51     * Get the overridden name or null if it cannot be overridden.
52     * @param string[] $contextParams
53     * @return string|null
54     */
55    public function getOverriddenName( array $contextParams ) {
56        // Only check user options if the user is logged to avoid loading
57        // default user options.
58        if ( $this->user->getId() === 0 ) {
59            return null;
60        }
61        return $this->userOptionsLookup->getOption( $this->user, $this->preference );
62    }
63
64    /**
65     * The priority of this override, lower wins
66     * @return int
67     */
68    public function priority() {
69        return $this->priority;
70    }
71
72    /**
73     * @return array
74     */
75    public function explain(): array {
76        return [
77            'type' => 'userPreference',
78            'priority' => $this->priority(),
79            'userPreference' => $this->preference
80        ];
81    }
82}