Completed
Branch master (1820a0)
by Oss
02:45
created

BpmOnline   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 17.86%

Importance

Changes 0
Metric Value
wmc 6
eloc 32
dl 0
loc 61
ccs 5
cts 28
cp 0.1786
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authentication() 0 28 2
A __construct() 0 5 1
A getResponse() 0 7 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\HTTPClient;
12
use Brownie\BpmOnline\DataService\Contract;
13
use Brownie\HttpClient\Request;
14
use Brownie\Util\StorageArray;
15
16
/**
17
 * @method BpmOnline    setHttpClient($httpClient)
18
 * @method HTTPClient   getHttpClient()
19
 * @method BpmOnline    setConfig($config)
20
 * @method Config       getConfig()
21
 * @method BpmOnline    setConfigurationNumber($configurationNumber)
22
 * @method int          getConfigurationNumber()
23
 * @method BpmOnline    setDataFormat($dataFormat)
24
 * @method string       getDataFormat()
25
 */
26
class BpmOnline extends StorageArray
27
{
28
29
    /**
30
     * List of supported fields.
31
     *
32
     * @var array
33
     */
34
    protected $fields = [
35
        'httpClient' => null,
36
        'config' => null,
37
        'configurationNumber' => 0,
38
        'dataFormat' => 'json',
39
        'authentication' => null,
40
    ];
41
42 1
    public function __construct(HTTPClient $httpClient, Config $config)
43
    {
44 1
        parent::__construct([
45 1
            'httpClient' => $httpClient,
46 1
            'config' => $config,
47
        ]);
48 1
    }
49
50
    private function authentication()
51
    {
52
        $request = new Request();
53
        $request
54
            ->setMethod(Request::HTTP_METHOD_POST)
55
            ->setUrl(
56
                $this->getConfig()->getApiUrlScheme() . '://' .
57
                //$this->getConfig()->getUserDomain() . '.' .
58
                'www.' . $this->getConfig()->getApiDomain() .
59
                '/ServiceModel/AuthService.svc/Login'
60
            )
61
            ->setBodyFormat(Request::FORMAT_APPLICATION_JSON)
62
            ->setBody(json_encode([
63
                'UserName' => $this->getConfig()->getUserName(),
64
                'UserPassword' => $this->getConfig()->getUserPassword(),
65
            ]))
66
            ->setTimeOut($this->getConfig()->getApiConnectTimeOut());
67
68
        $response = $this->getHttpClient()->request($request);
69
70
        if (200 == $response->getHttpCode()) {
71
            $this->setAuthentication([
0 ignored issues
show
Bug introduced by
The method setAuthentication() does not exist on Brownie\BpmOnline\BpmOnline. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            $this->/** @scrutinizer ignore-call */ 
72
                   setAuthentication([
Loading history...
72
                'cookie' => $response->getHttpCookieList()->toArray()
73
            ]);
74
            return true;
75
        }
76
77
        return false;
78
    }
79
80
    public function getResponse(Contract $contract)
81
    {
82
        $contract->validate();
83
84
        if (empty($this->getAuthentication())) {
85
            if (!$this->authentication()) {
86
                throw new AuthenticationException('Authentication failed.');
87
            }
88
        }
89
90
    }
91
}
92