Passed
Push — master ( fc0491...5c5f1e )
by Bartosz
04:11 queued 02:23
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\App\ResponseInterface;
20
use Magento\Framework\Exception\FileSystemException;
21
use Magento\Framework\Exception\NoSuchEntityException;
22
23
/**
24
 * Class Attachment
25
 * @package LizardMedia\ProductAttachment\Controller\Download
26
 */
27
class Attachment extends Action
28
{
29
    /**
30
     * @var AttachmentRepositoryInterface
31
     */
32
    private $attachmentRepository;
33
34
    /**
35
     * @var DownloadProcessor
36
     */
37
    private $downloadProcessor;
38
39
    /**
40
     * Attachment constructor.
41
     * @param AttachmentRepositoryInterface $attachmentRepository
42
     * @param DownloadProcessor $downloadProcessor
43
     * @param Context $context
44
     */
45
    public function __construct(
46
        AttachmentRepositoryInterface $attachmentRepository,
47
        DownloadProcessor $downloadProcessor,
48
        Context $context
49
    ) {
50
        parent::__construct($context);
51
        $this->attachmentRepository = $attachmentRepository;
52
        $this->downloadProcessor = $downloadProcessor;
53
    }
54
55
    /**
56
     * @return ResponseInterface
57
     */
58
    public function execute(): ResponseInterface
59
    {
60
        $attachmentId = (int) $this->getRequest()->getParam('id', 0);
61
        $attachment = $this->loadAttachmentById($attachmentId);
62
63
        if ($attachment instanceof AttachmentInterface) {
64
            try {
65
                $this->downloadProcessor->processDownload($attachment);
66
                return $this->_response;
67
            } 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...
68
                $this->messageManager->addErrorMessage(__('Sorry, there was an error getting requested content.'));
69
            }
70
        }
71
72
        $this->_redirect->redirect($this->_response, $this->_redirect->getRefererUrl());
73
        return $this->_response;
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