Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.71% covered (danger)
35.71%
10 / 28
20.00% covered (danger)
20.00%
3 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManagerGroup
35.71% covered (danger)
35.71%
10 / 28
20.00% covered (danger)
20.00%
3 / 15
114.91
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 clear
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 cachePurge
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStorage
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
2.15
 put
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 multiMethod
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 multiPut
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 multiRemove
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 call
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMulti
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 find
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findMulti
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 found
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 foundMulti
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Flow\Data;
4
5use Flow\Container;
6use Flow\Exception\DataModelException;
7
8/**
9 * A little glue code to allow passing around and manipulating multiple
10 * ObjectManagers more conveniently.
11 */
12class ManagerGroup {
13    /**
14     * @var Container
15     */
16    protected $container;
17
18    /**
19     * @var string[] Map from FQCN or short name to key in container that holds
20     *  the relevant ObjectManager
21     */
22    protected $classMap;
23
24    /**
25     * @var bool[] List of container keys that have been used
26     */
27    protected $used = [];
28
29    /**
30     * @param Container $container
31     * @param string[] $classMap Map from ObjectManager alias to container key
32     *  holding that object manager.
33     */
34    public function __construct( Container $container, array $classMap ) {
35        $this->container = $container;
36        $this->classMap = $classMap;
37    }
38
39    /**
40     * Runs ObjectManager::clear on all managers that have been accessed since
41     * the last clear.
42     */
43    public function clear() {
44        foreach ( array_keys( $this->used ) as $key ) {
45            $this->container[$key]->clear();
46        }
47        $this->used = [];
48    }
49
50    /**
51     * Purge all cached data related to this object
52     *
53     * @param object $object
54     */
55    public function cachePurge( $object ) {
56        $this->getStorage( get_class( $object ) )->cachePurge( $object );
57    }
58
59    /**
60     * @param string $className
61     * @return ObjectManager
62     * @throws DataModelException
63     */
64    public function getStorage( $className ) {
65        if ( !isset( $this->classMap[$className] ) ) {
66            throw new DataModelException( "Request for '$className' is not in classmap: " .
67                implode( ', ', array_keys( $this->classMap ) ), 'process-data' );
68        }
69        $key = $this->classMap[$className];
70        $this->used[$key] = true;
71
72        return $this->container[$key];
73    }
74
75    /**
76     * @param object $object
77     * @param array $metadata
78     * @throws DataModelException
79     */
80    public function put( $object, array $metadata ) {
81        $this->getStorage( get_class( $object ) )->put( $object, $metadata );
82    }
83
84    /**
85     * @param string $method
86     * @param array $objects
87     * @param array $metadata
88     * @throws DataModelException
89     */
90    protected function multiMethod( $method, array $objects, array $metadata ) {
91        $itemsByClass = [];
92
93        foreach ( $objects as $object ) {
94            $itemsByClass[ get_class( $object ) ][] = $object;
95        }
96
97        foreach ( $itemsByClass as $class => $myObjects ) {
98            $this->getStorage( $class )->$method( $myObjects, $metadata );
99        }
100    }
101
102    /**
103     * @param array $objects
104     * @param array $metadata
105     */
106    public function multiPut( array $objects, array $metadata = [] ) {
107        $this->multiMethod( 'multiPut', $objects, $metadata );
108    }
109
110    /**
111     * @param array $objects
112     * @param array $metadata
113     */
114    public function multiRemove( array $objects, array $metadata = [] ) {
115        $this->multiMethod( 'multiRemove', $objects, $metadata );
116    }
117
118    /**
119     * @param string $method
120     * @param array $args
121     * @return mixed
122     * @throws DataModelException
123     */
124    protected function call( $method, $args ) {
125        $className = array_shift( $args );
126
127        return $this->getStorage( $className )->$method( ...$args );
128    }
129
130    public function get( ...$args ) {
131        return $this->call( __FUNCTION__, $args );
132    }
133
134    public function getMulti( ...$args ) {
135        return $this->call( __FUNCTION__, $args );
136    }
137
138    public function find( ...$args ) {
139        return $this->call( __FUNCTION__, $args );
140    }
141
142    public function findMulti( ...$args ) {
143        return $this->call( __FUNCTION__, $args );
144    }
145
146    public function found( ...$args ) {
147        return $this->call( __FUNCTION__, $args );
148    }
149
150    public function foundMulti( ...$args ) {
151        return $this->call( __FUNCTION__, $args );
152    }
153}