66
77use Illuminate \Support \Facades \File ;
88use Illuminate \Support \Facades \Process ;
9+ use Moox \VeraPdf \DTOs \VeraPdfHealth ;
910use Moox \VeraPdf \DTOs \VeraPdfResult ;
1011use Moox \VeraPdf \Support \VeraPdfOutputPath ;
1112use RuntimeException ;
13+ use Throwable ;
1214
1315class VeraPdfService
1416{
17+ public function basePath (): string
18+ {
19+ return rtrim ((string ) config ('verapdf.base_path ' ), '/ \\' );
20+ }
21+
1522 public function launcherPath (): string
1623 {
17- $ basePath = rtrim (( string ) config ( ' verapdf.base_path ' ), ' / \\' );
24+ $ basePath = $ this -> basePath ( );
1825 $ launcher = (string ) config ('verapdf.paths.launcher ' , 'verapdf ' );
1926 $ path = $ basePath .'/ ' .$ launcher ;
2027
@@ -34,23 +41,74 @@ public function launcherPath(): string
3441 return $ path ;
3542 }
3643
44+ public function javaMissingMessage (): string
45+ {
46+ return 'Java not found. Install a JRE/JDK on the server first (e.g. sudo apt install default-jre-headless). ' ;
47+ }
48+
49+ public function notInstalledMessage (): string
50+ {
51+ return 'veraPDF is not installed. Run php artisan verapdf:install first. ' ;
52+ }
53+
54+ public function assertJavaAvailable (): void
55+ {
56+ if ($ this ->javaAvailable ()) {
57+ return ;
58+ }
59+
60+ throw new RuntimeException ($ this ->javaMissingMessage ());
61+ }
62+
63+ public function assertInstalled (): void
64+ {
65+ if ($ this ->isInstalled ()) {
66+ return ;
67+ }
68+
69+ throw new RuntimeException ($ this ->notInstalledMessage ());
70+ }
71+
72+ public function inspectHealth (): VeraPdfHealth
73+ {
74+ $ launcherPath = null ;
75+ $ launcherError = null ;
76+
77+ try {
78+ $ launcherPath = $ this ->launcherPath ();
79+ } catch (RuntimeException $ e ) {
80+ $ launcherError = $ e ->getMessage ();
81+ }
82+
83+ $ outputPath = VeraPdfOutputPath::resolve ();
84+ $ outputPathWritable = true ;
85+
86+ try {
87+ File::ensureDirectoryExists ($ outputPath );
88+ } catch (Throwable ) {
89+ $ outputPathWritable = false ;
90+ }
91+
92+ return new VeraPdfHealth (
93+ javaAvailable: $ this ->javaAvailable (),
94+ launcherPath: $ launcherPath ,
95+ launcherError: $ launcherError ,
96+ installed: $ this ->isInstalled (),
97+ cliBinariesPresent: $ this ->hasCliBinaries (),
98+ guiArtefactsPresent: $ this ->hasGuiArtefacts (),
99+ outputPath: $ outputPath ,
100+ outputPathWritable: $ outputPathWritable ,
101+ );
102+ }
103+
37104 /**
38105 * @param string|null $reportDirectory Absolute filesystem directory for report output.
39106 * When null, uses `verapdf.output.path` config.
40107 */
41108 public function validate (string $ pdfPath , ?string $ reportDirectory = null ): VeraPdfResult
42109 {
43- if (! $ this ->javaAvailable ()) {
44- throw new RuntimeException (
45- 'Java not found. Install a JRE/JDK on the server first (e.g. sudo apt install default-jre-headless). '
46- );
47- }
48-
49- if (! $ this ->isInstalled ()) {
50- throw new RuntimeException (
51- 'veraPDF is not installed. Run php artisan verapdf:install first. '
52- );
53- }
110+ $ this ->assertJavaAvailable ();
111+ $ this ->assertInstalled ();
54112
55113 if (! file_exists ($ pdfPath )) {
56114 throw new RuntimeException ("File not found: {$ pdfPath }" );
@@ -138,7 +196,7 @@ public function hasCliBinaries(): bool
138196 */
139197 public function hasGuiArtefacts (): bool
140198 {
141- $ basePath = rtrim (( string ) config ( ' verapdf.base_path ' ), ' / \\' );
199+ $ basePath = $ this -> basePath ( );
142200
143201 if (is_file ($ basePath .'/verapdf-gui ' ) || is_file ($ basePath .'/verapdf-gui.bat ' )) {
144202 return true ;
@@ -162,7 +220,7 @@ public function findCliJar(): ?string
162220
163221 private function findJarInBin (string $ needle ): ?string
164222 {
165- $ binDir = rtrim (( string ) config ( ' verapdf.base_path ' ), ' / \\' ).'/bin ' ;
223+ $ binDir = $ this -> basePath ( ).'/bin ' ;
166224
167225 if (! is_dir ($ binDir )) {
168226 return null ;
0 commit comments