ReportGenerator::generate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 9
dl 0
loc 19
rs 9.9666
c 4
b 0
f 1
cc 3
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\Generator;
6
7
use Setono\SyliusStockMovementPlugin\Model\ReportConfigurationInterface;
8
use Setono\SyliusStockMovementPlugin\Model\ReportInterface;
9
use Setono\SyliusStockMovementPlugin\Provider\StockMovementProviderInterface;
10
use Setono\SyliusStockMovementPlugin\Validator\ReportValidatorInterface;
11
use Sylius\Component\Resource\Factory\FactoryInterface;
12
use Sylius\Component\Resource\Repository\RepositoryInterface;
13
14
class ReportGenerator implements ReportGeneratorInterface
15
{
16
    /** @var FactoryInterface */
17
    private $reportFactory;
18
19
    /** @var RepositoryInterface */
20
    private $reportRepository;
21
22
    /** @var StockMovementProviderInterface */
23
    private $stockMovementProvider;
24
25
    /** @var ReportValidatorInterface */
26
    private $reportValidator;
27
28
    public function __construct(
29
        FactoryInterface $reportFactory,
30
        RepositoryInterface $reportRepository,
31
        StockMovementProviderInterface $stockMovementProvider,
32
        ReportValidatorInterface $reportValidator
33
    ) {
34
        $this->reportFactory = $reportFactory;
35
        $this->reportRepository = $reportRepository;
36
        $this->stockMovementProvider = $stockMovementProvider;
37
        $this->reportValidator = $reportValidator;
38
    }
39
40
    public function generate(ReportConfigurationInterface $reportConfiguration): ?ReportInterface
41
    {
42
        /** @var ReportInterface $report */
43
        $report = $this->reportFactory->createNew();
44
        $report->setReportConfiguration($reportConfiguration);
45
46
        foreach ($this->stockMovementProvider->getStockMovements($reportConfiguration) as $stockMovement) {
47
            $report->addStockMovement($stockMovement);
48
        }
49
50
        if ($report->getStockMovements()->count() === 0) {
51
            return null;
52
        }
53
54
        $this->reportValidator->validate($report);
55
56
        $this->reportRepository->add($report);
57
58
        return $report;
59
    }
60
}
61