Passed
Branch 2.0.0 (f59c7c)
by Chubarov
02:44
created

CookieAuthentication::setPathCookieFile()   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 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 0
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
    /**
27
     * Getting the cookie and write it to a file
28
     * @return boolean
29
     */
30 2
    public function auth()
31
    {
32 2
            $curl    = curl_init();
33
            $headers = array(
34 2
                "POST  HTTP/1.0",
35
                "Content-type: application/json"
36 2
            );
37
38 2
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
39 2
            curl_setopt($curl, CURLOPT_HEADER, 1);
40 2
            curl_setopt($curl, CURLOPT_URL,  $this->configuration['UrlLogin']);
41 2
            curl_setopt($curl, CURLOPT_POST, true);
42 2
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
43 2
            curl_setopt($curl, CURLOPT_COOKIEJAR, $this->pathToCookieFile);
44
45 2
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array(
46 2
                'UserName'       => $this->configuration['Login'],
47 2
                'UserPassword'   => $this->configuration['Password'],
48 2
                'SolutionName'   => 'TSBpm',
49 2
                'TimeZoneOffset' => '-120',
50 2
                'Language'       => 'Ru-ru')));
51
52 2
            $response = curl_exec($curl);
53 2
            curl_close($curl);
54 2
            ob_clean();
55
56 2
            return $response;
57
    }
58
59 4
    public function getPrefixCSRF()
60
    {
61 4
        return $this->prefixCSRF;
62
    }
63
64 1
    public function setPathCookieFile($path)
65
    {
66 1
        $this->pathToCookieFile = $path;
67 1
    }
68
    /**
69
     * @return string
70
     */
71 5
    public function getCsrf()
72
    {
73 5
        if ( file_exists($this->pathToCookieFile) === false )
74 5
        {
75 5
            return '';
76
        }
77 1
        preg_match("/BPMCSRF\\s(.+)/", file_get_contents($this->pathToCookieFile), $matches);
78
79 1
        if ( isset($matches[1]) === false )
80 1
        {
81
            return '';
82
        }
83 1
        return $matches[1];
84
    }
85
}