AcquiaSearchService::factory()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 21
cts 21
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 5
nop 2
crap 6
1
<?php
2
3
namespace Acquia\Search;
4
5
use Acquia\Network\Subscription;
6
use Guzzle\Service\Builder\ServiceBuilder;
7
8
class AcquiaSearchService extends ServiceBuilder
9
{
10
    /**
11
     * {@inheritdoc}
12
     *
13
     * @return \Guzzle\Service\Builder\ServiceBuilder
14
     */
15 12
    public static function factory($config = null, array $globalParameters = array())
16
    {
17 12
        if ($config instanceof Subscription) {
18
19 12
            $subscription = $config;
20 12
            if (!isset($subscription['derived_key_salt'])) {
21 3
                throw new \UnexpectedValueException('Derived key salt not found in subscription');
22
            }
23 9
            if (!isset($subscription['heartbeat_data']['search_service_colony'])) {
24 3
                throw new \UnexpectedValueException('Acquia Search hostname not found in subscription');
25
            }
26 6
            if (!isset($subscription['heartbeat_data']['search_cores'])) {
27 3
                throw new \UnexpectedValueException('Index data not found in subscription');
28
            }
29
30 3
            $derivedKey = new DerivedKey($subscription['derived_key_salt'], $subscription->getKey());
31
32 3
            $config = array('services' => array());
33 3
            foreach ($subscription['heartbeat_data']['search_cores'] as $indexInfo) {
34
35 3
                $config['services'][$indexInfo['core_id']] = array(
36 3
                    'class' => 'Acquia\Search\AcquiaSearchClient',
37
                    'params' => array(
38 3
                        'base_url' => 'https://' . $indexInfo['balancer'],
39 3
                        'index_id' => $indexInfo['core_id'],
40 3
                        'derived_key' => $derivedKey->generate($indexInfo['core_id']),
41 3
                    ),
42
                );
43 3
            }
44 3
        }
45
46 3
        return parent::factory($config, $globalParameters);
47
    }
48
}
49