Passed
Push — master ( 3691f4...d4a7c5 )
by Bartosz
47s queued 10s
created

DownloadResourceResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 59
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolveResourceType() 0 4 1
A resolveResource() 0 6 2
A buildFullFilePath() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * File: DownloadResourceResolver.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @copyright Copyright (C) 2020 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\ProductAttachment\Controller;
13
14
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface;
15
use Magento\Downloadable\Helper\Download as DownloadHelper;
16
use Magento\Downloadable\Helper\File as FileHelper;
17
use Magento\Framework\App\Filesystem\DirectoryList;
18
19
/**
20
 * Class DownloadResourceResolver
21
 * @package LizardMedia\ProductAttachment\Controller
22
 */
23
class DownloadResourceResolver
24
{
25
    /**
26
     * @var FileHelper
27
     */
28
    private $fileHelper;
29
30
    /**
31
     * @var DirectoryList
32
     */
33
    private $directoryList;
34
35
    /**
36
     * DownloadResourceResolver constructor.
37
     * @param FileHelper $fileHelper
38
     * @param DirectoryList $directoryList
39
     */
40
    public function __construct(FileHelper $fileHelper, DirectoryList $directoryList)
41
    {
42
        $this->fileHelper = $fileHelper;
43
        $this->directoryList = $directoryList;
44
    }
45
46
    /**
47
     * @param AttachmentInterface $attachment
48
     * @return string
49
     * @codeCoverageIgnore
50
     */
51
    public function resolveResourceType(AttachmentInterface $attachment): string
52
    {
53
        return $attachment->getAttachmentType();
54
    }
55
56
    /**
57
     * @param AttachmentInterface $attachment
58
     * @return string
59
     */
60
    public function resolveResource(AttachmentInterface $attachment): string
61
    {
62
        return $attachment->getAttachmentType() === DownloadHelper::LINK_TYPE_URL
63
            ? $attachment->getAttachmentUrl()
64
            : $this->buildFullFilePath($attachment);
65
    }
66
67
    /**
68
     * @param AttachmentInterface $attachment
69
     * @return string
70
     */
71
    private function buildFullFilePath(AttachmentInterface $attachment): string
72
    {
73
        return (string) $this->fileHelper->getFilePath(
74
            implode(
75
                DIRECTORY_SEPARATOR,
76
                [$this->directoryList->getPath(DirectoryList::MEDIA), $attachment->getBasePath()]
77
            ),
78
            $attachment->getAttachmentFile()
79
        );
80
    }
81
}