File::extractFileName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
crap 12
1
<?php
2
/**
3
 * ©[2016] SugarCRM Inc.  Licensed by SugarCRM under the Apache 2.0 license.
4
 */
5
6
namespace SugarAPI\SDK\Response;
7
8
use SugarAPI\SDK\Response\Abstracts\AbstractResponse;
9
10
class File extends AbstractResponse {
11
12
    /**
13
     * The name of the File from Response
14
     * @var string
15
     */
16
    protected $fileName;
17
18
    /**
19
     * File Path for response
20
     * @var string
21
     */
22
    protected $destinationPath;
23
24
    public function __construct($curlRequest, $curlResponse = NULL, $destination = NULL){
25
        parent::__construct($curlRequest,$curlResponse);
26
        $this->setDestinationPath($destination);
27
    }
28
29
    /**
30
     * @inheritdoc
31
     * Extract Filename from Headers
32
     * @param mixed $curlResponse
33
     */
34
    public function setCurlResponse($curlResponse) {
35
        parent::setCurlResponse($curlResponse);
36
        if (!$this->error) {
37
            if (empty($this->fileName)) {
38
                $this->extractFileName();
39
            }
40
            $this->writeFile();
41
        }
42
    }
43
44
45
    /**
46
     * Configure the Destination path to store the File response
47
     * @param null $destination
48
     * @return self
49
     */
50
    public function setDestinationPath($destination = NULL){
51
        if (empty($destination)){
52
            $destination = sys_get_temp_dir().'/SugarAPI';
53
        }
54
        $this->destinationPath = $destination;
55
        return $this;
56
    }
57
58
    /**
59
     * Extract the filename from the Headers, and store it in filename property
60
     */
61
    protected function extractFileName(){
62
        foreach (explode("\r\n", $this->headers) as $header)
63
        {
64
            if (strpos($header, 'filename')!==FALSE){
65
                $this->setFileName(substr($header, (strpos($header, "\"")+1), -1));
66
            }
67
        }
68
    }
69
70
    /**
71
     * Set the Filename for response to be saved to
72
     * @param $fileName
73
     * @return self
74
     */
75
    public function setFileName($fileName){
76
        $this->fileName = $fileName;
77
        return $this;
78
    }
79
80
    /**
81
     * Return the filename found in response
82
     * @return mixed
83
     */
84
    public function getFileName(){
85
        return $this->fileName;
86
    }
87
88
    /**
89
     * Write the downloaded file
90
     * @return string|boolean - False if not written
91
     */
92
    public function writeFile(){
93
        if (!empty($this->fileName)){
94
            if (!file_exists($this->destinationPath)){
95
                mkdir($this->destinationPath, 0777);
96
            }
97
            $file = $this->file();
98
            $fileHandle = fopen($file,'w+');
99
            fwrite($fileHandle,$this->body);
100
            fclose($fileHandle);
101
            return $file;
102
        }else{
103
            return FALSE;
104
        }
105
    }
106
107
    /**
108
     * Return the full File path, where Response was stored
109
     * @return string
110
     */
111
    public function file(){
112
        return rtrim($this->destinationPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$this->fileName;
113
    }
114
115
}