Connection::setLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
use Symfony\Component\Yaml\Yaml;
17
use Symfony\Component\Config\Definition\Processor;
18
19
/**
20
 *
21
 */
22
class Connection implements LoggerAwareInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    const CLASS_SOAPCLIENT = 'SoapClient';
28
29
    /**
30
     * @var string
31
     */
32
    const CLASS_SOAPHEADER = 'SoapHeader';
33
34
    /**
35
     * @var string
36
     */
37
    const API_INDIVIDUAL = 'individual.yml';
38
39
    /**
40
     * @var string
41
     */
42
    const API_BROADCAST = 'broadcast.yml';
43
44
    /**
45
     * @var ReflectionClass
46
     */
47
    private $client;
48
49
    /**
50
     * @var ReflectionClass
51
     */
52
    private $header;
53
54
    /**
55
     * @var Array
56
     */
57
    private $options = [];
0 ignored issues
show
Unused Code introduced by
The property $options is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
58
59
    /**
60
     * @var LoggerInterface
61
     */
62
    private $logger = null;
63
64
    /**
65
     * @param string $client
66
     * @param string $header
67
     *
68
     * @throws InvalidArgumentException
69
     */
70
    public function __construct($client = 'SoapClient', $header = 'SoapHeader')
71
    {
72 4
        $load = function($class, $expected) {
73 4
            $result = new \ReflectionClass($class);
74 4
            if ($class != $expected && !$result->isSubclassOf($expected)) {
75 2
                throw new \InvalidArgumentException(sprintf(
76 2
                    "%s is not an instance of %s",
77 2
                    $class, $expected
78
                ));
79
            }
80 3
            return $result;
81 4
        };
82
83 4
        $this->client = $load($client, self::CLASS_SOAPCLIENT);
84 3
        $this->header = $load($header, self::CLASS_SOAPHEADER);
85 2
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 1
    public function setLogger(LoggerInterface $logger)
91
    {
92 1
        $this->logger = $logger;
93 1
    }
94
95
96
    /**
97
     * @param array $parameters
98
     * @param string $config
99
     *
100
     * @return \SoapClient
101
     */
102 2
    public function open(array $parameters = [], $config = 'individual.yml', array $options = [])
103
    {
104 2
        $processor = new Processor();
105 2
        $configuration = $processor->processConfiguration(
106 2
            new Configuration(),
107
            [
108 2
                Yaml::parse(file_get_contents(__DIR__.'/../config/'.$config)),
109 2
                $parameters
110
            ]
111
        );
112
113 2
        if ($this->logger) {
114 1
            $this->logger->debug(sprintf('connecting to %s', $configuration['wsdl']));
115
        }
116
117
        $client = $this
118 2
            ->client
119 2
            ->newInstance($configuration['wsdl'], array_merge($options, $configuration['options']))
120
        ;
121 2
        $client->__setSoapHeaders(
122 2
            $this->header->newInstance(
123 2
                $configuration['namespace'],
124 2
                'AutomationAuthHeader',
125
                [
126 2
                    'Login' => $configuration['login'],
127 2
                    'Password' => $configuration['password'],
128
                ]
129
            )
130
        );
131
132 2
        return $client;
133
    }
134
}
135