Passed
Push — master ( 6957cf...3bb145 )
by Joachim
04:59
created

ReportPathResolver::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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