FolderLinkController::redirectToListTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
/*************************************************************************************/
13
14
namespace Dealer\Controller;
15
16
use Dealer\Controller\Base\BaseController;
17
use Dealer\Dealer;
18
use Dealer\Model\DealerFolder;
19
use Propel\Runtime\Propel;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Thelia\Core\Security\AccessManager;
22
use Thelia\Core\Security\Resource\AdminResources;
23
use Thelia\Tools\URL;
24
25
/**
26
 * Class FolderLinkController
27
 * @package Dealer\Controller
28
 */
29
class FolderLinkController extends BaseController
30
{
31
    const CONTROLLER_ENTITY_NAME = "dealer_folder_link";
32
33
    const CONTROLLER_CHECK_RESOURCE = Dealer::RESOURCES_ASSOCIATED;
34
    /**
35
     * @inheritDoc
36
     */
37
    protected function getListRenderTemplate()
38
    {
39
        $id = $this->getRequest()->query->get("dealer_id");
40
41
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit#associated",
42
            ["dealer_id" => $id, ]));
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    protected function redirectToListTemplate()
49
    {
50
        $id = $this->getRequest()->request->get("dealer_id");
51
52
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit#associated",
53
            ["dealer_id" => $id, ]));
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    protected function getEditRenderTemplate()
60
    {
61
        // TODO: Implement getEditRenderTemplate() method.
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    protected function getCreateRenderTemplate()
68
    {
69
        // TODO: Implement getCreateRenderTemplate() method.
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    protected function getObjectId($object)
76
    {
77
        /** @var DealerFolder $object */
78
        return $object->getId();
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84
    protected function getExistingObject()
85
    {
86
        // TODO: Implement getExistingObject() method.
87
    }
88
89
    /**
90
     * @inheritDoc
91
     */
92
    protected function hydrateObjectForm($object)
93
    {
94
        // TODO: Implement hydrateObjectForm() method.
95
    }
96
97
    /**
98
     * Method to get current controller associated service
99
     * @return object
100
     */
101
    protected function getService()
102
    {
103
        if (!$this->service) {
104
            $this->service = $this->getContainer()->get("dealer_folder_link_service");
105
        }
106
107
        return $this->service;
108
    }
109
110
    /**
111
     * Method to get Base Creation Form
112
     * @return \Thelia\Form\BaseForm
113
     */
114
    protected function getCreationForm()
115
    {
116
        return $this->createForm(static::CONTROLLER_ENTITY_NAME . "_create");
117
    }
118
119
    /**
120
     * Delete an object
121
     *
122
     * @return \Thelia\Core\HttpFoundation\Response the response
123
     */
124
    public function deleteAction()
125
    {
126
        // Check current user authorization
127
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, Dealer::getModuleCode(),
128
                AccessManager::DELETE)
129
        ) {
130
            return $response;
131
        }
132
133
        $con = Propel::getConnection();
134
        $con->beginTransaction();
135
        try {
136
            // Check token
137
            $this->getTokenProvider()->checkToken(
138
                $this->getRequest()->query->get("_token")
139
            );
140
            $data = [
141
                "folder_id" => $this->getRequest()->request->get(static::CONTROLLER_ENTITY_NAME . "_id"),
142
                "dealer_id" => $this->getRequest()->request->get("dealer_id"),
143
            ];
144
            $this->getService()->deleteFromArray($data);
145
            $con->commit();
146
147
            if ($this->getRequest()->request->get("success_url") == null) {
148
                return $this->redirectToListTemplate();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->redirectToListTemplate(); (Symfony\Component\HttpFoundation\RedirectResponse) is incompatible with the return type documented by Dealer\Controller\Folder...ontroller::deleteAction of type Thelia\Core\HttpFoundation\Response.

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...
149
            } else {
150
                return new RedirectResponse(URL::getInstance()->absoluteUrl($this->getRequest()->request->get("success_url")));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...->get('success_url'))); (Symfony\Component\HttpFoundation\RedirectResponse) is incompatible with the return type documented by Dealer\Controller\Folder...ontroller::deleteAction of type Thelia\Core\HttpFoundation\Response.

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...
151
            }
152
        } catch (\Exception $e) {
153
            $con->rollBack();
154
155
            return $this->renderAfterDeleteError($e);
156
        }
157
    }
158
}
159