Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
35 / 40
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManageMentorsAddMentor
87.50% covered (warning)
87.50%
35 / 40
75.00% covered (warning)
75.00%
3 / 4
6.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getFormFields
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 onSubmit
66.67% covered (warning)
66.67%
10 / 15
0.00% covered (danger)
0.00%
0 / 1
3.33
 onSuccess
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace GrowthExperiments\Specials\Forms;
4
5use GrowthExperiments\MentorDashboard\MentorTools\IMentorWeights;
6use GrowthExperiments\Mentorship\Provider\IMentorWriter;
7use GrowthExperiments\Mentorship\Provider\MentorProvider;
8use MediaWiki\Context\IContextSource;
9use MediaWiki\Status\Status;
10use MediaWiki\User\UserIdentity;
11use MediaWiki\User\UserIdentityLookup;
12
13class ManageMentorsAddMentor extends ManageMentorsAbstractForm {
14
15    private UserIdentityLookup $userIdentityLookup;
16    private MentorProvider $mentorProvider;
17    private IMentorWriter $mentorWriter;
18    private ?UserIdentity $mentorUser = null;
19
20    public function __construct(
21        UserIdentityLookup $userIdentityLookup,
22        MentorProvider $mentorProvider,
23        IMentorWriter $mentorWriter,
24        IContextSource $context,
25        string $messagePrefix = ''
26    ) {
27        parent::__construct( $context, $messagePrefix );
28
29        $this->userIdentityLookup = $userIdentityLookup;
30        $this->mentorProvider = $mentorProvider;
31        $this->mentorWriter = $mentorWriter;
32
33        $this->setPreHtml( $this->msg(
34            'growthexperiments-manage-mentors-add-mentor-pretext'
35        )->parse() );
36    }
37
38    /**
39     * @inheritDoc
40     */
41    protected function getFormFields(): array {
42        return [
43            'username' => [
44                'type' => 'user',
45                'label-message' => 'growthexperiments-manage-mentors-add-mentor-username',
46            ],
47            'reason' => [
48                'type' => 'text',
49                'label-message' => 'growthexperiments-manage-mentors-add-mentor-reason',
50            ],
51        ];
52    }
53
54    /**
55     * @inheritDoc
56     */
57    public function onSubmit( array $data ) {
58        if ( !self::canManageMentors( $this->getAuthority() ) ) {
59            return false;
60        }
61
62        $this->mentorUser = $this->userIdentityLookup->getUserIdentityByName( $data['username'] );
63        if ( !$this->mentorUser ) {
64            return Status::newFatal(
65                'nosuchuser',
66                $data['username']
67            );
68        }
69
70        $mentor = $this->mentorProvider->newMentorFromUserIdentity( $this->mentorUser );
71        $mentor->setWeight( IMentorWeights::WEIGHT_NONE );
72
73        return Status::wrap( $this->mentorWriter->addMentor(
74            $mentor,
75            $this->getUser(),
76            $data['reason']
77        ) );
78    }
79
80    /**
81     * @inheritDoc
82     */
83    protected function onSuccess(): void {
84        $out = $this->getOutput();
85        $out->addWikiMsg(
86            'growthexperiments-manage-mentors-add-mentor-success',
87            $this->mentorUser->getName()
88        );
89        $out->addWikiMsg(
90            'growthexperiments-manage-mentors-return-back'
91        );
92    }
93}