Passed
Push — master ( 9c8d3b...04f5d8 )
by Oss
01:40
created

BpmOnline   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 8.2%

Importance

Changes 0
Metric Value
wmc 8
eloc 69
dl 0
loc 112
ccs 5
cts 61
cp 0.082
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authentication() 0 42 4
A __construct() 0 5 1
A getResponse() 0 44 3
1
<?php
2
/**
3
 * @category    Brownie/BpmOnline
4
 * @author      Brownie <[email protected]>
5
 * @license     https://opensource.org/licenses/MIT
6
 */
7
8
namespace Brownie\BpmOnline;
9
10
use Brownie\BpmOnline\Exception\AuthenticationException;
11
use Brownie\HttpClient\Header\Header;
12
use Brownie\HttpClient\HTTPClient;
13
use Brownie\BpmOnline\DataService\Contract;
14
use Brownie\HttpClient\Request;
15
use Brownie\Util\StorageArray;
16
17
/**
18
 * @method BpmOnline    setHttpClient($httpClient)
19
 * @method HTTPClient   getHttpClient()
20
 * @method BpmOnline    setConfig($config)
21
 * @method Config       getConfig()
22
 * @method BpmOnline    setConfigurationNumber($configurationNumber)
23
 * @method int          getConfigurationNumber()
24
 * @method BpmOnline    setDataFormat($dataFormat)
25
 * @method string       getDataFormat()
26
 * @method BpmOnline    setAuthentication(array $authenticationData)
27
 * @method array        getAuthentication()
28
 */
29
class BpmOnline extends StorageArray
30
{
31
32
    /**
33
     * List of supported fields.
34
     *
35
     * @var array
36
     */
37
    protected $fields = [
38
        'httpClient' => null,
39
        'config' => null,
40
        'configurationNumber' => 0,
41
        'dataFormat' => 'json',
42
        'authentication' => null,
43
    ];
44
45 1
    public function __construct(HTTPClient $httpClient, Config $config)
46
    {
47 1
        parent::__construct([
48 1
            'httpClient' => $httpClient,
49 1
            'config' => $config,
50
        ]);
51 1
    }
52
53
    private function authentication()
54
    {
55
        $request = $this
56
            ->getHttpClient()
57
            ->createRequest()
58
            ->setMethod(Request::HTTP_METHOD_POST)
59
            ->setUrl(
60
                $this->getConfig()->getApiUrlScheme() . '://' .
61
                $this->getConfig()->getUserDomain() . '.' .
62
                $this->getConfig()->getApiDomain() .
63
                '/ServiceModel/AuthService.svc/Login'
64
            )
65
            ->setBodyFormat(Request::FORMAT_APPLICATION_JSON)
66
            ->addHeader(new Header([
67
                'name' => 'Content-Type',
68
                'value' => Request::FORMAT_APPLICATION_JSON,
69
            ]))
70
            ->setBody(json_encode([
71
                'UserName' => $this->getConfig()->getUserName(),
72
                'UserPassword' => $this->getConfig()->getUserPassword(),
73
            ]))
74
            ->setTimeOut($this->getConfig()->getApiConnectTimeOut());
75
76
        $response = $this->getHttpClient()->request($request);
77
78
        if (200 != $response->getHttpCode()) {
79
            return false;
80
        }
81
82
        $jsonResponse = json_decode($response->getBody(), true);
83
84
        if ((JSON_ERROR_NONE != json_last_error()) || (0 != $jsonResponse['Code']))
85
        {
86
            return false;
87
        }
88
89
        $this->setAuthentication([
90
            '.aspxauth' => $response->getHttpCookieList()->get('.aspxauth'),
91
            'bpmcsrf' => $response->getHttpCookieList()->get('bpmcsrf'),
92
        ]);
93
94
        return true;
95
    }
96
97
    public function getResponse(Contract $contract)
98
    {
99
        $contract->validate();
100
101
        if (empty($this->getAuthentication())) {
102
            if (!$this->authentication()) {
103
                throw new AuthenticationException('Authentication failed.');
104
            }
105
        }
106
107
        $request =
108
            $this
109
                ->getHttpClient()
110
                ->createRequest()
111
                ->setMethod(Request::HTTP_METHOD_POST)
112
                ->setUrl(
113
                    $this->getConfig()->getApiUrlScheme() . '://' .
114
                    $this->getConfig()->getUserDomain() . '.' . $this->getConfig()->getApiDomain() .
115
                    '/' . $this->getConfigurationNumber() . '/dataservice/' . $this->getDataFormat() .
116
                    '/reply/' . $contract->getContractType()
117
                )
118
                ->setBodyFormat(Request::FORMAT_APPLICATION_JSON)
119
                ->addHeader(new Header([
120
                    'name' => 'Content-Type',
121
                    'value' => Request::FORMAT_APPLICATION_JSON,
122
                ]))
123
                ->setBody(json_encode($contract->toArray()))
124
                ->addCookie($this->getAuthentication()['.aspxauth'])
125
                ->addCookie($this->getAuthentication()['bpmcsrf'])
126
                ->addHeader(
127
                    $this
128
                        ->getHttpClient()
129
                        ->createHeader('Authorization', 'Cookie')
130
                )
131
                ->addHeader(
132
                    $this
133
                        ->getHttpClient()
134
                        ->createHeader('BPMCSRF', $this->getAuthentication()['bpmcsrf']->getValue())
135
                )
136
                ->setTimeOut($this->getConfig()->getApiConnectTimeOut());
137
138
        $response = $this->getHttpClient()->request($request);
139
140
        return $response->getBody();
141
    }
142
}
143