Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkflowTopicListListener
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTopicListEntry
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 onAfterInsert
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 onAfterUpdate
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Flow\Data\Listener;
4
5use Flow\Data\Index\TopKIndex;
6use Flow\Data\ObjectManager;
7use Flow\Model\TopicListEntry;
8
9/**
10 * Every time an action is performed against something within a topic workflow
11 * the workflow's last_update_timestamp is updated as well.  This listener
12 * passes that updated timestamp along to the topic list last updated index
13 * so that it can reorder any lists this workflow is in.
14 */
15class WorkflowTopicListListener extends AbstractListener {
16
17    /**
18     * @var ObjectManager
19     */
20    protected $topicListStorage;
21
22    /**
23     * @var TopKIndex
24     */
25    protected $topicListLastUpdatedIndex;
26
27    /**
28     * @param ObjectManager $topicListStorage
29     * @param TopKIndex $topicListLastUpdatedIndex
30     */
31    public function __construct( ObjectManager $topicListStorage, TopKIndex $topicListLastUpdatedIndex ) {
32        $this->topicListStorage = $topicListStorage;
33        $this->topicListLastUpdatedIndex = $topicListLastUpdatedIndex;
34    }
35
36    /**
37     * @param string $workflowId
38     * @return TopicListEntry|false
39     */
40    protected function getTopicListEntry( $workflowId ) {
41        $list = $this->topicListStorage->find( [ 'topic_id' => $workflowId ] );
42
43        // One topic maps to only one topic list now
44        if ( $list ) {
45            return reset( $list );
46        } else {
47            return false;
48        }
49    }
50
51    /**
52     * Is this necessary? It seems it doesn't find anything since the topic workflow is
53     * inserted before TopicListEntry (TLE), but then there is a direct listener on the
54     * TLE insertion so it shouldn't be needed.
55     * @param object $object
56     * @param array $new
57     * @param array $metadata
58     */
59    public function onAfterInsert( $object, array $new, array $metadata ) {
60        $entry = $this->getTopicListEntry( $new['workflow_id'] );
61        if ( $entry ) {
62            $row = [
63                    'workflow_last_update_timestamp' => $new['workflow_last_update_timestamp']
64                ] + TopicListEntry::toStorageRow( $entry );
65            $this->topicListLastUpdatedIndex->onAfterInsert( $entry, $row, $metadata );
66        }
67    }
68
69    public function onAfterUpdate( $object, array $old, array $new, array $metadata ) {
70        $entry = $this->getTopicListEntry( $new['workflow_id'] );
71        if ( $entry ) {
72            $row = TopicListEntry::toStorageRow( $entry );
73            $this->topicListLastUpdatedIndex->onAfterUpdate(
74                $entry,
75                [
76                    'workflow_last_update_timestamp' => $old['workflow_last_update_timestamp']
77                ] + $row,
78                [
79                    'workflow_last_update_timestamp' => $new['workflow_last_update_timestamp']
80                ] + $row,
81                $metadata
82            );
83        }
84    }
85}