Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractTopicInsertListener
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 onAfterInsertExpectedChange
n/a
0 / 0
n/a
0 / 0
0
 onAfterInsert
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 getUsersToSubscribe
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Flow\Data\Listener;
4
5use Flow\Container;
6use Flow\Exception\InvalidDataException;
7use Flow\FlowActions;
8use Flow\Model\PostRevision;
9use Flow\Model\Workflow;
10use Flow\WatchedTopicItems;
11use MediaWiki\User\User;
12
13/**
14 * Auto-watch topics when the user performs one of the actions specified
15 * in the constructor.
16 */
17abstract class AbstractTopicInsertListener extends AbstractListener {
18    /**
19     * @param string $changeType
20     * @param Workflow $workflow
21     */
22    abstract protected function onAfterInsertExpectedChange( $changeType, Workflow $workflow );
23
24    /** @inheritDoc */
25    public function onAfterInsert( $object, array $row, array $metadata ) {
26        if ( !$object instanceof PostRevision ) {
27            wfWarn( __METHOD__ . ': Object is no PostRevision instance' );
28            return;
29        }
30
31        if ( !isset( $metadata['workflow'] ) ) {
32            wfWarn( __METHOD__ . ': Missing required metadata: workflow' );
33            return;
34        }
35        $workflow = $metadata['workflow'];
36        if ( !$workflow instanceof Workflow ) {
37            throw new InvalidDataException( 'Workflow metadata is not Workflow instance' );
38        }
39
40        if ( $workflow->getType() !== 'topic' ) {
41            wfWarn( __METHOD__ . ': Expected "topic" workflow but received "' . $workflow->getType() . '"' );
42            return;
43        }
44
45        if ( !$workflow->getArticleTitle() ) {
46            return;
47        }
48
49        $this->onAfterInsertExpectedChange( $row['rev_change_type'], $metadata['workflow'] );
50    }
51
52    /**
53     * Returns an array of user ids to subscribe to the title.
54     *
55     * @param string $changeType
56     * @param string $watchType Key of the corresponding 'watch' array in FlowActions.php
57     * @param WatchedTopicItems[] $params Params to feed to callback function that will return
58     *   an array of users to subscribe
59     * @return User[]
60     */
61    public static function getUsersToSubscribe( $changeType, $watchType, array $params = [] ) {
62        /** @var FlowActions $actions */
63        $actions = Container::get( 'flow_actions' );
64
65        // Find users defined for this action, in FlowActions.php
66        try {
67            $users = $actions->getValue( $changeType, 'watch', $watchType );
68        } catch ( \Exception ) {
69            return [];
70        }
71
72        // Null will be returned if nothing is defined for this changeType
73        if ( !$users ) {
74            return [];
75        }
76
77        // Some actions may have more complex logic to determine watching users
78        if ( is_callable( $users ) ) {
79            $users = $users( ...$params );
80        }
81
82        return $users;
83    }
84}