RewardHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 3 1
A apply() 0 14 3
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