GeoDealerController::getService()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
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\Dealer;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Thelia\Controller\Admin\BaseAdminController;
19
use Thelia\Core\Security\AccessManager;
20
use Thelia\Core\Security\Resource\AdminResources;
21
use Thelia\Form\Exception\FormValidationException;
22
use Thelia\Tools\URL;
23
24
/**
25
 * Class GeoDealerController
26
 * @package Dealer\Controller
27
 */
28
class GeoDealerController extends BaseAdminController
29
{
30
    protected $service;
31
32
    /**
33
     * Save changes on a modified object, and either go back to the object list, or stay on the edition page.
34
     *
35
     * @return \Thelia\Core\HttpFoundation\Response the response
36
     */
37
    public function processUpdateAction()
38
    {
39
        // Check current user authorization
40
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, Dealer::getModuleCode(),
41
                AccessManager::VIEW)
42
        ) {
43
            return $response;
44
        }
45
46
        // Error (Default: false)
47
        $error_msg = false;
0 ignored issues
show
Unused Code introduced by
$error_msg is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
49
        // Create the Form from the request
50
        $changeForm = $this->getForm($this->getRequest());
51
52
        try {
53
            // Check the form against constraints violations
54
            $form = $this->validateForm($changeForm, "POST");
55
56
            // Get the form field values
57
            $data = $form->getData();
58
59
            $updatedObject = $this->getService()->updateFromArray($data);
60
61
            // Check if object exist
62
            if (!$updatedObject) {
63
                throw new \LogicException(
64
                    $this->getTranslator()->trans("No %obj was updated.", ['%obj' => 'Dealer'])
65
                );
66
            }
67
            // If we have to stay on the same page, do not redirect to the successUrl,
68
            // just redirect to the edit page again.
69
            if ($this->getRequest()->get('save_mode') == 'stay') {
70
                $id = $this->getRequest()->query->get("dealer_id");
71
72
                return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit",
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...('dealer_id' => $id))); (Symfony\Component\HttpFoundation\RedirectResponse) is incompatible with the return type documented by Dealer\Controller\GeoDea...er::processUpdateAction 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...
73
                    ["dealer_id" => $id, ]));
74
            }
75
76
            // Redirect to the success URL
77
            return $this->generateSuccessRedirect($changeForm);
78
        } catch (FormValidationException $ex) {
0 ignored issues
show
Bug introduced by
The class Thelia\Form\Exception\FormValidationException 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...
79
            // Form cannot be validated
80
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
81
            /*} catch (\Exception $ex) {
82
                // Any other error
83
                $error_msg = $ex->getMessage();*/
84
        }
85
86
        if (false !== $error_msg) {
87
            // At this point, the form has errors, and should be redisplayed.
88
            $this->setupFormErrorContext(
89
                $this->getTranslator()->trans("%obj modification", ['%obj' => 'Dealer']),
90
                $error_msg,
91
                $changeForm,
92
                $ex
93
            );
94
95
            $id = $this->getRequest()->query->get("dealer_id");
96
97
            return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit",
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...('dealer_id' => $id))); (Symfony\Component\HttpFoundation\RedirectResponse) is incompatible with the return type documented by Dealer\Controller\GeoDea...er::processUpdateAction 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...
98
                ["dealer_id" => $id, ]));
99
        }
100
    }
101
102
    protected function getForm($data = null)
103
    {
104
        if (!is_array($data)) {
105
            $data = [];
106
        }
107
108
        return $this->createForm("dealer-geo", "form", $data);
109
    }
110
111
    protected function getService()
112
    {
113
        if (!$this->service) {
114
            $this->service = $this->getContainer()->get("dealer_geo_service");
115
        }
116
117
        return $this->service;
118
    }
119
}
120