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 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
FeatureCompactor
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 4
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 compactRow
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
30
 compactRows
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 expandCacheResult
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3namespace Flow\Data\Compactor;
4
5use Flow\Data\Compactor;
6use Flow\Exception\DataModelException;
7
8/**
9 * Removes the feature fields from stored array since its duplicating the cache key values
10 * Re-adds them when retrieving from cache.
11 */
12class FeatureCompactor implements Compactor {
13    /**
14     * @var string[]
15     */
16    protected $indexed;
17
18    /**
19     * @param string[] $indexedColumns
20     */
21    public function __construct( array $indexedColumns ) {
22        $this->indexed = $indexedColumns;
23    }
24
25    /**
26     * The indexed values are always available when querying, this strips
27     * the duplicated data.
28     *
29     * @param array $row
30     * @return array
31     * @throws DataModelException
32     */
33    public function compactRow( array $row ) {
34        foreach ( $this->indexed as $key ) {
35            unset( $row[$key] );
36        }
37        foreach ( $row as $foo ) {
38            if ( $foo !== null && !is_scalar( $foo ) ) {
39                throw new DataModelException(
40                    'Attempted to compact row containing objects, must be scalar values: ' .
41                    print_r( $foo, true ), 'process-data'
42                );
43            }
44        }
45        return $row;
46    }
47
48    /**
49     * @param array[] $rows
50     * @return array[]
51     */
52    public function compactRows( array $rows ) {
53        return array_map( [ $this, 'compactRow' ], $rows );
54    }
55
56    /**
57     * The $cached array is three dimensional.  Each top level key is a cache key
58     * and contains an array of rows.  Each row is an array representing a single data model.
59     *
60     * $cached = [ $cacheKey => [ [ 'rev_id' => 123, ... ], ... ], ... ]
61     *
62     * The $keyToQuery array maps from cache key to the values that were used to build the cache key.
63     * These values are re-added to the results found in memcache.
64     *
65     * @param array $cached Array of results from BagOStuff::multiGet each containing a list of rows
66     * @param array $keyToQuery Map from key in $cached to the values used to generate that key
67     * @return array The $cached array with the queried values merged in
68     * @throws DataModelException
69     */
70    public function expandCacheResult( array $cached, array $keyToQuery ) {
71        foreach ( $cached as $key => $rows ) {
72            $query = $keyToQuery[$key] ?? [];
73            if ( !is_array( $query ) ) {
74                throw new DataModelException( 'Cached data for "' . $key .
75                    '"" should map to a valid query: ' . print_r( $query, true ), 'process-data' );
76            }
77
78            foreach ( $query as $foo ) {
79                if ( $foo !== null && !is_scalar( $foo ) ) {
80                    throw new DataModelException(
81                        'Query values to merge with cache contains objects, should be scalar values: ' .
82                        print_r( $foo, true ),
83                        'process-data'
84                    );
85                }
86            }
87            foreach ( $rows as $k => $row ) {
88                foreach ( $row as $foo ) {
89                    if ( $foo !== null && !is_scalar( $foo ) ) {
90                        throw new DataModelException(
91                            'Result from cache contains objects, should be scalar values: ' .
92                            print_r( $foo, true ),
93                            'process-data'
94                        );
95                    }
96                }
97                $cached[$key][$k] += $query;
98            }
99        }
100
101        return $cached;
102    }
103}