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

AttachmentsBody   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 45
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getBoundary() 0 4 1
A build() 0 22 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