DownloadProcessor::readFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * File: DownloadProcessor.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 Exception;
15
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface;
16
use Magento\Framework\App\Filesystem\DirectoryList;
17
use Magento\Framework\App\Response\Http\FileFactory;
18
use Magento\Framework\Exception\FileSystemException;
19
use Magento\Framework\Filesystem\Io\File;
20
21
/**
22
 * Class DownloadProcessor
23
 * @package LizardMedia\ProductAttachment\Controller
24
 */
25
class DownloadProcessor
26
{
27
    /**
28
     * @var DownloadResourceResolver
29
     */
30
    private $downloadResourceResolver;
31
32
    /**
33
     * @var FileFactory
34
     */
35
    private $fileFactory;
36
37
    /**
38
     * @var File
39
     */
40
    private $file;
41
42
    /**
43
     * DownloadProcessor constructor.
44
     * @param DownloadResourceResolver $downloadResourceResolver
45
     * @param FileFactory $fileFactory
46
     * @param File $file
47
     */
48
    public function __construct(
49
        DownloadResourceResolver $downloadResourceResolver,
50
        FileFactory $fileFactory,
51
        File $file
52
    ) {
53
        $this->downloadResourceResolver = $downloadResourceResolver;
54
        $this->fileFactory = $fileFactory;
55
        $this->file = $file;
56
    }
57
58
    /**
59
     * @param AttachmentInterface $attachment
60
     * @return void
61
     * @throws FileSystemException
62
     */
63
    public function processDownload(AttachmentInterface $attachment): void
64
    {
65
        try {
66
            $name = basename($this->downloadResourceResolver->resolveResource($attachment));
67
            $this->fileFactory->create(
68
                $name,
69
                [
70
                    'type' => 'string',
71
                    'value' => $this->readFile($attachment),
72
                    'rm' => true
73
                ],
74
                DirectoryList::TMP
75
            );
76
        } catch (Exception $exception) {
77
            throw new FileSystemException(__('File could not be downloaded'));
78
        }
79
    }
80
81
    /**
82
     * @param AttachmentInterface $attachment
83
     * @return string
84
     * @throws FileSystemException
85
     */
86
    private function readFile(AttachmentInterface $attachment): string
87
    {
88
        $fileContent = $this->file->read($this->downloadResourceResolver->resolveResource($attachment));
89
        if ($fileContent === false ) {
90
            throw new FileSystemException(__('File could not be read'));
91
        }
92
93
        return $fileContent;
94
    }
95
}
96