Completed
Push — remove-content-bundle ( fd7705...1b4274 )
by Kamil
18:36
created

ChannelAndCurrencyPricingConfigurationTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transform() 0 17 3
A reverseTransform() 0 12 2
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
namespace Sylius\Bundle\CoreBundle\Form\DataTransformer;
13
14
use Symfony\Component\Form\DataTransformerInterface;
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * @author Arkadiusz Krakowiak <[email protected]>
19
 */
20
final class ChannelAndCurrencyPricingConfigurationTransformer implements DataTransformerInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $delimiter;
26
27
    /**
28
     * @param string $delimiter
29
     */
30
    public function __construct($delimiter)
31
    {
32
        $this->delimiter = $delimiter;
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function transform($configuration)
39
    {
40
        if (empty($configuration)) {
41
            return [];
42
        }
43
44
        Assert::isArray($configuration);
45
46
        $flatConfiguration = [];
47
        array_walk($configuration, function ($value, $channelCode) use (&$flatConfiguration) {
48
            foreach ($value as $currencyCode => $price) {
49
                $flatConfiguration[sprintf('%s%s%s', $channelCode, $this->delimiter, $currencyCode)] = $price;
50
            }
51
        });
52
53
        return $flatConfiguration;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function reverseTransform($flatConfiguration)
60
    {
61
        Assert::isArray($flatConfiguration);
62
63
        $configuration = [];
64
        foreach ($flatConfiguration as $key => $value) {
65
            list($channelCode, $currencyCode) = explode($this->delimiter, $key);
66
            $configuration[$channelCode][$currencyCode] = $value;
67
        }
68
69
        return $configuration;
70
    }
71
}
72