Site   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 47
c 1
b 0
f 1
dl 0
loc 78
ccs 18
cts 30
cp 0.6
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 2 1
A doRequest() 0 2 1
A setClient() 0 4 1
A getClient() 0 13 2
A getFiles() 0 28 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: execut
5
 * Date: 9/29/16
6
 * Time: 1:02 PM
7
 */
8
9
namespace execut\import\components\source\adapter;
10
11
12
use execut\import\components\source\Adapter;
13
use execut\import\components\source\File;
14
use GuzzleHttp\Client;
15
16
class Site extends Adapter
17
{
18
    public $site = null;
19
    public $authUrl = null;
20
    public $method = 'post';
21
    public $login = null;
22
    public $loginField = null;
23
    public $password = null;
24
    public $passwordField = null;
25
    public $fileUrl = null;
26
    public $otherFields = [];
27
    protected $client = null;
28
    const STD_HEADERS = [
29
        'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0',
30
        'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
31
        'Accept-Language' => 'en-US,en;q=0.5',
32
        'Accept-Encoding' => 'gzip, deflate',
33
        'Connection' => 'keep-alive',
34
        'Upgrade-Insecure-Requests' => '1'
35
    ];
36 1
    public function getFiles() {
37 1
        if ($this->authUrl) {
38
            $formParams = [
39
                $this->loginField => $this->login,
40
                $this->passwordField => $this->password,
41
            ];
42
            $formParams = array_merge($formParams, $this->otherFields);
43
44
            $this->doRequest($this->method, $this->authUrl, [
45
                'form_params' => $formParams,
46
                'headers' => self::STD_HEADERS,
47
            ]);
48
        }
49
50 1
        $cacheKey = __CLASS__ . $this->site;
0 ignored issues
show
Unused Code introduced by
The assignment to $cacheKey is dead and can be removed.
Loading history...
51
//        if (!($body = \yii::$app->cache->get($cacheKey))) {
52 1
            $fileResponse = $this->doRequest('get', $this->fileUrl);
53 1
            $body = $fileResponse->getBody()->getContents();
54
//            \yii::$app->cache->set($cacheKey, $body);
55
//        }
56
57 1
        $fileNameParts = explode('/', $this->fileUrl);
58 1
        $fileName = $fileNameParts[count($fileNameParts) - 1];
59 1
        $file = $this->createFile($fileName);
60 1
        $file->content = $body;
0 ignored issues
show
Bug Best Practice introduced by
The property $content is declared protected in execut\import\components\source\File. Since you implement __set, consider adding a @property or @property-write.
Loading history...
61
62
        return [
63 1
            $file,
64
        ];
65
    }
66
67 1
    protected function doRequest($method, $url, $params = []) {
68 1
        return $this->getClient()->request($method, $url, $params);
69
    }
70
71
    protected function decode($content) {
72
        return iconv('cp1251', 'utf8', $content);
73
    }
74
75 1
    public function setClient($client) {
76 1
        $this->client = $client;
77
78 1
        return $this;
79
    }
80
81 1
    public function getClient() {
82 1
        if ($this->client === null) {
83
            $client = new Client([
84
                'base_uri' => $this->site,
85
                'cookie' => true,
86
                'cookies' => true,
87
                'timeout' => 180,
88
            ]);
89
90
            $this->client = $client;
91
        }
92
93 1
        return $this->client;
94
    }
95
}