Completed
Pull Request — master (#17)
by Christian
03:05
created

AttachmentsBody::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Client\Http;
13
14
use Xabbuh\XApi\Model\Attachment;
15
16
/**
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
class AttachmentsBody
20
{
21
    private $boundary;
22
    private $json;
23
    private $attachments;
24
25
    /**
26
     * @param string       $json
27
     * @param Attachment[] $attachments
28
     */
29
    public function __construct($json, array $attachments)
30
    {
31
        $this->boundary = uniqid();
32
        $this->json = $json;
33
        $this->attachments = $attachments;
34
    }
35
36
    public function getBoundary()
37
    {
38
        return $this->boundary;
39
    }
40
41
    public function build()
42
    {
43
        $body = '--'.$this->boundary."\r\n";
44
        $body .= "Content-Type: application/json\r\n";
45
        $body .= 'Content-Length: '.strlen($this->json)."\r\n";
46
        $body .= "\r\n";
47
        $body .= $this->json."\r\n";
48
49
        foreach ($this->attachments as $attachment) {
50
            $body .= '--'.$this->boundary."\r\n";
51
            $body .= 'Content-Type: '.$attachment->getContentType()."\r\n";
52
            $body .= "Content-Transfer-Encoding: binary\r\n";
53
            $body .= 'Content-Length: '.$attachment->getLength()."\r\n";
54
            $body .= 'X-Experience-API-Hash: '.$attachment->getSha2()."\r\n";
55
            $body .= "\r\n";
56
            $body .= $attachment->getContent()."\r\n";
0 ignored issues
show
Bug introduced by
The method getContent() does not exist on Xabbuh\XApi\Model\Attachment. Did you maybe mean getContentType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
57
        }
58
59
        $body .= '--'.$this->boundary.'--'."\r\n";
60
61
        return $body;
62
    }
63
}
64