Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
MentorshipSummaryCreator
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
6
100.00% covered (success)
100.00%
1 / 1
 createSummary
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 createAddSummary
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 createChangeSummary
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 createRemoveSummary
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace GrowthExperiments\Mentorship;
4
5use GrowthExperiments\Mentorship\Hooks\MentorHooks;
6use MediaWiki\User\UserIdentity;
7
8/**
9 * Create autocomment-formatted summary for StructuredMentorWriter
10 *
11 * @see MentorHooks::onFormatAutocomments()
12 * @see https://www.mediawiki.org/wiki/Autocomment
13 */
14class MentorshipSummaryCreator {
15
16    private static function createSummary(
17        string $messagePrefix,
18        UserIdentity $performer,
19        UserIdentity $mentor,
20        string $reason
21    ): string {
22        $messageKey = $messagePrefix;
23        if ( !$performer->equals( $mentor ) ) {
24            $messageKey .= '-admin';
25        } else {
26            $messageKey .= '-self';
27        }
28        if ( $reason === '' ) {
29            $messageKey .= '-no-reason';
30        } else {
31            $messageKey .= '-with-reason';
32        }
33
34        return sprintf(
35            '/* %s:%s|%s */',
36            $messageKey,
37            $mentor->getName(),
38            $reason
39        );
40    }
41
42    /**
43     * @param UserIdentity $performer
44     * @param UserIdentity $mentor
45     * @param string $reason
46     * @return string
47     */
48    public static function createAddSummary(
49        UserIdentity $performer,
50        UserIdentity $mentor,
51        string $reason = ''
52    ): string {
53        return self::createSummary(
54            'growthexperiments-manage-mentors-summary-add',
55            $performer,
56            $mentor,
57            $reason
58        );
59    }
60
61    /**
62     * @param UserIdentity $performer
63     * @param UserIdentity $mentor
64     * @param string $reason
65     * @return string
66     */
67    public static function createChangeSummary(
68        UserIdentity $performer,
69        UserIdentity $mentor,
70        string $reason = ''
71    ): string {
72        return self::createSummary(
73            'growthexperiments-manage-mentors-summary-change',
74            $performer,
75            $mentor,
76            $reason
77        );
78    }
79
80    /**
81     * @param UserIdentity $performer
82     * @param UserIdentity $mentor
83     * @param string $reason
84     * @return string
85     */
86    public static function createRemoveSummary(
87        UserIdentity $performer,
88        UserIdentity $mentor,
89        string $reason = ''
90    ): string {
91        return self::createSummary(
92            'growthexperiments-manage-mentors-summary-remove',
93            $performer,
94            $mentor,
95            $reason
96        );
97    }
98}