Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| PostRevisionTopicHistoryIndex | |
0.00% |
0 / 22 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| cachePurge | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| onAfterInsert | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| onAfterUpdate | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| onAfterRemove | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| findTopicId | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| backingStoreFindMulti | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Data\Index; |
| 4 | |
| 5 | use Flow\Collection\PostCollection; |
| 6 | use Flow\Data\FlowObjectCache; |
| 7 | use Flow\Data\ObjectMapper; |
| 8 | use Flow\Data\Storage\PostRevisionTopicHistoryStorage; |
| 9 | use Flow\Exception\DataModelException; |
| 10 | use Flow\Model\PostRevision; |
| 11 | use Flow\Model\UUID; |
| 12 | use InvalidArgumentException; |
| 13 | |
| 14 | /** |
| 15 | * TopKIndex that calculates the topic_root_id |
| 16 | */ |
| 17 | class PostRevisionTopicHistoryIndex extends TopKIndex { |
| 18 | public function __construct( |
| 19 | FlowObjectCache $cache, |
| 20 | PostRevisionTopicHistoryStorage $storage, |
| 21 | ObjectMapper $mapper, |
| 22 | $prefix, |
| 23 | array $indexed, |
| 24 | array $options = [] |
| 25 | ) { |
| 26 | if ( $indexed !== [ 'topic_root_id' ] ) { |
| 27 | throw new InvalidArgumentException( __CLASS__ . ' is hardcoded to only index topic_root_id: ' . |
| 28 | print_r( $indexed, true ) ); |
| 29 | } |
| 30 | parent::__construct( $cache, $storage, $mapper, $prefix, $indexed, $options ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param PostRevision $object |
| 35 | * @param array $row |
| 36 | */ |
| 37 | public function cachePurge( $object, array $row ) { |
| 38 | $row['topic_root_id'] = $this->findTopicId( $object ); |
| 39 | parent::cachePurge( $object, $row ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param PostRevision $object |
| 44 | * @param string[] $new |
| 45 | * @param array $metadata |
| 46 | */ |
| 47 | public function onAfterInsert( $object, array $new, array $metadata ) { |
| 48 | $new['topic_root_id'] = $this->findTopicId( $object ); |
| 49 | parent::onAfterInsert( $object, $new, $metadata ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param PostRevision $object |
| 54 | * @param string[] $old |
| 55 | * @param string[] $new |
| 56 | * @param array $metadata |
| 57 | */ |
| 58 | public function onAfterUpdate( $object, array $old, array $new, array $metadata ) { |
| 59 | $old['topic_root_id'] = $new['topic_root_id'] = $this->findTopicId( $object ); |
| 60 | parent::onAfterUpdate( $object, $old, $new, $metadata ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param PostRevision $object |
| 65 | * @param string[] $old |
| 66 | * @param array $metadata |
| 67 | */ |
| 68 | public function onAfterRemove( $object, array $old, array $metadata ) { |
| 69 | $old['topic_root_id'] = $this->findTopicId( $object ); |
| 70 | parent::onAfterRemove( $object, $old, $metadata ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Finds topic ID for given Post |
| 75 | * |
| 76 | * @param PostRevision $post |
| 77 | * @return UUID Topic ID |
| 78 | * @throws DataModelException |
| 79 | */ |
| 80 | protected function findTopicId( PostRevision $post ) { |
| 81 | try { |
| 82 | $root = $post->getCollection()->getRoot(); |
| 83 | } catch ( DataModelException ) { |
| 84 | // in some cases, we may fail to find root post from the current |
| 85 | // object (e.g. data has already been removed) |
| 86 | // try to find if via parent, in that case |
| 87 | $parentId = $post->getReplyToId(); |
| 88 | if ( $parentId === null ) { |
| 89 | throw new DataModelException( 'Unable to locate root for post ' . |
| 90 | $post->getCollectionId() ); |
| 91 | } |
| 92 | |
| 93 | $parent = PostCollection::newFromId( $parentId ); |
| 94 | $root = $parent->getRoot(); |
| 95 | } |
| 96 | |
| 97 | return $root->getId(); |
| 98 | } |
| 99 | |
| 100 | protected function backingStoreFindMulti( array $queries ) { |
| 101 | return $this->storage->findMulti( $queries, $this->queryOptions() ); |
| 102 | } |
| 103 | } |