Completed
Push — master ( 2055e6...14d075 )
by Paweł
17s queued 10s
created

AttachmentHrefProcessor::process()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 3
nop 2
dl 0
loc 12
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Serializer\Processor;
6
7
use function array_key_exists;
8
use function is_array;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
11
12
final class AttachmentHrefProcessor
13
{
14
    private $uploaderHelper;
15
    private $requestStack;
16
17
    public function __construct(
18
        UploaderHelper $uploaderHelper,
19
        RequestStack $requestStack
20
    ) {
21
        $this->uploaderHelper = $uploaderHelper;
22
        $this->requestStack = $requestStack;
23
    }
24
25
    public function process($object, array $data): array
26
    {
27
        if (is_array($data) && array_key_exists('file_name', $data)) {
28
            $filePath = $this->uploaderHelper->asset($object, 'file');
29
            $currentRequest = $this->requestStack->getCurrentRequest();
30
            if (null !== $filePath && null !== $currentRequest && false === strpos($filePath, '://')) {
31
                $filePath = $currentRequest->getSchemeAndHttpHost().$filePath;
32
            }
33
            $data['href']['download'] = $filePath;
34
        }
35
36
        return $data;
37
    }
38
}
39