Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
BoardHistoryIndex
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 8
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 findMulti
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 backingStoreFindMulti
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 cachePurge
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onAfterInsert
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onAfterUpdate
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onAfterRemove
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 findTopicId
n/a
0 / 0
n/a
0 / 0
0
 findTopicListId
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Flow\Data\Index;
4
5use Flow\Data\FlowObjectCache;
6use Flow\Data\ObjectManager;
7use Flow\Data\ObjectMapper;
8use Flow\Data\Storage\BoardHistoryStorage;
9use Flow\Exception\DataModelException;
10use Flow\Model\AbstractRevision;
11use Flow\Model\PostRevision;
12use Flow\Model\PostSummary;
13use Flow\Model\TopicListEntry;
14use Flow\Model\UUID;
15use Flow\Model\Workflow;
16
17/**
18 * Keeps a list of revision ids relevant to the board history bucketed
19 * by the owning TopicList id (board workflow).
20 *
21 * Can be used with Header, PostRevision and PostSummary ObjectMapper's
22 */
23abstract class BoardHistoryIndex extends TopKIndex {
24    /**
25     * @var ObjectManager Manager for the TopicListEntry model
26     */
27    protected $om;
28
29    /**
30     * @inheritDoc
31     */
32    public function __construct(
33        FlowObjectCache $cache,
34        BoardHistoryStorage $storage,
35        ObjectMapper $mapper,
36        $prefix,
37        array $indexed,
38        array $options,
39        ObjectManager $om
40    ) {
41        if ( $indexed !== [ 'topic_list_id' ] ) {
42            throw new DataModelException(
43                __CLASS__ . ' is hardcoded to only index topic_list_id: ' . print_r( $indexed, true ),
44                'process-data'
45            );
46        }
47        parent::__construct( $cache, $storage, $mapper, $prefix, $indexed, $options );
48        $this->om = $om;
49    }
50
51    public function findMulti( array $queries, array $options = [] ) {
52        if ( count( $queries ) > 1 ) {
53            // why?
54            throw new DataModelException( __METHOD__ . ' expects only one value in $queries', 'process-data' );
55        }
56        return parent::findMulti( $queries, $options );
57    }
58
59    /**
60     * @param array $queries
61     * @return array
62     */
63    public function backingStoreFindMulti( array $queries ) {
64        return $this->storage->findMulti(
65            $queries,
66            $this->queryOptions()
67        ) ?: [];
68    }
69
70    /**
71     * @param PostSummary|PostRevision $object
72     * @param string[] $row
73     */
74    public function cachePurge( $object, array $row ) {
75        $row['topic_list_id'] = $this->findTopicListId( $object, $row, [] );
76        parent::cachePurge( $object, $row );
77    }
78
79    /**
80     * @param PostSummary|PostRevision $object
81     * @param string[] $new
82     * @param array $metadata
83     */
84    public function onAfterInsert( $object, array $new, array $metadata ) {
85        $new['topic_list_id'] = $this->findTopicListId( $object, $new, $metadata );
86        parent::onAfterInsert( $object, $new, $metadata );
87    }
88
89    /**
90     * @param PostSummary|PostRevision $object
91     * @param string[] $old
92     * @param string[] $new
93     * @param array $metadata
94     */
95    public function onAfterUpdate( $object, array $old, array $new, array $metadata ) {
96        $new['topic_list_id'] = $old['topic_list_id'] = $this->findTopicListId( $object, $new, $metadata );
97        parent::onAfterUpdate( $object, $old, $new, $metadata );
98    }
99
100    /**
101     * @param PostSummary|PostRevision $object
102     * @param string[] $old
103     * @param array $metadata
104     */
105    public function onAfterRemove( $object, array $old, array $metadata ) {
106        $old['topic_list_id'] = $this->findTopicListId( $object, $old, $metadata );
107        parent::onAfterRemove( $object, $old, $metadata );
108    }
109
110    /**
111     * Find a topic ID given an AbstractRevision
112     *
113     * @param AbstractRevision $object
114     * @return UUID Topic ID
115     */
116    abstract protected function findTopicId( AbstractRevision $object );
117
118    /**
119     * Find a topic list ID related to an AbstractRevision
120     *
121     * @param AbstractRevision $object
122     * @param string[] $row
123     * @param array $metadata
124     * @return string Alphadecimal uid of the related board
125     * @throws DataModelException When the related id cannot be located
126     */
127    protected function findTopicListId( AbstractRevision $object, array $row, array $metadata ) {
128        if ( isset( $metadata['workflow'] ) && $metadata['workflow'] instanceof Workflow ) {
129            $topicId = $metadata['workflow']->getId();
130        } else {
131            $topicId = $this->findTopicId( $object );
132        }
133
134        $found = $this->om->find( [ 'topic_id' => $topicId ] );
135        if ( !$found ) {
136            throw new DataModelException(
137                "No topic list contains topic " . $topicId->getAlphadecimal() .
138                ", called for revision " . $object->getRevisionId()->getAlphadecimal()
139            );
140        }
141
142        /** @var TopicListEntry $topicListEntry */
143        $topicListEntry = reset( $found );
144        return $topicListEntry->getListId()->getAlphadecimal();
145    }
146}