Priloha   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Test Coverage

Coverage 3.8%

Importance

Changes 0
Metric Value
wmc 26
eloc 84
dl 0
loc 214
c 0
b 0
f 0
rs 10
ccs 3
cts 79
cp 0.038

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirstAttachment() 0 4 2
A addAttachmentFromFile() 0 4 1
A getAttachment() 0 12 3
A download() 0 22 3
A attachFile() 0 15 5
A saveToFile() 0 16 4
A addAttachment() 0 11 1
A getAttachmentsList() 0 16 5
A getDownloadUrl() 0 5 2
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
0 ignored issues
show
Bug introduced by
The type FlexiBeeRO was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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