Completed
Push — master ( 64f1ce...4aca8d )
by Sébastien
05:09
created

JavaSqlConnection::getJdbcDsn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
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 Vanvelthem Sébastien
11
 * @license   MIT
12
 */
13
14
namespace Soluble\Jasper\DataSource;
15
16
use Soluble\Japha\Bridge\Adapter as BridgeAdapter;
17
use Soluble\Japha\Bridge\Exception\JavaException;
18
use Soluble\Japha\Db\DriverManager;
19
use Soluble\Japha\Interfaces\JavaObject;
20
use Soluble\Jasper\DataSource\Contract\JavaSqlConnectionInterface;
21
use Soluble\Jasper\Exception\JavaProxiedException;
22
23
class JavaSqlConnection implements JavaSqlConnectionInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $dsn;
29
30
    /**
31
     * @var string
32
     */
33
    private $driverClass;
34
35
    /**
36
     * JDBCDataSource constructor.
37
     *
38
     * @param string $jdbcDsn     JDBC DSN, i.e.: "jdbc:mysql://localhost/database?user=X&password=Y&serverTimezone=UTC";
39
     * @param string $driverClass Java driver class, i.e.: 'com.mysql.jdbc.Driver' (must be in classpath on the jvm side)
40
     */
41 2
    public function __construct(string $jdbcDsn, string $driverClass)
42
    {
43 2
        $this->dsn         = $jdbcDsn;
44 2
        $this->driverClass = $driverClass;
45 2
    }
46
47 2
    public function getDriverClass(): string
48
    {
49 2
        return $this->driverClass;
50
    }
51
52 2
    public function getJdbcDsn(): string
53
    {
54 2
        return $this->dsn;
55
    }
56
57
    /**
58
     * @return JavaObject Java('java.sql.Connection')
59
     *
60
     * @throws JavaProxiedException when the sql connection cannot be initialized
61
     */
62 1
    public function getJasperReportSqlConnection(BridgeAdapter $bridgeAdapter): JavaObject
63
    {
64
        try {
65 1
            $connection = (new DriverManager($bridgeAdapter))->createConnection(
66 1
                $this->getJdbcDsn(),
67 1
                $this->getDriverClass()
68
            );
69 1
        } catch (JavaException $e) {
70 1
            throw new JavaProxiedException(
71 1
                $e,
72 1
                sprintf(
73 1
                'Error getting sql connection: %s',
74 1
                $e->getMessage()
75
            )
76
            );
77
        }
78
79
        return $connection;
80
    }
81
}
82