Passed
Push — master ( 662d52...2a8232 )
by eXeCUT
03:58
created

components/source/adapter/Site.php (1 issue)

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 2
    public function getFiles() {
37 2
        if ($this->authUrl) {
38
            $formParams = [
39 1
                $this->loginField => $this->login,
40 1
                $this->passwordField => $this->password,
41
            ];
42 1
            $formParams = array_merge($formParams, $this->otherFields);
43
44 1
            $this->doRequest($this->method, $this->authUrl, [
45 1
                'form_params' => $formParams,
46 1
                'headers' => self::STD_HEADERS,
47
            ]);
48
        }
49
50 2
        $cacheKey = __CLASS__ . $this->site;
51
//        if (!($body = \yii::$app->cache->get($cacheKey))) {
52 2
            $fileResponse = $this->doRequest('get', $this->fileUrl);
53 2
            $body = $fileResponse->getBody()->getContents();
54
//            \yii::$app->cache->set($cacheKey, $body);
55
//        }
56
57 2
        $fileNameParts = explode('/', $this->fileUrl);
58 2
        $fileName = $fileNameParts[count($fileNameParts) - 1];
59 2
        $file = $this->createFile($fileName);
60 2
        $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 2
            $file,
64
        ];
65
    }
66
67 2
    protected function doRequest($method, $url, $params = []) {
68 2
        return $this->getClient()->request($method, $url, $params);
69
    }
70
71
    protected function decode($content) {
72
        return iconv('cp1251', 'utf8', $content);
73
    }
74
75 2
    public function setClient($client) {
76 2
        $this->client = $client;
77
78 2
        return $this;
79
    }
80
81 2
    public function getClient() {
82 2
        if ($this->client === null) {
83
            $client = new Client([
84
                'base_uri' => $this->site,
85
                'cookie' => true,
86
                'cookies' => true,
87
            ]);
88
89
            $this->client = $client;
90
        }
91
92 2
        return $this->client;
93
    }
94
}