Passed
Push — master ( ca0e1d...febc39 )
by Oss
01:48
created

BpmOnline   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 17.24%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 66
ccs 5
cts 29
cp 0.1724
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A authentication() 0 29 2
A getResponse() 0 11 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. Did you maybe mean authentication()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

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())) {
0 ignored issues
show
Bug introduced by
The method getAuthentication() does not exist on Brownie\BpmOnline\BpmOnline. Did you maybe mean authentication()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85
            if (!$this->authentication()) {
86
                throw new AuthenticationException('Authentication failed.');
87
            }
88
        }
89
90
    }
91
}
92