Test Failed
Push — master ( 2514b0...1bd162 )
by Vítězslav
02:52
created

Priloha::getAttachment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 12
rs 9.4285
c 1
b 0
f 0
1
<?php
2
/**
3
 * FlexiPeeHP - Objekt kontaktu.
4
 *
5
 * @author     Vítězslav Dvořák <[email protected]>
6
 * @copyright  (C) 2015-2017 Spoje.Net
7
 */
8
9
namespace FlexiPeeHP;
10
11
/**
12
 * Příloha
13
 *
14
 * @link https://www.flexibee.eu/api/dokumentace/ref/attachments/
15
 * @link https://demo.flexibee.eu/c/demo/priloha/properties
16
 */
17
class Priloha extends FlexiBeeRW
18
{
19
    /**
20
     * Evidence užitá objektem.
21
     *
22
     * @var string
23
     */
24
    public $evidence = 'priloha';
25
26
    /**
27
     * Evidence s přílohami
28
     *
29
     * @var array
30
     */
31
    public static $relatedEvidence = [
32
        'prodejka' => 'doklFak', 'pohledavka' => 'doklFak', 'zavazek' => 'doklFak',
33
        'faktura-prijata' => 'doklFak', 'faktura-vydana' => 'doklFak',
34
        'interni-doklad' => 'doklInt', 'pokladni-pohyb' => 'doklInt', 'vzajemny-zapocet' => 'doklInt',
35
        'banka' => 'doklInt',
36
        'poptavka-vydana' => 'doklObch', 'poptavka-prijata' => 'doklObch', 'objednavka-prijata' => 'doklObch',
37
        'nabidka-vydana' => 'doklObch',
38
        'objednavka-vydana' => 'doklObch', 'nabidka-prijata' => 'doklObch',
39
        'skladovy-pohyb' => 'doklSklad',
40
        'cenik' => 'cenik',
41
        'adresar' => 'adresar', 'kontakt' => 'kontakt'
42
    ];
43
44
    /**
45
     * Attach file
46
     * Přilož Soubor
47
     *
48
     * @param string $filepath
49
     * @param array  $attachmentData
50
     */
51 1
    public function attachFile($filepath, $attachmentData = [])
52
    {
53 1
        if (file_exists($filepath)) {
54
            $attachmentData['nazSoub']     = basename($filepath);
55
            $attachmentData['contentType'] = mime_content_type($filepath);
56
            $attachmentData['dataSize']    = filesize($filepath);
57
            $attachmentData['dataHash']    = md5_file($filepath);
58
59
            switch ($attachmentData['contentType']) {
60
                case 'image/png':
61
                case 'image/gif':
62
                case 'image/jpeg':
63
                    break;
64
            }
65
            $attachmentData['content'] = base64_encode(file_get_contents($filepath));
66
        }
67 1
    }
68
69
    /**
70
     * Obtain url for Attachment Download
71
     *
72
     * @param \FlexiBeeRO $object Source object
73
     * @return string url
74
     */
75
    public static function getDownloadUrl($object)
76
    {
77
        $urlParts  = parse_url($object->apiURL);
78
        $pathParts = pathinfo($urlParts['path']);
79
        return $urlParts['scheme'].'://'.$urlParts['host'] .( array_key_exists('port',$urlParts) ? ':'.$urlParts['port'] : '') .$pathParts['dirname'].'/'.$pathParts['filename'].'/content';
80
    }
81
82
    /**
83
     * Obtain first attachment for given object
84
     *
85
     * @param  FlexiBeeRO $object
86
     * @return array
87
     */
88
    public static function getFirstAttachment($object)
89
    {
90
        $attachments = self::getAttachmentsList($object);
91
        return count($attachments) ? current($attachments) : null;
92
    }
93
94
    /**
95
     * Gives you attachment body as return value
96
     * 
97
     * @param int $attachmentID
98
     * 
99
     * @return string
100
     */
101
    public static function getAttachment($attachmentID)
102
    {
103
        $result     = null;
104
        $downloader = new Priloha($attachmentID);
105
        if ($downloader->lastResponseCode == 200) {
106
107
            $downloader->doCurlRequest(self::getDownloadURL($downloader), 'GET');
0 ignored issues
show
Documentation introduced by
$downloader is of type object<FlexiPeeHP\Priloha>, but the function expects a object<FlexiBeeRO>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
            if ($downloader->lastResponseCode == 200) {
109
                $result = $downloader->lastCurlResponse;
110
            }
111
        }
112
        return $result;
113
    }
114
115
    /**
116
     * Send "download" headers first and then file itself
117
     *
118
     * @param FlexiBeeRO $object
119
     * @param int|string $attachmentID
120
     */
121
    public static function download($object, $format = 'pdf',
122
                                    $attachmentID = null)
123
    {
124
        $attachments = self::getAttachmentsList($object);
125
126
        if (isset($attachmentID) && !array_key_exists($attachmentID,
127
                $attachments)) {
128
            $object->addStatusMessage(sprintf(_('Attagment %s does no exist'),
129
                    $attachmentID), 'warning');
130
        }
131
132
        $attachmentBody = $object->doCurlRequest(self::getDownloadUrl($object),
0 ignored issues
show
Documentation introduced by
$object is of type object<FlexiPeeHP\FlexiBeeRO>, but the function expects a object<FlexiBeeRO>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
            'GET');
134
        header('Content-Description: File Transfer');
135
        header('Content-Type: application/octet-stream');
136
        header('Content-Transfer-Encoding: binary');
137
        header('Expires: 0');
138
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
139
        header('Pragma: public');
140
        header('Content-Disposition: attachment; filename='.$object->getEvidence().'_'.$object.'.'.$format);
141
        header('Content-Length: '.strlen($attachmentBody));
142
        echo $attachmentBody;
143
    }
144
145
    /**
146
     * Save attachment to file
147
     *
148
     * @param int $attachmentID
149
     * @param string $destination directory or filename with path
150
     * @return int
151
     */
152
    public static function saveToFile($attachmentID, $destination)
153
    {
154
        $result     = 0;
155
        $downloader = new Priloha($attachmentID);
156
        if ($downloader->lastResponseCode == 200) {
157
158
            $downloader->doCurlRequest(self::getDownloadURL($downloader), 'GET');
0 ignored issues
show
Documentation introduced by
$downloader is of type object<FlexiPeeHP\Priloha>, but the function expects a object<FlexiBeeRO>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
            if ($downloader->lastResponseCode == 200) {
160
                if (is_dir($destination)) {
161
                    $destination .= '/'.$downloader->getDataValue('nazSoub');
162
                }
163
                $result = file_put_contents($destination,
164
                    $downloader->lastCurlResponse);
165
            }
166
        }
167
        return $result;
168
    }
169
170
    /**
171
     * Add Attachment from File
172
     *
173
     * @param FlexiBeeRW $object
174
     * @param string     $filename
175
     *
176
     * @return int      HTTP response code
177
     */
178
    public static function addAttachmentFromFile($object, $filename)
179
    {
180
        return self::addAttachment($object, basename($filename),
181
                file_get_contents($filename), mime_content_type($filename));
182
    }
183
184
    /**
185
     * Add Attachment related to current $object content
186
     *
187
     * @param FlexiBeeRW $object
188
     * @param string $filename
189
     * @param string $attachment Body
190
     * @param string $contentType Attachment Content-Type
191
     *
192
     * @return int HTTP Response code
193
     */
194
    public static function addAttachment($object, $filename, $attachment,
195
                                         $contentType)
196
    {
197
        $headersBackup                              = $object->defaultHttpHeaders;
198
        $object->postFields                         = $attachment;
199
        $object->defaultHttpHeaders['Content-Type'] = $contentType;
200
        $url                                        = $object->getFlexiBeeURL().'/prilohy/new/'.$filename;
201
        $response                                   = $object->doCurlRequest($url,
202
            'PUT');
203
        $object->defaultHttpHeaders                 = $headersBackup;
204
        return $response;
205
    }
206
207
    /**
208
     * Obtain Record related attachments list
209
     *
210
     * @param FlexiBeeRO $object
211
     * @return array
212
     */
213
    public static function getAttachmentsList($object)
214
    {
215
        $fburl       = $object->getFlexiBeeURL();
216
        $attachments = [];
217
        $atch        = $object->getFlexiData($fburl.'/prilohy'.(count($object->defaultUrlParams)
218
                ? '?'.http_build_query($object->defaultUrlParams) : ''));
219
        if (count($atch) && ($object->lastResponseCode == 200)) {
220
            foreach ($atch as $attachmentID => $attachmentData) {
221
                $attachments[$attachmentID]        = $attachmentData;
222
                $attachments[$attachmentID]['url'] = $object->url.'/c/'.$object->company.'/priloha/'.$attachmentData['id'];
223
            }
224
        }
225
        return $attachments;
226
    }
227
}
228