Completed
Push — master ( 841483...e7a85a )
by methylbro
8s
created

Connection::open()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 17
cts 17
cp 1
rs 8.8571
cc 2
eloc 14
nc 2
nop 4
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 2
                    $class, $expected
66 2
                ));
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
     * @return Array
77
     */
78 2
    private function resolveOptions()
79
    {
80
        $options = [
81
            'classmap' => [
82 2
                'ArrayOfListInfo' => 'Mediapart\Selligent\ArrayOfListInfo',
83 2
                'ListInfo' => 'Mediapart\Selligent\ListInfo',
84 2
                'Property' => 'Mediapart\Selligent\Property',
85 2
                'ArrayOfProperty' => 'Mediapart\Selligent\Properties',
86
87
                # System info
88 2
                'GetSystemStatusResponse' => 'Mediapart\Selligent\Response\GetSystemStatusResponse',
89
90
                # Manage Lists
91 2
                'GetListsResponse' => 'Mediapart\Selligent\Response\GetListsResponse',
92 2
                'GetListIDResponse' => 'Mediapart\Selligent\Response\GetListIDResponse',
93
94
                # Manage Segments
95 2
                'CreateSegmentResponse' => 'Mediapart\Selligent\Response\CreateSegmentResponse',
96 2
                'AddToSegmentResponse' => 'Mediapart\Selligent\Response\AddToSegmentResponse',
97 2
                'GetSegmentsResponse' => 'Mediapart\Selligent\Response\GetSegmentsResponse',
98 2
                'GetSegmentRecordCountResponse' => 'Mediapart\Selligent\Response\GetSegmentRecordCountResponse',
99
100
                # Manage Users
101 2
                'CreateUserResponse' => 'Mediapart\Selligent\Response\CreateUserResponse',
102 2
                'UpdateUserResponse' => 'Mediapart\Selligent\Response\UpdateUserResponse',
103 2
                'UpdateUsersResponse' => 'Mediapart\Selligent\Response\UpdateUsersResponse',
104 2
                'GetUserByIDResponse' => 'Mediapart\Selligent\Response\GetUserByIDResponse',
105 2
                'RetrieveHashForUserResponse' => 'Mediapart\Selligent\Response\RetrieveHashForUserResponse',
106 2
                'CountUsersByConstraintResponse' => 'Mediapart\Selligent\Response\CountUsersByConstraintResponse',
107 2
                'GetUsersByConstraintResponse' => 'Mediapart\Selligent\Response\GetUsersByConstraintResponse',
108 2
                'GetUserByConstraintResponse' => 'Mediapart\Selligent\Response\GetUserByConstraintResponse',
109 2
                'CountUsersByFilterResponse' => 'Mediapart\Selligent\Response\CountUsersByFilterResponse',
110 2
                'GetUsersByFilterResponse' => 'Mediapart\Selligent\Response\GetUsersByFilterResponse',
111 2
                'GetUserByFilterResponse' => 'Mediapart\Selligent\Response\GetUserByFilterResponse',
112
113
                # Manage Campaign
114 2
                'TriggerCampaignResponse' => 'Mediapart\Selligent\Response\TriggerCampaignResponse',
115 2
                'TriggerCampaignWithResultResponse' => 'Mediapart\Selligent\Response\TriggerCampaignWithResultResponse',
116 2
                'TriggerCampaignForUserResponse' => 'Mediapart\Selligent\Response\TriggerCampaignForUserResponse',
117 2
                'TriggerCampaignForUserWithResultResponse' => 'Mediapart\Selligent\Response\TriggerCampaignForUserWithResultResponse',
118
            ]
119 2
        ];
120
121 2
        if ($this->logger) {
122 1
            $this->logger->debug('resolved options', $options);
123 1
        }
124
125 2
        return $options;
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131 1
    public function setLogger(LoggerInterface $logger)
132
    {
133 1
        $this->logger = $logger;
134 1
    }
135
136
    /**
137
     *
138
     * @var string $login
139
     * @var string $password
140
     * @var string $wsdl
141
     * @var string $namespace
142
     *
143
     * @return \SoapClient
144
     */
145 2
    public function open($login, $password, $wsdl, $namespace = 'http://tempuri.org/')
146
    {
147 2
        $this->options = $this->resolveOptions();
148
149 2
        if ($this->logger) {
150 1
            $this->logger->debug(sprintf('connecting to %s', $wsdl));
151 1
        }
152
153 2
        $client = $this
154
            ->client
155 2
            ->newInstance($wsdl, $this->options)
156 2
        ;
157 2
        $client->__setSoapHeaders(
158 2
            $this->header->newInstance(
159 2
                $namespace,
160 2
                'AutomationAuthHeader',
161
                [
162 2
                    'Login' => $login,
163 2
                    'Password' => $password,
164
                ]
165 2
            )
166 2
        );
167
168
169 2
        return $client;
170
    }
171
}
172