Test Failed
Push — master ( 49d706...90fa53 )
by Vítězslav
03:42
created

Priloha   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 152
rs 10
c 1
b 0
f 0
wmc 15
lcom 0
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
B attachFile() 0 18 5
A getDownloadUrl() 0 4 1
A getFirstAttachment() 0 5 2
A download() 0 12 1
A saveToFile() 0 4 1
A addAttachmentFromFile() 0 4 1
A addAttachment() 0 10 1
A getAttachmentsList() 0 11 3
1
<?php
2
3
/**
4
 * FlexiPeeHP - Objekt kontaktu.
5
 *
6
 * @author     Vítězslav Dvořák <[email protected]>
7
 * @copyright  (C) 2015-2017 Spoje.Net
8
 */
9
10
namespace FlexiPeeHP;
11
12
/**
13
 * Příloha
14
 *
15
 * @link https://www.flexibee.eu/api/dokumentace/ref/attachments/
16
 * @link https://demo.flexibee.eu/c/demo/priloha/properties
17
 */
18
class Priloha extends FlexiBeeRW
19
{
20
21
    /**
22
     * Evidence užitá objektem.
23
     *
24
     * @var string
25
     */
26
    public $evidence = 'priloha';
27
28
    /**
29
     * Evidence s přílohami
30
     *
31
     * @var array
32
     */
33
    public static $relatedEvidence = [
34
      'prodejka' => 'doklFak', 'pohledavka' => 'doklFak', 'zavazek' => 'doklFak',
35
      'faktura-prijata' => 'doklFak', 'faktura-vydana' => 'doklFak',
36
      'interni-doklad' => 'doklInt', 'pokladni-pohyb' => 'doklInt', 'vzajemny-zapocet' => 'doklInt',
37
      'banka' => 'doklInt',
38
      'poptavka-vydana' => 'doklObch', 'poptavka-prijata' => 'doklObch', 'objednavka-prijata' => 'doklObch',
39
      'nabidka-vydana' => 'doklObch',
40
      'objednavka-vydana' => 'doklObch', 'nabidka-prijata' => 'doklObch',
41
      'skladovy-pohyb' => 'doklSklad',
42
      'cenik' => 'cenik',
43
      'adresar' => 'adresar', 'kontakt' => 'kontakt'
44
    ];
45
46
    /**
47
     * Přilož Soubor
48
     *
49
     * @param string $filepath
50
     * @param array  $attachmentData
51
     */
52
    public function attachFile($filepath, $attachmentData = [])
53
    {
54
        if (file_exists($filepath)) {
55
            $attachmentData['nazSoub'] = basename($filepath);
56
            $attachmentData['contentType'] = mime_content_type($filepath);
57
            $attachmentData['dataSize'] = filesize($filepath);
58
            $attachmentData['dataHash'] = md5_file($filepath);
59
60
            switch ($attachmentData['contentType']) {
61
                case 'image/png':
62
                case 'image/gif':
63
                case 'image/jpeg':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
64
65
                    break;
66
            }
67
            $attachmentData['content'] = base64_encode(file_get_contents($filepath));
68
        }
69
    }
70
71
    /**
72
     * Obtain url for Attachment Download
73
     *
74
     * @return string url
75
     */
76
    public static function getDownloadUrl($object)
77
    {
78
        return $object->apiURL . '/content';
79
    }
80
81
    /**
82
     * Obtain first attachment for given object
83
     *
84
     * @param  FlexiBeeRO $object
85
     * @return array
86
     */
87
    public static function getFirstAttachment($object)
88
    {
89
        $attachments = self::getAttachmentsList($object);
90
        return count($attachments) ? current($attachments) : null;
91
    }
92
93
    /**
94
     * Send "download" headers first and then file itself
95
     *
96
     * @param FlexiBeeRO $object
97
     * @param int|string $attachmentID
98
     */
99
    public static function download($object, $attachmentID = null)
0 ignored issues
show
Unused Code introduced by
The parameter $attachmentID is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        header('Content-Description: File Transfer');
102
        header('Content-Type: application/octet-stream');
103
        header('Content-Transfer-Encoding: binary');
104
        header('Expires: 0');
105
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
106
        header('Pragma: public');
107
        header('Content-Disposition: attachment; filename=' . $object->getName() . '_nrpe.sh');
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<FlexiPeeHP\FlexiBeeRO>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        header('Content-Length: ' . strlen($nrpesh));
0 ignored issues
show
Bug introduced by
The variable $nrpesh does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
109
        echo $object;
110
    }
111
112
    public static function saveToFile($object, $filename, $attachmentID = null)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $filename is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attachmentID is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
115
    }
116
117
    /**
118
     * Add Attachment from File
119
     *
120
     * @param FlexiBeeRW $object
121
     * @param string     $filename
122
     *
123
     * @return int      HTTP response code
124
     */
125
    public static function addAttachmentFromFile($object, $filename)
126
    {
127
        return self::addAttachment($object, basename($filename), file_get_contents($filename), mime_content_type($filename));
128
    }
129
130
    /**
131
     * Add Attachment related to current $object content
132
     *
133
     * @param FlexiBeeRW $object
134
     * @param string $filename
135
     * @param string $attachment Body
136
     * @param string $contentType Attachment Content-Type
137
     *
138
     * @return int HTTP Response code
139
     */
140
    public static function addAttachment($object, $filename, $attachment, $contentType)
141
    {
142
        $headersBackup = $object->defaultHttpHeaders;
143
        $object->postFields = $attachment;
144
        $object->defaultHttpHeaders['Content-Type'] = $contentType;
145
        $url = $object->getFlexiBeeURL() . '/prilohy/new/' . $filename;
146
        $response = $object->doCurlRequest($url, 'PUT');
0 ignored issues
show
Documentation introduced by
'PUT' is of type string, but the function expects a object<FlexiPeeHP\strinf>.

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...
147
        $object->defaultHttpHeaders = $headersBackup;
148
        return $response;
149
    }
150
151
    /**
152
     * Obtain Record related attachments list
153
     *
154
     * @param FlexiBeeRO $object
155
     * @return array
156
     */
157
    public static function getAttachmentsList($object)
158
    {
159
        $fburl = $object->getFlexiBeeURL();
160
        $attachments = $object->getFlexiData($fburl . '/prilohy');
161
        if (count($attachments)) {
162
            foreach ($attachments as $attachmentID => $attachmentData) {
163
                $attachments[$attachmentID]['url'] = $object->url . '/c/' . $object->company . '/priloha/' . $attachmentData['id'];
164
            }
165
        }
166
        return $attachments;
167
    }
168
169
}
170