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

MultipartStatementBody::getBoundary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
 * HTTP message body containing serialized statements and their attachments.
18
 *
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
class MultipartStatementBody
22
{
23
    private $boundary;
24
    private $serializedStatements;
25
    private $attachments;
26
27
    /**
28
     * @param string       $serializedStatements The JSON encoded statement(s)
29
     * @param Attachment[] $attachments          The statement attachments that include not only a file URL
30
     */
31
    public function __construct($serializedStatements, array $attachments)
32
    {
33
        $this->boundary = uniqid();
34
        $this->serializedStatements = $serializedStatements;
35
        $this->attachments = $attachments;
36
    }
37
38
    public function getBoundary()
39
    {
40
        return $this->boundary;
41
    }
42
43
    public function build()
44
    {
45
        $body = '--'.$this->boundary."\r\n";
46
        $body .= "Content-Type: application/json\r\n";
47
        $body .= 'Content-Length: '.strlen($this->serializedStatements)."\r\n";
48
        $body .= "\r\n";
49
        $body .= $this->serializedStatements."\r\n";
50
51
        foreach ($this->attachments as $attachment) {
52
            $body .= '--'.$this->boundary."\r\n";
53
            $body .= 'Content-Type: '.$attachment->getContentType()."\r\n";
54
            $body .= "Content-Transfer-Encoding: binary\r\n";
55
            $body .= 'Content-Length: '.$attachment->getLength()."\r\n";
56
            $body .= 'X-Experience-API-Hash: '.$attachment->getSha2()."\r\n";
57
            $body .= "\r\n";
58
            $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...
59
        }
60
61
        $body .= '--'.$this->boundary.'--'."\r\n";
62
63
        return $body;
64
    }
65
}
66