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

Attachment::goBack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * File: Attachment.php
7
 *
8
 * @author Bartosz Kubicki [email protected]>
9
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
10
 */
11
12
namespace LizardMedia\ProductAttachment\Controller\Download;
13
14
use LizardMedia\ProductAttachment\Api\AttachmentRepositoryInterface;
15
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface;
16
use LizardMedia\ProductAttachment\Controller\DownloadProcessor;
17
use Magento\Framework\App\Action\Action;
18
use Magento\Framework\App\Action\Context;
19
use Magento\Framework\Controller\Result\RawFactory;
20
use Magento\Framework\Controller\Result\Redirect;
21
use Magento\Framework\Controller\ResultInterface;
22
use Magento\Framework\Exception\FileSystemException;
23
use Magento\Framework\Exception\NoSuchEntityException;
24
25
/**
26
 * Class Attachment
27
 * @package LizardMedia\ProductAttachment\Controller\Download
28
 */
29
class Attachment extends Action
30
{
31
    /**
32
     * @var AttachmentRepositoryInterface
33
     */
34
    private $attachmentRepository;
35
36
    /**
37
     * @var DownloadProcessor
38
     */
39
    private $downloadProcessor;
40
41
    /**
42
     * Attachment constructor.
43
     * @param AttachmentRepositoryInterface $attachmentRepository
44
     * @param DownloadProcessor $downloadProcessor
45
     * @param Context $context
46
     */
47
    public function __construct(
48
        AttachmentRepositoryInterface $attachmentRepository,
49
        DownloadProcessor $downloadProcessor,
50
        Context $context
51
    ) {
52
        parent::__construct($context);
53
        $this->attachmentRepository = $attachmentRepository;
54
        $this->downloadProcessor = $downloadProcessor;
55
    }
56
57
    /**
58
     * @return ResultInterface
59
     */
60
    public function execute(): ResultInterface
61
    {
62
        $attachmentId = (int) $this->getRequest()->getParam('id', 0);
63
        $attachment = $this->loadAttachmentById($attachmentId);
64
65
        if ($attachment instanceof AttachmentInterface) {
66
            try {
67
                return $this->downloadProcessor->processDownload($attachment);
68
            } catch (FileSystemException $exception) {
0 ignored issues
show
Bug introduced by
The class Magento\Framework\Exception\FileSystemException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
69
                $this->messageManager->addErrorMessage(__('Sorry, there was an error getting requested content.'));
70
            }
71
        }
72
73
        return $this->goBack();
74
    }
75
76
    /**
77
     * @param int $id
78
     * @return AttachmentInterface|null
79
     */
80
    private function loadAttachmentById(int $id): ?AttachmentInterface
81
    {
82
        try {
83
            return $this->attachmentRepository->getById($id);
84
        } catch (NoSuchEntityException $e) {
0 ignored issues
show
Bug introduced by
The class Magento\Framework\Exception\NoSuchEntityException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
85
            $this->messageManager->addErrorMessage(__('Sorry, there was an error getting requested content.'));
86
            return null;
87
        }
88
    }
89
90
    /**
91
     * @return Redirect
92
     */
93
    private function goBack(): Redirect
94
    {
95
        return $this->resultRedirectFactory->create()->setRefererUrl();
96
    }
97
}
98