DefaultFileResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 18
c 1
b 0
f 0
dl 0
loc 52
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getReportFileResolver() 0 5 1
A __construct() 0 3 1
A getFileResolver() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Jasper report integration for PHP
7
 *
8
 * @link      https://github.com/belgattitude/soluble-jasper
9
 * @author    Vanvelthem Sébastien
10
 * @copyright Copyright (c) 2017-2019 Vanvelthem Sébastien
11
 * @license   MIT
12
 */
13
14
namespace Soluble\Jasper\Context;
15
16
use Soluble\Japha\Bridge\Adapter as BridgeAdapter;
17
use Soluble\Japha\Interfaces\JavaObject;
18
use Soluble\Jasper\Exception\InvalidArgumentException;
19
use Soluble\Jasper\Report;
20
21
class DefaultFileResolver
22
{
23
    /**
24
     * @var BridgeAdapter
25
     */
26
    private $ba;
27
28 19
    public function __construct(BridgeAdapter $ba)
29
    {
30 19
        $this->ba = $ba;
31 19
    }
32
33
    /**
34
     * @return JavaObject Java('net.sf.jasperreports.engine.util.FileResolver')
35
     */
36 1
    public function getReportFileResolver(Report $report, bool $resolveAbsolutePath = true): JavaObject
37
    {
38 1
        $reportPath = $report->getReportPath();
39
40 1
        return $this->getFileResolver([$reportPath], $resolveAbsolutePath);
41
    }
42
43
    /**
44
     * @param string[] $paths               paths that will be added to the FileResolver
45
     * @param bool     $resolveAbsolutePath Resolve absolute paths
46
     * @param bool     $checkPathExists     default to true, will throw exception if path does not exists
47
     *
48
     * @return JavaObject Java('net.sf.jasperreports.engine.util.FileResolver')
49
     */
50 19
    public function getFileResolver(array $paths, bool $resolveAbsolutePath = true, bool $checkPathExists = true): JavaObject
51
    {
52 19
        $resolverPaths = [];
53 19
        foreach ($paths as $path) {
54 19
            if ($checkPathExists && !is_dir($path)) {
55 1
                throw new InvalidArgumentException(
56 1
                    sprintf(
57 1
                        'Cannot add path to FileResolver, it does not exists: %s',
58
                        $path
59
                    )
60
                );
61
            }
62 18
            $resolverPaths[] = $this->ba->java('java.io.File', $path);
63
        }
64
65 18
        $fileResolver = $this->ba->java(
66 18
            'net.sf.jasperreports.engine.util.SimpleFileResolver',
67
            $resolverPaths
68
        );
69
70 18
        $fileResolver->setResolveAbsolutePath($resolveAbsolutePath);
71
72 18
        return $fileResolver;
73
    }
74
}
75