Completed
Pull Request — master (#374)
by Leny
28:15 queued 21:44
created

PageController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 127
rs 10
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A newAction() 0 4 1
A settingsAction() 0 4 1
A deleteAction() 0 4 1
A getNewPageType() 0 4 1
A getPageSettingsType() 0 4 1
A getBusinessPageType() 0 4 1
A getNewPage() 0 4 1
A getBaseTemplatePath() 0 4 1
A getRoutes() 0 4 1
1
<?php
2
3
namespace Victoire\Bundle\PageBundle\Controller;
4
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Victoire\Bundle\BusinessPageBundle\Form\BusinessPageType;
10
use Victoire\Bundle\PageBundle\Entity\BasePage;
11
use Victoire\Bundle\PageBundle\Entity\Page;
12
use Victoire\Bundle\PageBundle\Form\PageSettingsType;
13
use Victoire\Bundle\PageBundle\Form\PageType;
14
15
/**
16
 * @Route("/victoire-dcms/page")
17
 **/
18
class PageController extends BasePageController
19
{
20
    protected $routes;
21
22
    /**
23
     * Constructor.
24
     */
25
    public function __construct()
26
    {
27
        $this->routes = [
28
            'new'       => 'victoire_core_page_new',
29
            'show'      => 'victoire_core_page_show',
30
            'settings'  => 'victoire_core_page_settings',
31
            'detach'    => 'victoire_core_page_detach',
32
        ];
33
    }
34
35
    /**
36
     * New page.
37
     *
38
     * @param bool $isHomepage Is the page a homepage
39
     *
40
     * @Route("/new", name="victoire_core_page_new", defaults={"isHomepage" : false})
41
     * @Route("/homepage/new", name="victoire_core_homepage_new", defaults={"isHomepage" : true})
42
     * @Template()
43
     *
44
     * @return JsonResponse
45
     */
46
    public function newAction($isHomepage = false)
47
    {
48
        return new JsonResponse(parent::newAction($isHomepage));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...ewAction($isHomepage)); (Symfony\Component\HttpFoundation\JsonResponse) is incompatible with the return type of the parent method Victoire\Bundle\PageBund...geController::newAction of type array.

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...
49
    }
50
51
    /**
52
     * Page settings.
53
     *
54
     * @param Request  $request
55
     * @param BasePage $page
56
     *
57
     * @Route("/{id}/settings", name="victoire_core_page_settings")
58
     * @Template()
59
     * @ParamConverter("page", class="VictoirePageBundle:BasePage")
60
     *
61
     * @return JsonResponse The settings
62
     */
63
    public function settingsAction(Request $request, BasePage $page)
64
    {
65
        return new JsonResponse(parent::settingsAction($request, $page));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...tion($request, $page)); (Symfony\Component\HttpFoundation\JsonResponse) is incompatible with the return type of the parent method Victoire\Bundle\PageBund...troller::settingsAction of type array.

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...
66
    }
67
68
    /**
69
     * Page delete.
70
     *
71
     * @param BasePage $page
72
     *
73
     * @return JsonResponse
74
     * @Route("/{id}/delete", name="victoire_core_page_delete")
75
     * @Template()
76
     * @ParamConverter("page", class="VictoirePageBundle:BasePage")
77
     */
78
    public function deleteAction(BasePage $page)
79
    {
80
        return new JsonResponse(parent::deleteAction($page));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Symfony\Comp...::deleteAction($page)); (Symfony\Component\HttpFoundation\JsonResponse) is incompatible with the return type of the parent method Victoire\Bundle\PageBund...ontroller::deleteAction of type array<string,boolean|string>.

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...
81
    }
82
83
    /**
84
     * getNewPageType.
85
     *
86
     * @return string
87
     */
88
    protected function getNewPageType()
89
    {
90
        return PageType::class;
91
    }
92
93
    /**
94
     * getPageSettingsType.
95
     *
96
     * @return string
97
     */
98
    protected function getPageSettingsType()
99
    {
100
        return PageSettingsType::class;
101
    }
102
103
    /**
104
     * getBusinessPageType.
105
     *
106
     * @return string
107
     */
108
    protected function getBusinessPageType()
109
    {
110
        return BusinessPageType::class;
111
    }
112
113
    /**
114
     * getNewPage.
115
     *
116
     * @return \Victoire\Bundle\PageBundle\Entity\Page
117
     */
118
    protected function getNewPage()
119
    {
120
        return new Page();
121
    }
122
123
    /**
124
     * getBaseTemplatePath.
125
     *
126
     * @return string
127
     */
128
    protected function getBaseTemplatePath()
129
    {
130
        return 'VictoirePageBundle:Page';
131
    }
132
133
    /**
134
     * getRoutes.
135
     *
136
     * @param string $action
137
     *
138
     * @return string The route
139
     */
140
    protected function getRoutes($action)
141
    {
142
        return $this->routes[$action];
143
    }
144
}
145