CookieAuthentication::getCsrf()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
crap 3.0123
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
            if (ob_get_length()) {
60
                ob_clean();
61
            }
62
63 2
            return $response;
64
    }
65
66 4
    public function getPrefixCSRF()
67
    {
68 4
        return $this->prefixCSRF;
69
    }
70
71
    /**
72
     * @return string
73
     */
74 6
    public function getCsrf()
75
    {
76 6
        if ( file_exists($this->pathToCookieFile) === false )
77 6
        {
78 5
            return '';
79
        }
80 1
        preg_match("/BPMCSRF\\s(.+)/", file_get_contents($this->pathToCookieFile), $matches);
81
82 1
        if ( isset($matches[1]) === false )
83 1
        {
84
            return '';
85
        }
86 1
        return $matches[1];
87
    }
88
}
89