Passed
Branch master (e96466)
by Chubarov
03:06
created

CookieAuthentication::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
namespace agoalofalife\bpm\Assistants;
3
4
use agoalofalife\bpm\Contracts\Authentication;
5
6
/**
7
 * Class CookieAuthentication
8
 * @package agoalofalife\bpm\Assistants
9
 */
10
class CookieAuthentication implements Authentication
11
{
12
    protected $prefixCSRF       = 'BPMCSRF';
13
    protected $pathToCookieFile = __DIR__ . '/../resource/cookie.txt';
14
    protected $configuration;
15
16 2
    public function setConfig(array $config)
17
    {
18 2
        $this->configuration = $config;
19 2
    }
20
21 5
    public function getPathCookieFile()
22
    {
23 5
        return $this->pathToCookieFile;
24
    }
25
26 2
    public function setPathCookieFile($path)
27
    {
28 2
        $this->pathToCookieFile = $path;
29 2
    }
30
31
    /**
32
     * Getting the cookie and write it to a file
33
     * @return boolean
34
     */
35 2
    public function auth()
36
    {
37 2
            $curl    = curl_init();
38
            $headers = array(
39 2
                "POST  HTTP/1.0",
40
                "Content-type: application/json"
41 2
            );
42
43 2
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
44 2
            curl_setopt($curl, CURLOPT_HEADER, 1);
45 2
            curl_setopt($curl, CURLOPT_URL,  $this->configuration['UrlLogin']);
46 2
            curl_setopt($curl, CURLOPT_POST, true);
47 2
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
48 2
            curl_setopt($curl, CURLOPT_COOKIEJAR, $this->pathToCookieFile);
49
50 2
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array(
51 2
                'UserName'       => $this->configuration['Login'],
52 2
                'UserPassword'   => $this->configuration['Password'],
53 2
                'SolutionName'   => 'TSBpm',
54 2
                'TimeZoneOffset' => '-120',
55 2
                'Language'       => 'Ru-ru')));
56
57 2
            $response = curl_exec($curl);
58 2
            curl_close($curl);
59 2
            ob_clean();
60
61 2
            return $response;
62
    }
63
64 4
    public function getPrefixCSRF()
65
    {
66 4
        return $this->prefixCSRF;
67
    }
68
69
    /**
70
     * @return string
71
     */
72 6
    public function getCsrf()
73
    {
74 6
        if ( file_exists($this->pathToCookieFile) === false )
75 6
        {
76 5
            return '';
77
        }
78 1
        preg_match("/BPMCSRF\\s(.+)/", file_get_contents($this->pathToCookieFile), $matches);
79
80 1
        if ( isset($matches[1]) === false )
81 1
        {
82
            return '';
83
        }
84 1
        return $matches[1];
85
    }
86
}