Edit   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 92
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2
ccs 0
cts 41
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A _initAction() 0 6 1
A execute() 0 31 5
1
<?php
2
/**
3
 * File: Edit.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\ICatalogue\Controller\Adminhtml\Catalogue;
10
11
use Magento\Backend\App\Action\Context;
12
use Magento\Backend\Model\View\Result\Page;
13
use Magento\Backend\Model\View\Result\Page as ResultPage;
14
use Magento\Framework\Controller\ResultInterface;
15
use Magento\Framework\Registry;
16
use Magento\Framework\View\Result\PageFactory;
17
use MSlwk\ICatalogue\Model\Catalogue;
18
use MSlwk\ICatalogue\Model\CatalogueFactory;
19
use Magento\Backend\Model\View\Result\Redirect;
20
use MSlwk\ICatalogue\Model\ResourceModel\Catalogue as CatalogueResource;
21
22
/**
23
 * Class Edit
24
 *
25
 * @package MSlwk\ICatalogue\Controller\Adminhtml\Catalogue
26
 */
27
class Edit extends ActionAbstract
28
{
29
    /**
30
     * Core registry
31
     *
32
     * @var Registry
33
     */
34
    protected $coreRegistry = null;
35
36
    /**
37
     * @var PageFactory
38
     */
39
    protected $resultPageFactory;
40
41
    /**
42
     * @var CatalogueFactory
43
     */
44
    protected $catalogueFactory;
45
46
    /**
47
     * @var CatalogueResource
48
     */
49
    protected $catalogueResource;
50
51
    /**
52
     * Edit constructor.
53
     *
54
     * @param CatalogueFactory $catalogueFactory
55
     * @param CatalogueResource $catalogueResource
56
     * @param Context $context
57
     * @param PageFactory $resultPageFactory
58
     * @param Registry $registry
59
     */
60
    public function __construct(
61
        CatalogueFactory $catalogueFactory,
62
        CatalogueResource $catalogueResource,
63
        Context $context,
64
        PageFactory $resultPageFactory,
65
        Registry $registry
66
    ) {
67
        $this->catalogueFactory = $catalogueFactory;
68
        $this->catalogueResource = $catalogueResource;
69
        $this->resultPageFactory = $resultPageFactory;
70
        $this->coreRegistry = $registry;
71
        parent::__construct($context);
72
    }
73
74
    /**
75
     * @return Page
76
     */
77
    protected function _initAction()
78
    {
79
        /** @var Page $resultPage */
80
        $resultPage = $this->resultPageFactory->create();
81
        return $resultPage;
82
    }
83
84
    /**
85
     * @return ResultInterface
86
     */
87
    public function execute()
88
    {
89
        /** @var Catalogue $catalogue */
90
        $catalogue = $this->catalogueFactory->create();
91
92
        $id = $this->getRequest()->getParam('id');
93
        if ($id) {
94
            $this->catalogueResource->load($catalogue, $id);
95
            if (!$catalogue->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $catalogue->getId() of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
96
                $this->messageManager->addErrorMessage(__('The requested catalogue doesn\'t exist'));
97
                /** @var Redirect $resultRedirect */
98
                $resultRedirect = $this->resultRedirectFactory->create();
99
                return $resultRedirect->setPath('*/*/');
100
            }
101
        }
102
103
        $data = $this->_getSession()->getFormData(true);
104
        if (!empty($data)) {
105
            $catalogue->setData($data);
106
        }
107
108
        $this->coreRegistry->register('mslwk_icatalogue_catalogue', $catalogue);
109
110
        /** @var ResultPage $resultPage */
111
        $resultPage = $this->_initAction();
112
        $resultPage->getConfig()->getTitle()->prepend(__('Catalogues'));
113
        $resultPage->getConfig()->getTitle()
114
            ->prepend($catalogue->getId() ? $catalogue->getTitle() : __('New Catalogue'));
115
116
        return $resultPage;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $resultPage; (Magento\Backend\Model\View\Result\Page) is incompatible with the return type documented by MSlwk\ICatalogue\Control...Catalogue\Edit::execute of type Magento\Framework\Controller\ResultInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
117
    }
118
}
119