Completed
Push — 1.3-pay-grid-action ( 93d89d )
by Kamil
21:56
created

OrderController::summaryAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\CoreBundle\Controller;
15
16
use FOS\RestBundle\View\View;
17
use Sylius\Bundle\OrderBundle\Controller\OrderController as BaseOrderController;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Webmozart\Assert\Assert;
21
22
class OrderController extends BaseOrderController
23
{
24
    public function summaryAction(Request $request): Response
25
    {
26
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
27
28
        $cart = $this->getCurrentCart();
29
        if (null !== $cart->getId()) {
30
            $cart = $this->getOrderRepository()->findCartForSummary($cart->getId());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Order\R...rderRepositoryInterface as the method findCartForSummary() does only exist in the following implementations of said interface: Sylius\Bundle\CoreBundle...ine\ORM\OrderRepository.

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...
31
        }
32
33
        if (!$configuration->isHtmlRequest()) {
34
            return $this->viewHandler->handle($configuration, View::create($cart));
35
        }
36
37
        $form = $this->resourceFormFactory->create($configuration, $cart);
38
39
        $view = View::create()
40
            ->setTemplate($configuration->getTemplate('summary.html'))
41
            ->setData([
42
                'cart' => $cart,
43
                'form' => $form->createView(),
44
            ])
45
        ;
46
47
        return $this->viewHandler->handle($configuration, $view);
48
    }
49
50
    public function thankYouAction(Request $request): Response
51
    {
52
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
53
54
        $orderId = $request->getSession()->get('sylius_order_id', null);
55
56
        if (null === $orderId) {
57
            $options = $configuration->getParameters()->get('after_failure');
58
59
            return $this->redirectHandler->redirectToRoute(
60
                $configuration,
61
                $options['route'] ?? 'sylius_shop_homepage',
62
                $options['parameters'] ?? []
63
            );
64
        }
65
66
        $request->getSession()->remove('sylius_order_id');
67
        $order = $this->repository->find($orderId);
68
        Assert::notNull($order);
69
70
        $view = View::create()
71
            ->setData([
72
                'order' => $order,
73
            ])
74
            ->setTemplate($configuration->getParameters()->get('template'))
75
        ;
76
77
        return $this->viewHandler->handle($configuration, $view);
78
    }
79
}
80