These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Component\Core\Resolver; |
||
15 | |||
16 | use Sylius\Component\Core\Model\ChannelInterface; |
||
17 | use Sylius\Component\Core\Model\PaymentInterface as CorePaymentInterface; |
||
18 | use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; |
||
19 | use Sylius\Component\Payment\Exception\UnresolvedDefaultPaymentMethodException; |
||
20 | use Sylius\Component\Payment\Model\PaymentInterface; |
||
21 | use Sylius\Component\Payment\Model\PaymentMethodInterface; |
||
22 | use Sylius\Component\Payment\Resolver\DefaultPaymentMethodResolverInterface; |
||
23 | use Webmozart\Assert\Assert; |
||
24 | |||
25 | class DefaultPaymentMethodResolver implements DefaultPaymentMethodResolverInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var PaymentMethodRepositoryInterface |
||
29 | */ |
||
30 | protected $paymentMethodRepository; |
||
31 | |||
32 | /** |
||
33 | * @param PaymentMethodRepositoryInterface $paymentMethodRepository |
||
34 | */ |
||
35 | public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository) |
||
36 | { |
||
37 | $this->paymentMethodRepository = $paymentMethodRepository; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | * |
||
43 | * @throws UnresolvedDefaultPaymentMethodException |
||
44 | */ |
||
45 | public function getDefaultPaymentMethod(PaymentInterface $subject): PaymentMethodInterface |
||
46 | { |
||
47 | /** @var CorePaymentInterface $subject */ |
||
48 | Assert::isInstanceOf($subject, CorePaymentInterface::class); |
||
49 | |||
50 | /** @var ChannelInterface $channel */ |
||
51 | $channel = $subject->getOrder()->getChannel(); |
||
0 ignored issues
–
show
|
|||
52 | |||
53 | $paymentMethods = $this->paymentMethodRepository->findEnabledForChannel($channel); |
||
54 | if (empty($paymentMethods)) { |
||
55 | throw new UnresolvedDefaultPaymentMethodException(); |
||
56 | } |
||
57 | |||
58 | return $paymentMethods[0]; |
||
59 | } |
||
60 | } |
||
61 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: