Completed
Push — wtf-removal ( 4585f3 )
by Kamil
18:08
created

getCustomerStatistics()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 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
namespace Sylius\Component\Core\Customer\Statistics;
13
14
use Sylius\Component\Core\Model\ChannelInterface;
15
use Sylius\Component\Core\Model\CustomerInterface;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
18
use Sylius\Component\Resource\Repository\RepositoryInterface;
19
20
/**
21
 * @author Jan Góralski <[email protected]>
22
 */
23
final class CustomerStatisticsProvider implements CustomerStatisticsProviderInterface
24
{
25
    /**
26
     * @var OrderRepositoryInterface
27
     */
28
    private $orderRepository;
29
30
    /**
31
     * @var RepositoryInterface
32
     */
33
    private $channelRepository;
34
35
    /**
36
     * @param OrderRepositoryInterface $orderRepository
37
     * @param RepositoryInterface $channelRepository
38
     */
39
    public function __construct(OrderRepositoryInterface $orderRepository, RepositoryInterface $channelRepository)
40
    {
41
        $this->orderRepository = $orderRepository;
42
        $this->channelRepository = $channelRepository;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getCustomerStatistics(CustomerInterface $customer)
49
    {
50
        $orders = $this->orderRepository->findByCustomer($customer);
51
        if (empty($orders)) {
52
            return new CustomerStatistics([]);
53
        }
54
55
        $perChannelCustomerStatisticsArray = [];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $perChannelCustomerStatisticsArray exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
56
57
        $channels = $this->channelRepository->findAll();
58
        foreach ($channels as $channel) {
59
            $channelOrders = $this->filterOrdersByChannel($orders, $channel);
60
            if (empty($channelOrders)) {
61
                continue;
62
            }
63
64
            $perChannelCustomerStatisticsArray[] = new PerChannelCustomerStatistics(
65
                count($channelOrders),
66
                $this->getOrdersSummedTotal($channelOrders),
67
                $channel
68
            );
69
        }
70
71
        return new CustomerStatistics($perChannelCustomerStatisticsArray);
72
    }
73
74
    /**
75
     * @param OrderInterface[] $orders
76
     *
77
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
78
     */
79
    private function getOrdersSummedTotal(array $orders)
80
    {
81
        return array_sum(array_map(function (OrderInterface $order) {
82
                return $order->getTotal();
83
            }, $orders)
84
        );
85
    }
86
87
    /**
88
     * @param OrderInterface[] $orders
89
     * @param ChannelInterface $channel
90
     *
91
     * @return OrderInterface[]
92
     */
93
    private function filterOrdersByChannel(array $orders, ChannelInterface $channel)
94
    {
95
        return array_filter($orders, function (OrderInterface $order) use ($channel) {
96
            return $order->getChannel() === $channel;
97
        });
98
    }
99
}
100