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

CookieAuthentication   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 76
ccs 38
cts 39
cp 0.9744
rs 10
c 1
b 0
f 0
wmc 8
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 4 1
A getPathCookieFile() 0 4 1
B auth() 0 28 1
A getPrefixCSRF() 0 4 1
A setPathCookieFile() 0 4 1
A getCsrf() 0 14 3
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
}