Completed
Push — master ( 4d4a30...6bef1f )
by Antony
03:37
created

FolderLinkController::getEditRenderTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 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
     * @inheritDoc
34
     */
35
    protected function getListRenderTemplate()
36
    {
37
        $id = $this->getRequest()->query->get("dealer_id");
38
39
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit#associated",
40
            ["dealer_id" => $id,]));
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    protected function redirectToListTemplate()
47
    {
48
        $id = $this->getRequest()->request->get("dealer_id");
49
50
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit#associated",
51
            ["dealer_id" => $id,]));
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    protected function getEditRenderTemplate()
58
    {
59
        // TODO: Implement getEditRenderTemplate() method.
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    protected function getCreateRenderTemplate()
66
    {
67
        // TODO: Implement getCreateRenderTemplate() method.
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    protected function getObjectId($object)
74
    {
75
        /** @var DealerFolder $object */
76
        return $object->getId();
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    protected function getExistingObject()
83
    {
84
        // TODO: Implement getExistingObject() method.
85
    }
86
87
    /**
88
     * @inheritDoc
89
     */
90
    protected function hydrateObjectForm($object)
91
    {
92
        // TODO: Implement hydrateObjectForm() method.
93
    }
94
95
    /**
96
     * Method to get current controller associated service
97
     * @return object
98
     */
99
    protected function getService()
100
    {
101
        if (!$this->service) {
102
            $this->service = $this->getContainer()->get("dealer_folder_link_service");
103
        }
104
105
        return $this->service;
106
    }
107
108
    /**
109
     * Method to get Base Creation Form
110
     * @return \Thelia\Form\BaseForm
111
     */
112
    protected function getCreationForm()
113
    {
114
        return $this->createForm(static::CONTROLLER_ENTITY_NAME . "_create");
115
    }
116
117
    /**
118
     * Delete an object
119
     *
120
     * @return \Thelia\Core\HttpFoundation\Response the response
121
     */
122
    public function deleteAction()
123
    {
124
        // Check current user authorization
125
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, Dealer::getModuleCode(),
126
                AccessManager::DELETE)
127
        ) {
128
            return $response;
129
        }
130
131
        $con = Propel::getConnection();
132
        $con->beginTransaction();
133
        try {
134
            // Check token
135
            $this->getTokenProvider()->checkToken(
136
                $this->getRequest()->query->get("_token")
137
            );
138
            $data = [
139
                "folder_id" => $this->getRequest()->request->get(static::CONTROLLER_ENTITY_NAME . "_id"),
140
                "dealer_id" => $this->getRequest()->request->get("dealer_id"),
141
            ];
142
            $this->getService()->deleteFromArray($data);
143
            $con->commit();
144
145
            if ($this->getRequest()->request->get("success_url") == null) {
146
                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...
147
            } else {
148
                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...
149
            }
150
        } catch (\Exception $e) {
151
            $con->rollBack();
152
153
            return $this->renderAfterDeleteError($e);
154
        }
155
    }
156
}