BasePageController::showByIdAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 3
1
<?php
2
3
namespace Victoire\Bundle\PageBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\Form\Form;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Victoire\Bundle\BlogBundle\Entity\Blog;
10
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
11
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
12
use Victoire\Bundle\CoreBundle\Controller\VictoireAlertifyControllerTrait;
13
use Victoire\Bundle\CoreBundle\Entity\EntityProxy;
14
use Victoire\Bundle\PageBundle\Entity\BasePage;
15
use Victoire\Bundle\PageBundle\Entity\Page;
16
17
/**
18
 * The BasePage controller is used to interact with all kind of pages.
19
 **/
20
class BasePageController extends Controller
21
{
22
    use VictoireAlertifyControllerTrait;
23
24
    /**
25
     * Find Page from url and render it.
26
     * Route for this action is defined in RouteLoader.
27
     *
28
     * @param Request $request
29
     * @param string  $url
30
     *
31
     * @return mixed
32
     */
33
    public function showAction(Request $request, $url = '')
34
    {
35
        $response = $this->get('victoire_page.page_helper')->renderPageByUrl(
36
            $url,
37
            $request->getLocale(),
38
            $request->isXmlHttpRequest() ? $request->query->get('modalLayout', null) : null
39
        );
40
41
        return $response;
42
    }
43
44
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$viewId" missing
Loading history...
45
     * Find url for a View id and optionally an Entity id and redirect to showAction.
46
     * Route for this action is defined in RouteLoader.
47
     *
48
     * @param Request $request
49
     * @param $viewId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
50
     * @param null $entityId
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
51
     *
52
     * @return RedirectResponse
53
     */
54
    public function showByIdAction(Request $request, $viewId, $entityId = null)
55
    {
56
        $parameters = [
57
            'viewId' => $viewId,
58
            'locale' => $request->getLocale(),
59
        ];
60
        if ($entityId) {
61
            $parameters['entityId'] = $entityId;
62
        }
63
        $page = $this->get('victoire_page.page_helper')->findPageByParameters($parameters);
64
65
        return $this->redirect($this->generateUrl('victoire_core_page_show', array_merge(
66
                ['url' => $page->getReference()->getUrl()],
67
                $request->query->all()
68
            )
69
        ));
70
    }
71
72
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entityId" missing
Loading history...
introduced by
Doc comment for parameter "$type" missing
Loading history...
73
     * Find BusinessPage url for an Entity id and type and redirect to showAction.
74
     * Route for this action is defined in RouteLoader.
75
     *
76
     * @param Request $request
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
77
     * @param $entityId
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
78
     * @param $type
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
79
     *
80
     * @return RedirectResponse
81
     */
82
    public function showBusinessPageByIdAction(Request $request, $entityId, $type)
83
    {
84
        $businessEntity = $this->get('victoire_core.entity.business_entity_repository')->findOneBy(['name' => $type]);
85
86
        $entityProxy = new EntityProxy();
87
        $entityProxy->setRessourceId($entityId);
88
        $entityProxy->setBusinessEntity($businessEntity);
89
        $entity = $this->get('victoire_business_entity.resolver.business_entity_resolver')->getBusinessEntity($entityProxy);
90
91
        $templateId = $this->get('victoire_business_page.business_page_helper')
92
            ->guessBestPatternIdForEntity($entity, $this->container->get('doctrine.orm.entity_manager'));
93
94
        $page = $this->get('victoire_page.page_helper')->findPageByParameters([
95
            'viewId'   => $templateId,
96
            'entityId' => $entityId,
97
            'locale'   => $request->getLocale(),
98
        ]);
99
100
        return $this->redirect(
101
            $this->generateUrl(
102
                'victoire_core_page_show',
103
                [
104
                    'url' => $page->getReference()->getUrl(),
105
                ]
106
            )
107
        );
108
    }
109
110
    /**
111
     * Display a form to create a new Blog or Page.
112
     * Route is defined in inherited controllers.
113
     *
114
     * @param Request $request
115
     * @param bool    $isHomepage
116
     *
117
     * @return array
118
     */
119
    protected function newAction(Request $request, $isHomepage = false)
120
    {
121
        $page = $this->getNewPage();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getNewPage() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
122
        if ($page instanceof Page) {
123
            $page->setHomepage($isHomepage ? $isHomepage : 0);
0 ignored issues
show
Bug introduced by
It seems like $isHomepage ? $isHomepage : 0 can also be of type integer; however, Victoire\Bundle\PageBund...iewTrait::setHomepage() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
124
        }
125
126
        $form = $this->get('form.factory')->create($this->getNewPageType(), $page);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getNewPageType() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
127
128
        return [
129
            'success' => true,
130
            'html'    => $this->get('templating')->render(
131
                $this->getBaseTemplatePath().':new.html.twig',
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getBaseTemplatePath() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
132
                ['form' => $form->createView()]
133
            ),
134
        ];
135
    }
136
137
    /**
138
     * Create a new Blog or Page.
139
     * Route is defined in inherited controllers.
140
     *
141
     * @param Request $request
142
     *
143
     * @return array
144
     */
145
    protected function newPostAction(Request $request)
146
    {
147
        $entityManager = $this->get('doctrine.orm.entity_manager');
148
        $page = $this->getNewPage();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getNewPage() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
149
150
        $form = $this->get('form.factory')->create($this->getNewPageType(), $page);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getNewPageType() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
151
        $form->handleRequest($request);
152
153
        if ($form->isValid()) {
154
            $page = $this->get('victoire_page.page_helper')->setPosition($page);
155
            $page->setAuthor($this->getUser());
156
            $entityManager->persist($page);
157
            $entityManager->flush();
158
159
            // If the $page is a BusinessEntity (eg. an Article), compute it's url
160
            if (null !== $this->get('victoire_core.helper.business_entity_helper')->findByEntityInstance($page)) {
161
                $page = $this
162
                    ->get('victoire_business_page.business_page_builder')
163
                    ->generateEntityPageFromTemplate($page->getTemplate(), $page, $entityManager);
164
            }
165
166
            $this->congrat($this->get('translator')->trans('victoire_page.create.success', [], 'victoire'));
167
168
            return $this->getViewReferenceRedirect($request, $page);
169
        }
170
171
        return [
172
            'success' => false,
173
            'message' => $this->get('victoire_form.error_helper')->getRecursiveReadableErrors($form),
174
            'html'    => $this->get('templating')->render(
175
                $this->getBaseTemplatePath().':new.html.twig',
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getBaseTemplatePath() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
176
                ['form' => $form->createView()]
177
            ),
178
        ];
179
    }
180
181
    /**
182
     * Display a form to edit Page settings.
183
     * Route is defined in inherited controllers.
184
     *
185
     * @param Request  $request
186
     * @param BasePage $page
187
     *
188
     * @return array
189
     */
190
    protected function settingsAction(Request $request, BasePage $page)
191
    {
192
        $form = $this->createSettingsForm($page);
193
194
        return  [
195
            'success' => true,
196
            'html'    => $this->get('templating')->render(
197
                $this->getBaseTemplatePath().':settings.html.twig',
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getBaseTemplatePath() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
198
                [
199
                    'page'               => $page,
200
                    'form'               => $form->createView(),
201
                    'businessProperties' => $this->getBusinessProperties($page),
202
                ]
203
            ),
204
        ];
205
    }
206
207
    /**
208
     * Save Page settings.
209
     * Route is defined in inherited controllers.
210
     *
211
     * @param Request  $request
212
     * @param BasePage $page
213
     *
214
     * @return array
215
     */
216
    protected function settingsPostAction(Request $request, BasePage $page)
217
    {
218
        $entityManager = $this->getDoctrine()->getManager();
219
        $form = $this->createSettingsForm($page);
220
221
        $form->handleRequest($request);
222
223
        if ($form->isValid()) {
224
            $entityManager->persist($page);
225
            $entityManager->flush();
226
227
            $this->congrat($this->get('translator')->trans('victoire_page.update.success', [], 'victoire'));
228
229
            return $this->getViewReferenceRedirect($request, $page);
230
        }
231
232
        return  [
233
            'success' => false,
234
            'message' => $this->get('victoire_form.error_helper')->getRecursiveReadableErrors($form),
235
            'html'    => $this->get('templating')->render(
236
                $this->getBaseTemplatePath().':settings.html.twig',
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getBaseTemplatePath() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
237
                [
238
                    'page'               => $page,
239
                    'form'               => $form->createView(),
240
                    'businessProperties' => $this->getBusinessProperties($page),
241
                ]
242
            ),
243
        ];
244
    }
245
246
    /**
247
     * Delete a Page.
248
     * Route is defined in inherited controllers.
249
     *
250
     * @param BasePage $page
251
     *
252
     * @return array
253
     */
254
    protected function deleteAction(BasePage $page)
255
    {
256
        try {
257
            //Throw Exception if Page is undeletable
258
            if ($page->isUndeletable()) {
259
                $message = $this->get('translator')->trans('page.undeletable', [], 'victoire');
260
                throw new \Exception($message);
261
            }
262
263
            $entityManager = $this->get('doctrine.orm.entity_manager');
264
            $entityManager->remove($page);
265
            $entityManager->flush();
266
267
            return [
268
                'success' => true,
269
                'url'     => $this->generateUrl('victoire_core_homepage_show'),
270
            ];
271
        } catch (\Exception $ex) {
272
            return [
273
                'success' => false,
274
                'message' => $ex->getMessage(),
275
            ];
276
        }
277
    }
278
279
    /**
280
     * Return an array for JsonResponse redirecting to a ViewReference.
281
     *
282
     * @param Request  $request
283
     * @param BasePage $page
284
     *
285
     * @return array
286
     */
287
    protected function getViewReferenceRedirect(Request $request, BasePage $page)
288
    {
289
        $parameters = [
290
            'viewId' => $page->getId(),
291
        ];
292
293
        if (!($page instanceof Blog)) {
294
            $parameters['locale'] = $request->getLocale();
295
        }
296
297
        $viewReference = $this->get('victoire_view_reference.repository')
298
            ->getOneReferenceByParameters($parameters);
299
300
        $page->setReference($viewReference);
301
302
        return [
303
            'success'  => true,
304
            'url'      => $this->generateUrl(
305
                'victoire_core_page_show',
306
                [
307
                    '_locale' => $request->getLocale(),
308
                    'url'     => $viewReference->getUrl(),
309
                ]
310
            ),
311
        ];
312
    }
313
314
    /**
315
     * Create Settings form according to Page type.
316
     *
317
     * @param BasePage $page
318
     *
319
     * @return Form
320
     */
321
    protected function createSettingsForm(BasePage $page)
322
    {
323
        $form = $this->createForm($this->getPageSettingsType(), $page);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getPageSettingsType() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\BlogBund...ntroller\BlogController, Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
324
325
        if ($page instanceof BusinessPage) {
326
            $form = $this->createForm($this->getBusinessPageType(), $page);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\PageBund...ller\BasePageController as the method getBusinessPageType() does only exist in the following sub-classes of Victoire\Bundle\PageBund...ller\BasePageController: Victoire\Bundle\PageBund...ntroller\PageController. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
327
        }
328
329
        return $form;
330
    }
331
332
    /**
333
     * Return BusinessEntity seaoable properties if Page is a BusinessTemplate.
334
     *
335
     * @param BasePage $page
336
     *
337
     * @return array
338
     */
339 View Code Duplication
    protected function getBusinessProperties(BasePage $page)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
340
    {
341
        $businessProperties = [];
342
343
        if ($page instanceof BusinessTemplate) {
344
            //we can use the business entity properties on the seo
345
            $businessEntity = $this->get('victoire_core.entity.business_entity_repository')->findOneBy(['name' => $page->getBusinessEntityName()]);
346
            $businessProperties = $businessEntity->getBusinessPropertiesByType('seoable');
347
        }
348
349
        return $businessProperties;
350
    }
351
}
352