ReportPathResolver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 12
c 1
b 0
f 1
dl 0
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolve() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\Resolver;
6
7
use InvalidArgumentException;
8
use RuntimeException;
9
use Safe\Exceptions\StringsException;
10
use function Safe\sprintf;
11
use Setono\SyliusStockMovementPlugin\Model\ReportInterface;
12
use Throwable;
13
use Twig\Environment;
14
use Twig\Error\LoaderError;
15
use Twig\Error\RuntimeError;
16
use Twig\Error\SyntaxError;
17
18
final class ReportPathResolver implements ReportPathResolverInterface
19
{
20
    /** @var Environment */
21
    private $twig;
22
23
    public function __construct(Environment $twig)
24
    {
25
        $this->twig = $twig;
26
    }
27
28
    /**
29
     * @throws StringsException
30
     * @throws Throwable
31
     * @throws LoaderError
32
     * @throws RuntimeError
33
     * @throws SyntaxError
34
     */
35
    public function resolve(ReportInterface $report): string
36
    {
37
        $reportConfiguration = $report->getReportConfiguration();
38
        if (null === $reportConfiguration) {
39
            throw new RuntimeException(sprintf('No report configuration associated with report %s', $report->getId()));
40
        }
41
42
        $template = $this->twig->load($reportConfiguration->getTemplate());
43
44
        if (!$template->hasBlock('extension')) {
45
            throw new InvalidArgumentException(sprintf(
46
                'The block "extension" is not present in the template %s',
47
                $reportConfiguration->getTemplate()
48
            ));
49
        }
50
51
        $extension = $template->renderBlock('extension');
52
53
        return 'stock-movement-report-' . $report->getId() . '.' . $extension;
54
    }
55
}
56