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

Preview::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: Preview.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\Adminhtml\Attachment\File;
13
14
use LizardMedia\ProductAttachment\Api\AttachmentRepositoryInterface;
15
use LizardMedia\ProductAttachment\Api\Data\AttachmentInterface;
16
use LizardMedia\ProductAttachment\Controller\DownloadProcessor;
17
use LizardMedia\ProductAttachment\Model\Attachment;
18
use Magento\Backend\App\Action;
19
use Magento\Backend\App\Action\Context;
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 Preview
27
 * @package LizardMedia\ProductAttachment\Controller\Adminhtml\Attachment\File
28
 */
29
class Preview extends Action
30
{
31
    /**
32
     * @var AttachmentRepositoryInterface
33
     */
34
    private $attachmentRepository;
35
36
    /**
37
     * @var DownloadProcessor
38
     */
39
    private $downloadProcessor;
40
41
    /**
42
     * @param AttachmentRepositoryInterface $attachmentRepository
43
     * @param DownloadProcessor $downloadProcessor
44
     * @param Context $context
45
     */
46
    public function __construct(
47
        AttachmentRepositoryInterface $attachmentRepository,
48
        DownloadProcessor $downloadProcessor,
49
        Context $context
50
    ) {
51
        parent::__construct($context);
52
        $this->attachmentRepository = $attachmentRepository;
53
        $this->downloadProcessor = $downloadProcessor;
54
    }
55
56
    /**
57
     * @return ResultInterface
58
     */
59
    public function execute(): ResultInterface
60
    {
61
        $attachmentId = $this->getRequest()->getParam(Attachment::ID, 0);
62
        $attachment = $this->loadAttachmentById((int) $attachmentId);
63
        if ($attachment instanceof AttachmentInterface) {
64
            try {
65
                return $this->downloadProcessor->processDownload($attachment);
66
            } 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...
67
                $this->messageManager->addErrorMessage(__('Sorry, there was an error getting requested content.'));
68
                return $this->goBack();
69
            }
70
        } else {
71
            return $this->goBack();
72
        }
73
    }
74
75
    /**
76
     * @param int $id
77
     * @return AttachmentInterface | null
78
     */
79
    private function loadAttachmentById(int $id) : ?AttachmentInterface
80
    {
81
        try {
82
            return $this->attachmentRepository->getById($id);
83
        } 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...
84
            $this->messageManager->addErrorMessage(__('Attachment not found.'));
85
            return null;
86
        }
87
    }
88
89
    /**
90
     * @return Redirect
91
     */
92
    private function goBack(): Redirect
93
    {
94
        return $this->resultRedirectFactory->create()->setRefererUrl();
95
    }
96
}
97