Completed
Pull Request — master (#374)
by Leny
06:25
created

PageController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace Victoire\Bundle\PageBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Victoire\Bundle\BusinessPageBundle\Form\BusinessPageType;
11
use Victoire\Bundle\PageBundle\Entity\BasePage;
12
use Victoire\Bundle\PageBundle\Entity\Page;
13
use Victoire\Bundle\PageBundle\Form\PageSettingsType;
14
use Victoire\Bundle\PageBundle\Form\PageType;
15
16
/**
17
 * @Route("/victoire-dcms/page")
18
 **/
19
class PageController extends BasePageController
20
{
21
    protected $routes;
22
23
    /**
24
     * Constructor.
25
     */
26
    public function __construct()
27
    {
28
        $this->routes = [
29
            'new'       => 'victoire_core_page_new',
30
            'show'      => 'victoire_core_page_show',
31
            'settings'  => 'victoire_core_page_settings',
32
            'detach'    => 'victoire_core_page_detach',
33
        ];
34
    }
35
36
    /**
37
     * New page.
38
     *
39
     * @param bool $isHomepage Is the page a homepage
40
     *
41
     * @Route("/new", name="victoire_core_page_new", defaults={"isHomepage" : false})
42
     * @Route("/homepage/new", name="victoire_core_homepage_new", defaults={"isHomepage" : true})
43
     * @Template()
44
     *
45
     * @return JsonResponse
46
     */
47
    public function newAction($isHomepage = false)
48
    {
49
        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...
50
    }
51
52
    /**
53
     * Page settings.
54
     *
55
     * @param Request  $request
56
     * @param BasePage $page
57
     *
58
     * @Route("/{id}/settings", name="victoire_core_page_settings")
59
     * @Template()
60
     * @ParamConverter("page", class="VictoirePageBundle:BasePage")
61
     *
62
     * @return JsonResponse The settings
63
     */
64
    public function settingsAction(Request $request, BasePage $page)
65
    {
66
        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...
67
    }
68
69
    /**
70
     * Page delete.
71
     *
72
     * @param BasePage $page
73
     *
74
     * @return JsonResponse
75
     * @Route("/{id}/delete", name="victoire_core_page_delete")
76
     * @Template()
77
     * @ParamConverter("page", class="VictoirePageBundle:BasePage")
78
     */
79
    public function deleteAction(BasePage $page)
80
    {
81
        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...
82
    }
83
84
    /**
85
     * getNewPageType.
86
     *
87
     * @return string
88
     */
89
    protected function getNewPageType()
90
    {
91
        return PageType::class;
92
    }
93
94
    /**
95
     * getPageSettingsType.
96
     *
97
     * @return string
98
     */
99
    protected function getPageSettingsType()
100
    {
101
        return PageSettingsType::class;
102
    }
103
104
    /**
105
     * getBusinessPageType.
106
     *
107
     * @return string
108
     */
109
    protected function getBusinessPageType()
110
    {
111
        return BusinessPageType::class;
112
    }
113
114
    /**
115
     * getNewPage.
116
     *
117
     * @return \Victoire\Bundle\PageBundle\Entity\Page
118
     */
119
    protected function getNewPage()
120
    {
121
        return new Page();
122
    }
123
124
    /**
125
     * getBaseTemplatePath.
126
     *
127
     * @return string
128
     */
129
    protected function getBaseTemplatePath()
130
    {
131
        return 'VictoirePageBundle:Page';
132
    }
133
134
    /**
135
     * getRoutes.
136
     *
137
     * @param string $action
138
     *
139
     * @return string The route
140
     */
141
    protected function getRoutes($action)
142
    {
143
        return $this->routes[$action];
144
    }
145
}
146