Completed
Pull Request — master (#244)
by Łukasz
11:47
created

DataGridBindDataHandler::handleRequest()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 6
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\AbstractHandler;
13
use FSi\Bundle\AdminBundle\Event\AdminEvent;
14
use FSi\Bundle\AdminBundle\Event\ListEvent;
15
use FSi\Bundle\AdminBundle\Event\ListEvents;
16
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class DataGridBindDataHandler extends AbstractHandler
20
{
21
    /**
22
     * @param AdminEvent $event
23
     * @param Request $request
24
     * @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException
25
     * @return null|\Symfony\Component\HttpFoundation\Response
26
     */
27
    public function handleRequest(AdminEvent $event, Request $request)
28
    {
29
        $this->validateEvent($event);
30
31
        if ($request->isMethod('POST')) {
32
            $this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_REQUEST_PRE_BIND, $event);
33
            if ($event->hasResponse()) {
34
                return $event->getResponse();
35
            }
36
37
            $event->getDataGrid()->bindData($request);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FSi\Bundle\AdminBundle\Event\AdminEvent as the method getDataGrid() does only exist in the following sub-classes of FSi\Bundle\AdminBundle\Event\AdminEvent: FSi\Bundle\AdminBundle\Event\ListEvent. 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...
38
            $this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_REQUEST_POST_BIND, $event);
39
            if ($event->hasResponse()) {
40
                return $event->getResponse();
41
            }
42
43
            $event->getElement()->saveDataGrid();
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 saveDataGrid() does only exist in the following implementations of said interface: FSi\Bundle\AdminBundle\Admin\CRUD\AbstractCRUD, FSi\Bundle\AdminBundle\A...CRUD\GenericListElement, FSi\Bundle\AdminBundle\Doctrine\Admin\CRUDElement, FSi\Bundle\AdminBundle\Doctrine\Admin\ListElement, FSi\Bundle\AdminBundle\s...\Doctrine\MyCrudElement, FSi\Bundle\AdminBundle\s...\Doctrine\MyListElement, FSi\Bundle\AdminBundle\spec\fixtures\MyCRUD, FSi\Bundle\AdminBundle\spec\fixtures\MyList, FSi\FixturesBundle\Admin\CustomNews, FSi\FixturesBundle\Admin\CustomSubscriber, FSi\FixturesBundle\Admin\News, FSi\FixturesBundle\Admin\Subscriber.

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...
44
            $event->getDataSource()->bindParameters($request);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FSi\Bundle\AdminBundle\Event\AdminEvent as the method getDataSource() does only exist in the following sub-classes of FSi\Bundle\AdminBundle\Event\AdminEvent: FSi\Bundle\AdminBundle\Event\ListEvent. 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...
45
            $event->getDataGrid()->setData($event->getDataSource()->getResult());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FSi\Bundle\AdminBundle\Event\AdminEvent as the method getDataGrid() does only exist in the following sub-classes of FSi\Bundle\AdminBundle\Event\AdminEvent: FSi\Bundle\AdminBundle\Event\ListEvent. 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...
Bug introduced by
It seems like you code against a specific sub-type and not the parent class FSi\Bundle\AdminBundle\Event\AdminEvent as the method getDataSource() does only exist in the following sub-classes of FSi\Bundle\AdminBundle\Event\AdminEvent: FSi\Bundle\AdminBundle\Event\ListEvent. 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...
46
        }
47
48
        $this->eventDispatcher->dispatch(ListEvents::LIST_RESPONSE_PRE_RENDER, $event);
49
        if ($event->hasResponse()) {
50
            return $event->getResponse();
51
        }
52
    }
53
54
    /**
55
     * @param AdminEvent $event
56
     * @throws \FSi\Bundle\AdminBundle\Exception\RequestHandlerException
57
     */
58
    protected function validateEvent(AdminEvent $event)
59
    {
60
        if (!$event instanceof ListEvent) {
61
            throw new RequestHandlerException(sprintf("%s require ListEvent", get_class($this)));
62
        }
63
    }
64
}
65