Completed
Push — refactoring-session ( a6dec9 )
by Kamil
06:43
created

SalesSummary   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 4
A getChannel() 0 4 1
A getMonths() 0 4 1
A getSales() 0 4 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\Component\Core\Dashboard;
15
16
use Sylius\Component\Core\Model\ChannelInterface;
17
18
/**
19
 * @experimental
20
 */
21
final class SalesSummary implements SalesSummaryInterface
22
{
23
    /** @var ChannelInterface */
24
    private $channel;
25
26
    /** @psalm-var array<string, string> */
27
    private $monthsSalesMap = [];
28
29
    public function __construct(
30
        \DateTimeInterface $startDate,
31
        \DateTimeInterface $endDate,
32
        array $salesData,
33
        ChannelInterface $channel
34
    ) {
35
        $this->channel = $channel;
36
37
        $period = new \DatePeriod($startDate, \DateInterval::createFromDateString('1 month'), $endDate);
38
39
        /** @var \DateTimeInterface $date */
40
        foreach ($period as $date) {
41
            $periodName = $date->format('m.y');
42
            if (!isset($salesData[$periodName])) {
43
                $salesData[$periodName] = 0;
44
            }
45
        }
46
47
        uksort($salesData, function (string $date1, string $date2) {
48
            return \DateTime::createFromFormat('m.y', $date1) <=> \DateTime::createFromFormat('m.y', $date2);
49
        });
50
51
        foreach ($salesData as $month => $sales) {
52
            $this->monthsSalesMap[$month] = number_format(abs($sales / 100), 2, '.', '');
53
        }
54
    }
55
56
    public function getChannel(): ChannelInterface
57
    {
58
        return $this->channel;
59
    }
60
61
    public function getMonths(): array
62
    {
63
        return array_keys($this->monthsSalesMap);
64
    }
65
66
    public function getSales(): array
67
    {
68
        return array_values($this->monthsSalesMap);
69
    }
70
}
71