RewardHandler::apply()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusReferralsPlugin\Reward;
6
7
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateReferralAwareInterface;
8
use Sylius\Component\Core\Model\OrderInterface;
9
use Traversable;
10
11
final class RewardHandler implements RewardHandlerInterface
12
{
13
    private array $handlers;
14
15
    public function __construct(
16
        Traversable $handlers,
17
    ) {
18
        $this->handlers = iterator_to_array($handlers);
19
    }
20
21
    public function apply(OrderInterface $order): void
22
    {
23
        if (!$order instanceof AffiliateReferralAwareInterface) {
24
            return;
25
        }
26
27
        if (null === $affiliateReferral = $order->getAffiliateReferral()) {
28
            return;
29
        }
30
31
        /** @var string $rewardType */
32
        $rewardType = $affiliateReferral->getRewardType();
33
34
        $this->resolve($rewardType)->apply($order);
35
    }
36
37
    private function resolve(string $rewardType): RewardHandlerInterface
38
    {
39
        return $this->handlers[$rewardType];
40
    }
41
}
42