DefaultClassLoader   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getReportClassLoader() 0 5 1
A getClassLoader() 0 18 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 DefaultClassLoader
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('java.lang.ClassLoader')
35
     */
36 1
    public function getReportClassLoader(Report $report): JavaObject
37
    {
38 1
        $reportPath = $report->getReportPath();
39
40 1
        return $this->getClassLoader([$reportPath]);
41
    }
42
43
    /**
44
     * @param string[] $paths           paths that will be added to the classLoader
45
     * @param bool     $checkPathExists default to true, will throw exception if path does not exists
46
     *
47
     * @return JavaObject Java('java.lang.ClassLoader')
48
     */
49 19
    public function getClassLoader(array $paths, bool $checkPathExists = true): JavaObject
50
    {
51 19
        $classLoaderPaths = [];
52 19
        foreach ($paths as $path) {
53 19
            if ($checkPathExists && !is_dir($path)) {
54 1
                throw new InvalidArgumentException(
55 1
                    sprintf(
56 1
                        'Cannot add path to classLoader, it does not exists: %s',
57
                        $path
58
                    )
59
                );
60
            }
61 18
            $classLoaderPaths[] = $this->ba->java('java.io.File', $path)->toUrl();
62
        }
63
64 18
        $classLoaderPaths = array_unique($classLoaderPaths);
65
66 18
        return $this->ba->java('java.net.URLClassLoader', $classLoaderPaths);
67
    }
68
}
69