Completed
Pull Request — master (#8)
by
unknown
02:46
created

Connection::open()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of the Mediapart Selligent Client API
5
 *
6
 * CC BY-NC-SA <https://github.com/mediapart/selligent>
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mediapart\Selligent;
13
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\LoggerAwareInterface;
16
17
/**
18
 *
19
 */
20
class Connection implements LoggerAwareInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    const CLASS_SOAPCLIENT = 'SoapClient';
26
27
    /**
28
     * @var string
29
     */
30
    const CLASS_SOAPHEADER = 'SoapHeader';
31
32
    /**
33
     * @var ReflectionClass
34
     */
35
    private $client;
36
37
    /**
38
     * @var ReflectionClass
39
     */
40
    private $header;
41
42
    /**
43
     * @var Array
44
     */
45
    private $options = [];
46
47
    /**
48
     * @var LoggerInterface
49
     */
50
    private $logger = null;
51
52
    /**
53
     * @param string $client
54
     * @param string $header
55
     *
56
     * @throws InvalidArgumentException
57
     */
58
    public function __construct($client = 'SoapClient', $header = 'SoapHeader')
59
    {
60 4
        $load = function($class, $expected) {
61 4
            $result = new \ReflectionClass($class);
62 4
            if ($class != $expected && !$result->isSubclassOf($expected)) {
63 2
                throw new \InvalidArgumentException(sprintf(
64 2
                    "%s is not an instance of %s",
65
                    $class, $expected
66
                ));
67
            }
68 3
            return $result;
69 4
        };
70
71 4
        $this->client = $load($client, self::CLASS_SOAPCLIENT);
72 3
        $this->header = $load($header, self::CLASS_SOAPHEADER);
73 2
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78 1
    public function setLogger(LoggerInterface $logger)
79
    {
80 1
        $this->logger = $logger;
81 1
    }
82
83
84
    /**
85
     * @param array $config
86
     *
87
     * @return \SoapClient
88
     */
89 2
    public function open(array $config = [])
90
    {
91 2
        $this->options = $config['options']['classmap'];
92
93 2
        if ($this->logger) {
94 1
            $this->logger->debug(sprintf('connecting to %s', $config['wsdl']));
95
        }
96
97
        $client = $this
98 2
            ->client
99 2
            ->newInstance($config['wsdl'], $this->options)
100
        ;
101 2
        $client->__setSoapHeaders(
102 2
            $this->header->newInstance(
103 2
                $config['namespace'],
104 2
                'AutomationAuthHeader',
105
                [
106 2
                    'Login' => $config['login'],
107 2
                    'Password' => $config['password'],
108
                ]
109
            )
110
        );
111
112 2
        return $client;
113
    }
114
}
115