Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
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 / 23
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 / 2
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    public function __construct( ObjectManager $topicListStorage, TopKIndex $topicListLastUpdatedIndex ) {
28        $this->topicListStorage = $topicListStorage;
29        $this->topicListLastUpdatedIndex = $topicListLastUpdatedIndex;
30    }
31
32    /**
33     * @param string $workflowId
34     * @return TopicListEntry|false
35     */
36    protected function getTopicListEntry( $workflowId ) {
37        $list = $this->topicListStorage->find( [ 'topic_id' => $workflowId ] );
38
39        // One topic maps to only one topic list now
40        return $list ? reset( $list ) : false;
41    }
42
43    /**
44     * Is this necessary? It seems it doesn't find anything since the topic workflow is
45     * inserted before TopicListEntry (TLE), but then there is a direct listener on the
46     * TLE insertion so it shouldn't be needed.
47     * @param object $object
48     * @param array $new
49     * @param array $metadata
50     */
51    public function onAfterInsert( $object, array $new, array $metadata ) {
52        $entry = $this->getTopicListEntry( $new['workflow_id'] );
53        if ( $entry ) {
54            $row = [
55                    'workflow_last_update_timestamp' => $new['workflow_last_update_timestamp']
56                ] + TopicListEntry::toStorageRow( $entry );
57            $this->topicListLastUpdatedIndex->onAfterInsert( $entry, $row, $metadata );
58        }
59    }
60
61    /** @inheritDoc */
62    public function onAfterUpdate( $object, array $old, array $new, array $metadata ) {
63        $entry = $this->getTopicListEntry( $new['workflow_id'] );
64        if ( $entry ) {
65            $row = TopicListEntry::toStorageRow( $entry );
66            $this->topicListLastUpdatedIndex->onAfterUpdate(
67                $entry,
68                [
69                    'workflow_last_update_timestamp' => $old['workflow_last_update_timestamp']
70                ] + $row,
71                [
72                    'workflow_last_update_timestamp' => $new['workflow_last_update_timestamp']
73                ] + $row,
74                $metadata
75            );
76        }
77    }
78}