Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
CRAP
91.67% covered (success)
91.67%
11 / 12
UserPrefSearchProfileOverride
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
5.01
91.67% covered (success)
91.67%
11 / 12
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 getOverriddenName
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
 priority
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 explain
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
<?php
namespace CirrusSearch\Profile;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserOptionsLookup;
/**
 * Overrider based on user preference.
 */
class UserPrefSearchProfileOverride implements SearchProfileOverride {
    /**
     * @var UserIdentity
     */
    private $user;
    /**
     * @var UserOptionsLookup
     */
    private $userOptionsLookup;
    /**
     * @var string name of the preference
     */
    private $preference;
    /**
     * @var int
     */
    private $priority;
    /**
     * @param UserIdentity $user
     * @param UserOptionsLookup $userOptionsLookup
     * @param string $preference
     * @param int $priority
     */
    public function __construct(
        UserIdentity $user,
        UserOptionsLookup $userOptionsLookup,
        $preference,
        $priority = SearchProfileOverride::USER_PREF_PRIO
    ) {
        $this->user = $user;
        $this->userOptionsLookup = $userOptionsLookup;
        $this->preference = $preference;
        $this->priority = $priority;
    }
    /**
     * Get the overridden name or null if it cannot be overridden.
     * @param string[] $contextParams
     * @return string|null
     */
    public function getOverriddenName( array $contextParams ) {
        // Only check user options if the user is logged to avoid loading
        // default user options.
        if ( $this->user->getId() === 0 ) {
            return null;
        }
        return $this->userOptionsLookup->getOption( $this->user, $this->preference );
    }
    /**
     * The priority of this override, lower wins
     * @return int
     */
    public function priority() {
        return $this->priority;
    }
    /**
     * @return array
     */
    public function explain(): array {
        return [
            'type' => 'userPreference',
            'priority' => $this->priority(),
            'userPreference' => $this->preference
        ];
    }
}