Test Failed
Push — master ( 03506f...60a799 )
by Vítězslav
02:45
created

Priloha::getAttachmentsList()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 5
eloc 10
nc 2
nop 1
dl 0
loc 14
rs 8.8571
c 3
b 1
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
    public function attachFile($filepath, $attachmentData = [])
52
    {
53
        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
    }
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'].$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
     * Send "download" headers first and then file itself
96
     *
97
     * @param FlexiBeeRO $object
98
     * @param int|string $attachmentID
99
     */
100
    public static function download($object, $format = 'pdf',
101
                                    $attachmentID = null)
102
    {
103
        $attachments = self::getAttachmentsList($object);
104
105
        if (isset($attachmentID) && !array_key_exists($attachmentID,
106
                $attachments)) {
107
            $object->addStatusMessage(sprintf(_('Attagment %s does no exist'),
108
                    $attachmentID), 'warning');
109
        }
110
111
        $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...
112
            'GET');
113
        header('Content-Description: File Transfer');
114
        header('Content-Type: application/octet-stream');
115
        header('Content-Transfer-Encoding: binary');
116
        header('Expires: 0');
117
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
118
        header('Pragma: public');
119
        header('Content-Disposition: attachment; filename='.$object->getEvidence().'_'.$object.'.'.$format);
120
        header('Content-Length: '.strlen($attachmentBody));
121
        echo $attachmentBody;
122
    }
123
124
    /**
125
     * Save attachment to file
126
     *
127
     * @param int $attachmentID
128
     * @param string $destination directory or filename with path
129
     * @return int
130
     */
131
    public static function saveToFile($attachmentID, $destination)
132
    {
133
        $result     = 0;
134
        $downloader = new Priloha($attachmentID);
135
        if ($downloader->lastResponseCode == 200) {
136
137
            $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...
138
            if ($downloader->lastResponseCode == 200) {
139
                if (is_dir($destination)) {
140
                    $destination .= '/'.$downloader->getDataValue('nazSoub');
141
                }
142
                $result = file_put_contents($destination,
143
                    $downloader->lastCurlResponse);
144
            }
145
        }
146
        return $result;
147
    }
148
149
    /**
150
     * Add Attachment from File
151
     *
152
     * @param FlexiBeeRW $object
153
     * @param string     $filename
154
     *
155
     * @return int      HTTP response code
156
     */
157
    public static function addAttachmentFromFile($object, $filename)
158
    {
159
        return self::addAttachment($object, basename($filename),
160
                file_get_contents($filename), mime_content_type($filename));
161
    }
162
163
    /**
164
     * Add Attachment related to current $object content
165
     *
166
     * @param FlexiBeeRW $object
167
     * @param string $filename
168
     * @param string $attachment Body
169
     * @param string $contentType Attachment Content-Type
170
     *
171
     * @return int HTTP Response code
172
     */
173
    public static function addAttachment($object, $filename, $attachment,
174
                                         $contentType)
175
    {
176
        $headersBackup                              = $object->defaultHttpHeaders;
177
        $object->postFields                         = $attachment;
178
        $object->defaultHttpHeaders['Content-Type'] = $contentType;
179
        $url                                        = $object->getFlexiBeeURL().'/prilohy/new/'.$filename;
180
        $response                                   = $object->doCurlRequest($url,
181
            'PUT');
182
        $object->defaultHttpHeaders                 = $headersBackup;
183
        return $response;
184
    }
185
186
    /**
187
     * Obtain Record related attachments list
188
     *
189
     * @param FlexiBeeRO $object
190
     * @return array
191
     */
192
    public static function getAttachmentsList($object)
193
    {
194
        $fburl       = $object->getFlexiBeeURL();
195
        $attachments = [];
196
        $atch        = $object->getFlexiData($fburl.'/prilohy'.(count($object->defaultUrlParams)
197
                ? '?'.http_build_query($object->defaultUrlParams) : ''));
198
        if (count($atch) && ($object->lastResponseCode == 200)) {
199
            foreach ($atch as $attachmentID => $attachmentData) {
200
                $attachments[$attachmentID]        = $attachmentData;
201
                $attachments[$attachmentID]['url'] = $object->url.'/c/'.$object->company.'/priloha/'.$attachmentData['id'];
202
            }
203
        }
204
        return $attachments;
205
    }
206
}
207