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

BpmOnline::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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