Completed
Push — master ( 51a51b...3888f6 )
by Michael
06:55
created

DatabaseServiceProvider::getDbMigrationsService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Stats\Providers;
4
5
use Joomla\Database\DatabaseDriver;
6
use Joomla\DI\Container;
7
use Joomla\DI\ServiceProviderInterface;
8
use League\Flysystem\Adapter\Local;
9
use League\Flysystem\Filesystem;
10
use Stats\Database\Migrations;
11
12
/**
13
 * Database service provider
14
 *
15
 * @since  1.0
16
 */
17
class DatabaseServiceProvider implements ServiceProviderInterface
18
{
19
	/**
20
	 * Registers the service provider with a DI container.
21
	 *
22
	 * @param   Container  $container  The DI container.
23
	 *
24
	 * @return  void
25
	 *
26
	 * @since   1.0
27
	 */
28 1
	public function register(Container $container)
29
	{
30 1
		$container->alias('db', DatabaseDriver::class)
31 1
			->share(DatabaseDriver::class, [$this, 'getDbService'], true);
32
33 1
		$container->alias('db.migrations', Migrations::class)
34 1
			->share(Migrations::class, [$this, 'getDbMigrationsService'], true);
35 1
	}
36
37
	/**
38
	 * Get the `db` service
39
	 *
40
	 * @param   Container  $container  The DI container.
41
	 *
42
	 * @return  DatabaseDriver
43
	 *
44
	 * @since   1.0
45
	 */
46 1
	public function getDbService(Container $container)
47
	{
48
		/** @var \Joomla\Registry\Registry $config */
49 1
		$config = $container->get('config');
50
51 1
		$db = DatabaseDriver::getInstance((array) $config->get('database'));
52 1
		$db->setDebug($config->get('database.debug'));
53 1
		$db->setLogger($container->get('monolog.logger.database'));
54
55 1
		return $db;
56
	}
57
58
	/**
59
	 * Get the `db.migrations` service
60
	 *
61
	 * @param   Container  $container  The DI container.
62
	 *
63
	 * @return  Migrations
64
	 *
65
	 * @since   1.0
66
	 */
67 1
	public function getDbMigrationsService(Container $container)
68
	{
69 1
		return new Migrations(
70 1
			$container->get('db'),
71 1
			new Filesystem(new Local(APPROOT . '/etc'))
72 1
		);
73
	}
74
}
75