Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WorkflowTopicListListener | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getTopicListEntry | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| onAfterInsert | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| onAfterUpdate | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Data\Listener; |
| 4 | |
| 5 | use Flow\Data\Index\TopKIndex; |
| 6 | use Flow\Data\ObjectManager; |
| 7 | use 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 | */ |
| 15 | class 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 | if ( $list ) { |
| 41 | return reset( $list ); |
| 42 | } else { |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Is this necessary? It seems it doesn't find anything since the topic workflow is |
| 49 | * inserted before TopicListEntry (TLE), but then there is a direct listener on the |
| 50 | * TLE insertion so it shouldn't be needed. |
| 51 | * @param object $object |
| 52 | * @param array $new |
| 53 | * @param array $metadata |
| 54 | */ |
| 55 | public function onAfterInsert( $object, array $new, array $metadata ) { |
| 56 | $entry = $this->getTopicListEntry( $new['workflow_id'] ); |
| 57 | if ( $entry ) { |
| 58 | $row = [ |
| 59 | 'workflow_last_update_timestamp' => $new['workflow_last_update_timestamp'] |
| 60 | ] + TopicListEntry::toStorageRow( $entry ); |
| 61 | $this->topicListLastUpdatedIndex->onAfterInsert( $entry, $row, $metadata ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** @inheritDoc */ |
| 66 | public function onAfterUpdate( $object, array $old, array $new, array $metadata ) { |
| 67 | $entry = $this->getTopicListEntry( $new['workflow_id'] ); |
| 68 | if ( $entry ) { |
| 69 | $row = TopicListEntry::toStorageRow( $entry ); |
| 70 | $this->topicListLastUpdatedIndex->onAfterUpdate( |
| 71 | $entry, |
| 72 | [ |
| 73 | 'workflow_last_update_timestamp' => $old['workflow_last_update_timestamp'] |
| 74 | ] + $row, |
| 75 | [ |
| 76 | 'workflow_last_update_timestamp' => $new['workflow_last_update_timestamp'] |
| 77 | ] + $row, |
| 78 | $metadata |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | } |