Test Failed
Push — master ( bb67b0...f67d68 )
by Vítězslav
07:21
created

Priloha::addAttachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 4
dl 0
loc 12
rs 9.4285
c 0
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
    public static $relatedEvidence = [
26
        'prodejka' => 'doklFak', 'pohledavka' => 'doklFak', 'zavazek' => 'doklFak',
27
        'faktura-prijata' => 'doklFak', 'faktura-vydana' => 'doklFak',
28
        'interni-doklad' => 'doklInt', 'pokladni-pohyb' => 'doklInt', 'vzajemny-zapocet' => 'doklInt',
29
        'banka' => 'doklInt',
30
        'poptavka-vydana' => 'doklObch', 'poptavka-prijata' => 'doklObch', 'objednavka-prijata' => 'doklObch',
31
        'nabidka-vydana' => 'doklObch',
32
        'objednavka-vydana' => 'doklObch', 'nabidka-prijata' => 'doklObch',
33
        'skladovy-pohyb' => 'doklSklad',
34
        'cenik' => 'cenik',
35
        'adresar' => 'adresar', 'kontakt' => 'kontakt'
36
    ];
37
38
    public function attachFile($filepath, $attachmentData = [])
39
    {
40
        if (file_exists($filepath)) {
41
            $attachmentData['nazSoub']     = basename($filepath);
42
            $attachmentData['contentType'] = mime_content_type($filepath);
43
            $attachmentData['dataSize']    = filesize($filepath);
44
            $attachmentData['dataHash']    = md5_file($filepath);
45
46
            switch ($attachmentData['contentType']) {
47
                case 'image/png':
48
                case 'image/gif':
49
                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...
50
51
                    break;
52
            }
53
            $attachmentData['content'] = base64_encode(file_get_contents($filepath));
54
        }
55
    }
56
57
    /**
58
     * Obtain url for Attachment Download
59
     *
60
     * @return string url
61
     */
62
    public static function getDownloadUrl($object)
63
    {
64
        return $object->apiURL.'/content';
65
    }
66
67
    public function getFirsAttachment($object)
68
    {
69
        $attachments = self::getAttachmentsList($object);
70
        return count($attachments) ? current($attachments) : null;
71
    }
72
73
    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...
74
    {
75
        header('Content-Description: File Transfer');
76
        header('Content-Type: application/octet-stream');
77
        header('Content-Transfer-Encoding: binary');
78
        header('Expires: 0');
79
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
80
        header('Pragma: public');
81
        header('Content-Disposition: attachment; filename='.$this->host->getName().'_nrpe.sh');
0 ignored issues
show
Bug introduced by
The property host does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $this 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...
82
        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...
83
        echo $object;
84
    }
85
86
    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...
87
    {
88
89
    }
90
91
    /**
92
     * Add Attachment from File
93
     *
94
     * @param FlexiBeeRW $object
95
     * @param string     $filename
96
     *
97
     * @return int      HTTP response code
98
     */
99
    public static function addAttachmentFromFile($object, $filename)
100
    {
101
        return self::addAttachment($object, basename($filename),
102
                file_get_contents($filename), mime_content_type($filename));
103
    }
104
105
    /**
106
     * Add Attachment related to current $object content
107
     *
108
     * @param FlexiBeeRW $object
109
     * @param string $filename
110
     * @param string $attachment Body
111
     * @param string $contentType Attachment Content-Type
112
     *
113
     * @return int HTTP Response code
114
     */
115
    public static function addAttachment($object, $filename, $attachment,
116
                                         $contentType)
117
    {
118
        $headersBackup                              = $object->defaultHttpHeaders;
119
        $object->postFields                         = $attachment;
120
        $object->defaultHttpHeaders['Content-Type'] = $contentType;
121
        $url                                        = $object->getFlexiBeeURL().'/prilohy/new/'.$filename;
122
        $response                                   = $object->doCurlRequest($url,
123
            '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...
124
        $object->defaultHttpHeaders                 = $headersBackup;
125
        return $response;
126
    }
127
128
    /**
129
     * Obtain Record related attachments list
130
     *
131
     * @param FlexiBeeRO $object
132
     * @return array
133
     */
134
    public static function getAttachmentsList($object)
135
    {
136
        return $object->getFlexiData($object->getFlexiBeeURL().'/prilohy');
137
    }
138
}
139