Passed
Push — master ( 95b92f...37aede )
by Yannick
10:09 queued 01:17
created

AttachmentResponse   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 45
c 1
b 0
f 0
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sendContent() 0 3 1
A prepare() 0 8 2
A getContent() 0 3 1
A setContent() 0 4 2
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the xAPI package.
7
 *
8
 * (c) Christian Flothmann <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace XApi\LrsBundle\Response;
15
16
use LogicException;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Xabbuh\XApi\Model\Attachment;
20
21
/**
22
 * @author Jérôme Parmentier <[email protected]>
23
 */
24
class AttachmentResponse extends Response
25
{
26
    protected $attachment;
27
28
    public function __construct(Attachment $attachment)
29
    {
30
        parent::__construct(null);
31
32
        $this->attachment = $attachment;
33
    }
34
35
    public function prepare(Request $request): void
36
    {
37
        if (!$this->headers->has('Content-Type')) {
38
            $this->headers->set('Content-Type', $this->attachment->getContentType());
39
        }
40
41
        $this->headers->set('Content-Transfer-Encoding', 'binary');
42
        $this->headers->set('X-Experience-API-Hash', $this->attachment->getSha2());
43
    }
44
45
    /**
46
     * @throws LogicException
47
     */
48
    public function sendContent(): void
49
    {
50
        throw new LogicException('An AttachmentResponse is only meant to be part of a multipart Response.');
51
    }
52
53
    /**
54
     * @throws LogicException when the content is not null
55
     */
56
    public function setContent($content): void
57
    {
58
        if (null !== $content) {
59
            throw new LogicException('The content cannot be set on an AttachmentResponse instance.');
60
        }
61
    }
62
63
    /**
64
     * @return string|null
65
     */
66
    public function getContent()
67
    {
68
        return $this->attachment->getContent();
69
    }
70
}
71