Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
14 / 35
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
PersonalizedPraiseHooks
40.00% covered (danger)
40.00%
14 / 35
0.00% covered (danger)
0.00%
0 / 4
21.82
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 onPageSaveComplete
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
5.05
 onGetPreferences
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 onUserGetDefaultOptions
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\MentorDashboard\PersonalizedPraise;
4
5use GrowthExperiments\Mentorship\MentorManager;
6use GrowthExperiments\UserImpact\UserImpactLookup;
7use MediaWiki\Config\Config;
8use MediaWiki\Deferred\DeferredUpdates;
9use MediaWiki\Preferences\Hook\GetPreferencesHook;
10use MediaWiki\Storage\Hook\PageSaveCompleteHook;
11use MediaWiki\User\Hook\UserGetDefaultOptionsHook;
12
13class PersonalizedPraiseHooks implements
14    PageSaveCompleteHook,
15    GetPreferencesHook,
16    UserGetDefaultOptionsHook
17{
18
19    private Config $config;
20    private MentorManager $mentorManager;
21    private UserImpactLookup $userImpactLookup;
22    private PraiseworthyConditionsLookup $praiseworthyConditionsLookup;
23    private PraiseworthyMenteeSuggester $praiseworthyMenteeSuggester;
24
25    /**
26     * @param Config $config
27     * @param MentorManager $mentorManager
28     * @param UserImpactLookup $userImpactLookup
29     * @param PraiseworthyConditionsLookup $praiseworthyConditionsLookup
30     * @param PraiseworthyMenteeSuggester $praiseworthyMenteeSuggester
31     */
32    public function __construct(
33        Config $config,
34        MentorManager $mentorManager,
35        UserImpactLookup $userImpactLookup,
36        PraiseworthyConditionsLookup $praiseworthyConditionsLookup,
37        PraiseworthyMenteeSuggester $praiseworthyMenteeSuggester
38    ) {
39        $this->config = $config;
40        $this->mentorManager = $mentorManager;
41        $this->userImpactLookup = $userImpactLookup;
42        $this->praiseworthyConditionsLookup = $praiseworthyConditionsLookup;
43        $this->praiseworthyMenteeSuggester = $praiseworthyMenteeSuggester;
44    }
45
46    /**
47     * @inheritDoc
48     */
49    public function onPageSaveComplete(
50        $wikiPage, $user, $summary, $flags, $revisionRecord, $editResult
51    ) {
52        if ( !$this->config->get( 'GEPersonalizedPraiseBackendEnabled' ) ) {
53            return;
54        }
55
56        DeferredUpdates::addCallableUpdate( function () use ( $user ) {
57            $mentor = $this->mentorManager->getMentorForUserIfExists( $user );
58            if ( !$mentor ) {
59                return;
60            }
61
62            $menteeImpact = $this->userImpactLookup->getUserImpact( $user );
63            if (
64                $menteeImpact &&
65                $this->praiseworthyConditionsLookup->isMenteePraiseworthyForMentor(
66                    $menteeImpact,
67                    $mentor->getUserIdentity()
68            ) ) {
69                $this->praiseworthyMenteeSuggester->markMenteeAsPraiseworthy(
70                    $menteeImpact, $mentor->getUserIdentity()
71                );
72            }
73        } );
74    }
75
76    /**
77     * @inheritDoc
78     */
79    public function onGetPreferences( $user, &$preferences ) {
80        $preferences[ PraiseworthyConditionsLookup::WAS_PRAISED_PREF ] = [
81            'type' => 'api'
82        ];
83        $preferences[ PraiseworthyConditionsLookup::SKIPPED_UNTIL_PREF ] = [
84            'type' => 'api'
85        ];
86        $preferences[ PersonalizedPraiseSettings::PREF_NAME ] = [
87            'type' => 'api'
88        ];
89    }
90
91    /**
92     * @inheritDoc
93     */
94    public function onUserGetDefaultOptions( &$defaultOptions ) {
95        $defaultOptions += [
96            PraiseworthyConditionsLookup::WAS_PRAISED_PREF => false,
97            PraiseworthyConditionsLookup::SKIPPED_UNTIL_PREF => null,
98            PersonalizedPraiseSettings::PREF_NAME => '{}',
99        ];
100    }
101}