Completed
Pull Request — master (#259)
by Piotr
02:38
created

FormValidRequestHandler::isValidPostRequest()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
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 LogicException;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
20
class FormValidRequestHandler extends AbstractFormValidRequestHandler
21
{
22
    /**
23
     * @param \FSi\Bundle\AdminBundle\Event\FormEvent $event
24
     * @param \Symfony\Component\HttpFoundation\Request $request
25
     */
26
    protected function action(FormEvent $event, Request $request)
27
    {
28
        $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\Admin\CRUD\AbstractCRUD, FSi\Bundle\AdminBundle\A...CRUD\GenericFormElement, FSi\Bundle\AdminBundle\A...sitory\AbstractResource, FSi\Bundle\AdminBundle\A...\GenericResourceElement, FSi\Bundle\AdminBundle\Doctrine\Admin\CRUDElement, FSi\Bundle\AdminBundle\Doctrine\Admin\FormElement, FSi\Bundle\AdminBundle\D...e\Admin\ResourceElement, FSi\FixturesBundle\Admin\AboutUsPage, 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...
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    protected function getPreSaveEventName()
35
    {
36
        return FormEvents::FORM_DATA_PRE_SAVE;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    protected function getPostSaveEventName()
43
    {
44
        return FormEvents::FORM_DATA_POST_SAVE;
45
    }
46
47
    /**
48
     * @param \FSi\Bundle\AdminBundle\Event\AdminEvent $event
49
     * @param \Symfony\Component\HttpFoundation\Request $request
50
     * @return null|\Symfony\Component\HttpFoundation\Response
51
     */
52
    public function handleRequest(AdminEvent $event, Request $request)
53
    {
54
        $response = parent::handleRequest($event, $request);
55
        if ($response) {
56
            return $response;
57
        }
58
59
        $this->eventDispatcher->dispatch(FormEvents::FORM_RESPONSE_PRE_RENDER, $event);
60
        if ($event->hasResponse()) {
61
            return $event->getResponse();
62
        }
63
    }
64
65
    /**
66
     * @param \FSi\Bundle\AdminBundle\Event\FormEvent $event
67
     * @param \Symfony\Component\HttpFoundation\Request $request
68
     * @return bool
69
     */
70
    protected function isValidPostRequest(FormEvent $event, Request $request)
71
    {
72
        $element = $event->getElement();
73
        if ($element->hasOption('allow_add')
74
            && !$element->getOption('allow_add')
75
            && !$request->get('id')
76
        ) {
77
            throw new LogicException(sprintf(
78
                'Tried to add new object through element "%s", which has option "allow_add" set to false',
79
                get_class($element)
80
            ));
81
        }
82
83
        return parent::isValidPostRequest($event, $request);
84
    }
85
86
    /**
87
     * @param \FSi\Bundle\AdminBundle\Event\FormEvent $event
88
     * @param \Symfony\Component\HttpFoundation\Request $request
89
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
90
     */
91
    protected function getRedirectResponse(FormEvent $event, Request $request)
92
    {
93
        if ($request->query->has('redirect_uri')) {
94
            return new RedirectResponse($request->query->get('redirect_uri'));
95
        }
96
97
        return parent::getRedirectResponse($event, $request);
98
    }
99
}
100