1 | <?php |
||
19 | class EntityStoreFromConfigurationBuilder { |
||
20 | |||
21 | /** |
||
22 | * @param string $configurationFileName |
||
23 | * @return EntityStore |
||
24 | */ |
||
25 | 2 | public function buildEntityStore( $configurationFileName ) { |
|
26 | 2 | $config = $this->parseConfiguration( $configurationFileName ); |
|
27 | |||
28 | 2 | $store = $this->buildEntityStoreFromConfig( $config ); |
|
29 | |||
30 | 2 | if( array_key_exists( 'cache', $config ) ) { |
|
31 | 1 | $cache = $this->buildCacheFromConfig( $config['cache'] ); |
|
32 | |||
33 | 1 | if( $cache !== null ) { |
|
34 | 1 | return new CachedEntityStore( $store, $cache, $config['cache']['lifetime'] ); |
|
35 | } |
||
36 | } |
||
37 | |||
38 | 1 | return $store; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @param string $configurationFileName |
||
43 | * @return Cache |
||
44 | */ |
||
45 | public function buildCache( $configurationFileName ) { |
||
46 | $config = $this->parseConfiguration( $configurationFileName ); |
||
47 | |||
48 | if( !array_key_exists( 'cache', $config ) ) { |
||
49 | throw new InvalidArgumentException( 'No cache key in configuration' ); |
||
50 | } |
||
51 | return $this->buildCacheFromConfig( $config['cache'] ); |
||
52 | } |
||
53 | |||
54 | 2 | private function buildEntityStoreFromConfig( $config ) { |
|
55 | 2 | $options = new EntityStoreOptions( $config['options'] ); |
|
56 | |||
57 | 2 | switch( $config['backend'] ) { |
|
58 | 2 | case 'api': |
|
59 | 1 | return new ApiEntityStore( |
|
60 | 1 | $this->getWikibaseApi( $config['api'] ), |
|
61 | 1 | $this->getWikidataQueryApi( $config['api'] ), |
|
62 | $options |
||
63 | 1 | ); |
|
64 | 1 | case 'mongodb': |
|
65 | 1 | return new MongoDBEntityStore( $this->getMongoDbDatabase( $config['mongodb'] ), $options ); |
|
66 | 1 | default: |
|
67 | throw new InvalidArgumentException( 'Unknown backend: ' . $config['backend'] ); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | 1 | private function getWikibaseApi( $config ) { |
|
74 | |||
75 | 1 | private function getWikidataQueryApi( $config ) { |
|
76 | 1 | if( !array_key_exists( 'wikidataquery_url', $config ) ) { |
|
77 | return null; |
||
82 | |||
83 | 1 | private function getMongoDbDatabase( $config ) { |
|
92 | |||
93 | 1 | private function buildCacheFromConfig( $config ) { |
|
121 | |||
122 | 2 | private function parseConfiguration( $configurationFileName ) { |
|
132 | } |
||
133 |