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

AttachmentHrefProcessor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A process() 0 12 6
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