Completed
Push — master ( 55dbed...4e3396 )
by Piotr
12s
created

FormValidRequestHandler::getRedirectResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request;
11
12
use FSi\Bundle\AdminBundle\Admin\Context\Request\AbstractFormValidRequestHandler;
13
use FSi\Bundle\AdminBundle\Event\AdminEvent;
14
use FSi\Bundle\AdminBundle\Event\FormEvent;
15
use FSi\Bundle\AdminBundle\Event\FormEvents;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class FormValidRequestHandler extends AbstractFormValidRequestHandler
20
{
21
    /**
22
     * @param \FSi\Bundle\AdminBundle\Event\FormEvent $event
23
     * @param \Symfony\Component\HttpFoundation\Request $request
24
     */
25
    protected function action(FormEvent $event, Request $request)
26
    {
27
        $event->getElement()->save($event->getForm()->getData());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface FSi\Bundle\AdminBundle\Admin\Element as the method save() does only exist in the following implementations of said interface: FSi\Bundle\AdminBundle\A...UD\DependentCRUDElement, FSi\Bundle\AdminBundle\A...UD\DependentFormElement, FSi\Bundle\AdminBundle\A...CRUD\GenericCRUDElement, FSi\Bundle\AdminBundle\A...CRUD\GenericFormElement, FSi\Bundle\AdminBundle\A...\GenericResourceElement, FSi\Bundle\AdminBundle\Doctrine\Admin\CRUDElement, FSi\Bundle\AdminBundle\D...in\DependentCRUDElement, FSi\Bundle\AdminBundle\D...in\DependentFormElement, FSi\Bundle\AdminBundle\Doctrine\Admin\FormElement, FSi\Bundle\AdminBundle\D...e\Admin\ResourceElement, FSi\FixturesBundle\Admin\AboutUsPage, FSi\FixturesBundle\Admin\Category, FSi\FixturesBundle\Admin\CategoryNews, FSi\FixturesBundle\Admin\CustomNews, FSi\FixturesBundle\Admin\DTOFormElement, FSi\FixturesBundle\Admin\News, FSi\FixturesBundle\Admin\Person, FSi\FixturesBundle\Admin\Structure\HomePage, FSi\FixturesBundle\Admin\SubscriberForm, FSi\FixturesBundle\CustomAdmin\Contact.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    protected function getPreSaveEventName()
34
    {
35
        return FormEvents::FORM_DATA_PRE_SAVE;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    protected function getPostSaveEventName()
42
    {
43
        return FormEvents::FORM_DATA_POST_SAVE;
44
    }
45
46
    /**
47
     * @param \FSi\Bundle\AdminBundle\Event\AdminEvent $event
48
     * @param \Symfony\Component\HttpFoundation\Request $request
49
     * @return null|\Symfony\Component\HttpFoundation\Response
50
     */
51
    public function handleRequest(AdminEvent $event, Request $request)
52
    {
53
        $response = parent::handleRequest($event, $request);
54
        if ($response) {
55
            return $response;
56
        }
57
58
        $this->eventDispatcher->dispatch(FormEvents::FORM_RESPONSE_PRE_RENDER, $event);
59
        if ($event->hasResponse()) {
60
            return $event->getResponse();
61
        }
62
    }
63
64
    /**
65
     * @param \FSi\Bundle\AdminBundle\Event\FormEvent $event
66
     * @param \Symfony\Component\HttpFoundation\Request $request
67
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
68
     */
69
    protected function getRedirectResponse(FormEvent $event, Request $request)
70
    {
71
        if ($request->query->has('redirect_uri')) {
72
            return new RedirectResponse($request->query->get('redirect_uri'));
73
        }
74
75
        return parent::getRedirectResponse($event, $request);
76
    }
77
}
78