Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
WatchlistUpdater
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 optionallyWatchEditedSchema
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 optionallyWatchNewSchema
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare( strict_types = 1 );
4
5namespace EntitySchema\DataAccess;
6
7use EntitySchema\Domain\Model\EntitySchemaId;
8use MediaWiki\Title\Title;
9use MediaWiki\User\Options\UserOptionsLookup;
10use MediaWiki\User\User;
11use MediaWiki\Watchlist\WatchlistManager;
12
13/**
14 * @license GPL-2.0-or-later
15 */
16class WatchlistUpdater {
17
18    private UserOptionsLookup $userOptionsLookup;
19    private WatchlistManager $watchlistManager;
20
21    public function __construct(
22        UserOptionsLookup $userOptionsLookup,
23        WatchlistManager $watchlistManager
24    ) {
25        $this->userOptionsLookup = $userOptionsLookup;
26        $this->watchlistManager = $watchlistManager;
27    }
28
29    public function optionallyWatchEditedSchema( User $user, EntitySchemaId $entitySchemaId ): void {
30        if ( !$user->isNamed() ) {
31            return;
32        }
33        if ( $this->userOptionsLookup->getOption( $user, 'watchdefault' ) ) {
34            $this->watchlistManager->setWatch(
35                true,
36                $user,
37                Title::makeTitle( NS_ENTITYSCHEMA_JSON, $entitySchemaId->getId() )
38            );
39        }
40    }
41
42    public function optionallyWatchNewSchema( User $user, EntitySchemaId $entitySchemaId ): void {
43        if ( !$user->isNamed() ) {
44            return;
45        }
46        if ( $this->userOptionsLookup->getOption( $user, 'watchcreations' )
47            || $this->userOptionsLookup->getOption( $user, 'watchdefault' ) ) {
48            $this->watchlistManager->setWatch(
49                true,
50                $user,
51                Title::makeTitle( NS_ENTITYSCHEMA_JSON, $entitySchemaId->getId() )
52            );
53        }
54    }
55
56}