getCompileManagerJavaException()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 7.0283

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 42
ccs 22
cts 24
cp 0.9167
rs 8.5066
c 0
b 0
f 0
cc 7
nc 7
nop 2
crap 7.0283
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\Proxy\Engine;
15
16
use Soluble\Japha\Bridge\Adapter as BridgeAdapter;
17
use Soluble\Japha\Bridge\Exception\JavaException;
18
use Soluble\Japha\Interfaces\JavaClass;
19
use Soluble\Japha\Interfaces\JavaObject;
20
use Soluble\Jasper\Exception;
21
use Soluble\Jasper\Proxy\RemoteJavaObjectProxyInterface;
22
23
class JasperCompileManager implements RemoteJavaObjectProxyInterface
24
{
25
    /**
26
     * @var BridgeAdapter
27
     */
28
    private $ba;
29
30
    /**
31
     * @var JavaClass Java('net.sf.jasperreports.engine.JasperCompileManager')
32
     */
33
    private $compileManager;
34
35 29
    public function __construct(BridgeAdapter $bridgeAdapter)
36
    {
37 29
        $this->ba = $bridgeAdapter;
38 29
    }
39
40
    /**
41
     * Compile the jrxml report file in a faster binary format.
42
     *
43
     * @return JavaObject Java('net.sf.jasperreports.engine.JasperReport')
44
     *
45
     * @throws Exception\BrokenXMLReportFileException when cannot parse the xml content or invalid xml file
46
     * @throws Exception\ReportFileNotFoundException  when the report file cannot be located (both php and java sides)
47
     * @throws Exception\ReportCompileException       when there's an error compiling/evaluating the report
48
     * @throws Exception\JavaProxiedException         when the compileReport has encountered a Java error
49
     * @throws Exception\RuntimeException             when an unexpected problem have been encountered
50
     */
51 24
    public function compileReport(string $reportFile): JavaObject
52
    {
53
        try {
54 24
            return $this->getJavaProxiedObject()->compileReport($reportFile);
55 7
        } catch (JavaException $e) {
56 7
            throw $this->getCompileManagerJavaException($e, $reportFile);
57
        } catch (\Throwable $e) {
58
            throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
59
        }
60
    }
61
62
    /**
63
     * Compile the jrxml report file in a file.
64
     *
65
     * @throws Exception\InvalidArgumentException     when the source and dest are the same file
66
     * @throws Exception\BrokenXMLReportFileException when cannot parse the xml content or invalid xml file
67
     * @throws Exception\ReportFileNotFoundException  when the report file cannot be located (both php and java sides)
68
     * @throws Exception\ReportCompileException       when there's an error compiling/evaluating the report
69
     * @throws Exception\JavaProxiedException         when the compileReport has encountered a Java error
70
     * @throws Exception\RuntimeException             when an unexpected problem have been encountered
71
     */
72 4
    public function compileReportToFile(string $sourceFile, string $destFile): void
73
    {
74 4
        if (!file_exists($sourceFile)) {
75 1
            throw new Exception\ReportFileNotFoundException(
76 1
                sprintf(
77 1
                    'Report file %s does not exists',
78
                    $sourceFile
79
                )
80
            );
81
        }
82
83 3
        if (basename($sourceFile) === basename($destFile)) {
84 1
            throw new Exception\InvalidArgumentException(
85 1
                sprintf(
86 1
                    'Source and destination file names must be different (source: %s, dest: %s)',
87
                    $sourceFile,
88
                    $destFile
89
                    )
90
            );
91
        }
92
93
        try {
94 2
            $this->getJavaProxiedObject()->compileReportToFile($sourceFile, $destFile);
95 1
        } catch (JavaException $e) {
96 1
            throw $this->getCompileManagerJavaException($e, $sourceFile);
97
        } catch (\Throwable $e) {
98
            throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
99
        }
100 1
    }
101
102
    /**
103
     * Return mapped exception from jasper compile manager java:.
104
     *
105
     * Exception\BrokenXMLReportFileException        when cannot parse the xml content or invalid xml file
106
     * Exception\ReportFileNotFoundException         when the report file is not found
107
     * Exception\ReportFileNotFoundFromJavaException when the report file is not found from the java side
108
     * Exception\ReportCompileException              when there's an error compiling/evaluating the report
109
     * Exception\JavaProxiedException                when the compileReport has encountered a Java error
110
     */
111 8
    private function getCompileManagerJavaException(JavaException $e, string $reportFile): Exception\ExceptionInterface
112
    {
113 8
        $exception = null;
114
115 8
        $className = $e->getJavaClassName();
116 8
        if ($className === 'net.sf.jasperreports.engine.JRException') {
117 8
            $cause = $e->getCause();
118 8
            if (mb_strpos($cause, 'java.io.FileNotFoundException') !== false) {
119 2
                if (file_exists($reportFile)) {
120 1
                    $exception = new Exception\ReportFileNotFoundFromJavaException(sprintf(
121 1
                        'Report file "%s" exists but cannot be located from the java side.',
122
                        $reportFile
123
                    ));
124
                } else {
125 1
                    $exception = new Exception\ReportFileNotFoundException(sprintf(
126 2
                        'Report file "%s" cannot be found',
127
                        $reportFile
128
                    ));
129
                }
130 6
            } elseif (mb_strpos($cause, 'org.xml.sax.SAXParseException') !== false) {
131 4
                $exception = new Exception\BrokenXMLReportFileException($e, sprintf(
132 4
                    'The report file "%s" cannot be parsed or not in jasper format',
133
                    $reportFile
134
                ));
135 2
            } elseif (mb_strpos($cause, 'Errors were encountered when compiling report expressions class file') !== false) {
136 1
                $exception = new Exception\ReportCompileException($e, sprintf(
137 1
                    'Report compilation failed for "%s"',
138
                    $reportFile
139
                ));
140 1
            } elseif (mb_strpos($cause, 'Error saving file:') !== false) {
141 1
                $exception = new Exception\JavaSaveProxiedException($e, sprintf(
142 1
                    'Cannot save file, %s',
143
                    $cause
144
                ));
145
            }
146
        }
147
148 8
        return $exception ?? new Exception\JavaProxiedException(
149
            $e,
150
            sprintf(
151 8
                'Error compiling report "%s"',
152
                $reportFile
153
                )
154
            );
155
    }
156
157
    /**
158
     * @return JavaObject Java('net.sf.jasperreports.engine.JasperCompileManager')
159
     */
160 27
    public function getJavaProxiedObject(): JavaObject
161
    {
162 27
        if ($this->compileManager === null) {
163 27
            $this->compileManager = $this->ba->javaClass('net.sf.jasperreports.engine.JasperCompileManager');
164
        }
165
166 27
        return $this->compileManager;
167
    }
168
}
169