Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
LegacyStructuredMentorWriter
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 doSaveMentorData
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 isBlocked
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace GrowthExperiments\Mentorship\Provider;
4
5use GrowthExperiments\Config\WikiPageConfigLoader;
6use GrowthExperiments\Config\WikiPageConfigWriterFactory;
7use MediaWiki\Title\Title;
8use MediaWiki\User\UserFactory;
9use MediaWiki\User\UserIdentity;
10use MediaWiki\User\UserIdentityLookup;
11use StatusValue;
12use Wikimedia\Rdbms\IDBAccessObject;
13
14class LegacyStructuredMentorWriter extends AbstractStructuredMentorWriter {
15    use LegacyGetMentorDataTrait;
16
17    private WikiPageConfigWriterFactory $configWriterFactory;
18    private Title $mentorList;
19
20    /**
21     * @param MentorProvider $mentorProvider
22     * @param UserIdentityLookup $userIdentityLookup
23     * @param UserFactory $userFactory
24     * @param WikiPageConfigLoader $configLoader
25     * @param WikiPageConfigWriterFactory $configWriterFactory
26     * @param Title $mentorList
27     */
28    public function __construct(
29        MentorProvider $mentorProvider,
30        UserIdentityLookup $userIdentityLookup,
31        UserFactory $userFactory,
32        WikiPageConfigLoader $configLoader,
33        WikiPageConfigWriterFactory $configWriterFactory,
34        Title $mentorList
35    ) {
36        parent::__construct( $mentorProvider, $userIdentityLookup, $userFactory );
37
38        $this->configLoader = $configLoader;
39        $this->configWriterFactory = $configWriterFactory;
40        $this->mentorList = $mentorList;
41    }
42
43    /**
44     * Wrapper around WikiPageConfigWriter to save all mentor data
45     *
46     * @param array $mentorData
47     * @param string $summary
48     * @param UserIdentity $performer
49     * @param bool $bypassWarnings Should warnings raised by the validator stop the operation?
50     * @return StatusValue
51     */
52    protected function doSaveMentorData(
53        array $mentorData,
54        string $summary,
55        UserIdentity $performer,
56        bool $bypassWarnings
57    ): StatusValue {
58        $configWriter = $this->configWriterFactory
59            ->newWikiPageConfigWriter( $this->mentorList, $performer );
60        $configWriter->setVariable( self::CONFIG_KEY, $mentorData );
61        return $configWriter->save( $summary, false, self::CHANGE_TAG, $bypassWarnings );
62    }
63
64    /**
65     * @inheritDoc
66     */
67    public function isBlocked(
68        UserIdentity $performer,
69        int $freshness = IDBAccessObject::READ_NORMAL
70    ): bool {
71        $block = $this->userFactory->newFromUserIdentity( $performer )->getBlock( $freshness );
72        return $block && $block->appliesToTitle( $this->mentorList );
73    }
74}