ConfigurationCreator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 80
c 0
b 0
f 0
ccs 28
cts 28
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 38 3
1
<?php
2
3
namespace LaFourchette\AdobeCampaignClientBundle\Client;
4
5
use LaFourchette\AdobeCampaignClientBundle\Exception\ConfigurationCreationException;
6
use LaFourchette\AdobeCampaignClientBundle\Util\AdobeCampaignXmlLoader;
7
8
/**
9
 * Create a Adobe Configuration object with security informations
10
 */
11
class ConfigurationCreator
12
{
13
    const SOAP_MESSAGE_PAYLOAD = <<<EOT
14
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:xtk:session">
15
    <x:Header/>
16
    <x:Body>
17
        <urn:Logon>
18
            <urn:sessiontoken/>
19
            <urn:strLogin>%s</urn:strLogin>
20
            <urn:strPassword>%s</urn:strPassword>
21
            <urn:elemParameters/>
22
        </urn:Logon>
23
    </x:Body>
24
</x:Envelope>
25
EOT;
26
27
    /**
28
     * @var ClientInstantiator
29
     */
30
    private $clientInstantiator;
31
32
    /**
33
     * @var array Configuration informations
34
     */
35
    private $config;
36
37
    /**
38
     * @param ClientInstantiator $clientInstantiator
39
     * @param array $config
40
     */
41 3
    public function __construct(ClientInstantiator $clientInstantiator, $config)
42
    {
43 3
        $this->clientInstantiator = $clientInstantiator;
44 3
        $this->config = $config;
45 3
    }
46
47
    /**
48
     * Create a Configuration object
49
     *
50
     * @return Configuration
51
     */
52 3
    public function create()
53
    {
54 3
        $soapClient = $this->clientInstantiator->instantiateBasicClient(null, array(
55 3
            'location' => $this->config['base_uri'].Client::SOAP_ROUTER_PATH,
56 3
            'uri' => $this->config['base_uri'],
57
            'trace' => 1
58 3
        ));
59
60
        try {
61 3
            $response = $soapClient->__doRequest(
62 3
                sprintf(self::SOAP_MESSAGE_PAYLOAD, $this->config['login'], $this->config['password']),
63 3
                $this->config['base_uri'].Client::SOAP_ROUTER_PATH,
64 3
                'xtk:session#Logon',
65
                1
66 3
            );
67 3
        } catch(\Exception $e) {
68 1
            throw new ConfigurationCreationException($e->getMessage(), $e->getCode(), $e);
69
        }
70
71 2
        if (null === $response) {
72 1
            throw new ConfigurationCreationException('Empty response received');
73
        }
74
75 1
        $xmlResponse = AdobeCampaignXmlLoader::loadXml($response);
76
77 1
        $xmlSessionToken = $xmlResponse->xpath('/Envelope/Body/LogonResponse/pstrSessionToken');
78 1
        $session = array_pop($xmlSessionToken)->__toString();
79
80 1
        $xmlSecurityToken = $xmlResponse->xpath('/Envelope/Body/LogonResponse/pstrSecurityToken');
81 1
        $security = array_pop($xmlSecurityToken)->__toString();
82
83 1
        $configuration = new Configuration();
84 1
        $configuration->setBaseUri($this->config['base_uri']);
85 1
        $configuration->setSecurity($security);
86 1
        $configuration->setSession($session);
87
88 1
        return $configuration;
89
    }
90
}
91