Completed
Push — master ( eaa10a...12d04d )
by Albert
03:25
created

DatabaseServiceProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 43
ccs 0
cts 25
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 28 1
1
<?php
2
3
namespace Albert221\Blog\ServiceProvider;
4
5
use Albert221\Blog\Entity\Category;
6
use Albert221\Blog\Entity\Post;
7
use Albert221\Blog\Repository\CategoryRepositoryInterface;
8
use Albert221\Blog\Repository\PostRepositoryInterface;
9
use Doctrine\ORM\EntityManager;
10
use Doctrine\ORM\Tools\Setup;
11
use League\Container\ServiceProvider\AbstractServiceProvider;
12
13
class DatabaseServiceProvider extends AbstractServiceProvider
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $provides = [
19
        'entityManager',
20
        PostRepositoryInterface::class,
21
        CategoryRepositoryInterface::class
22
    ];
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function register()
28
    {
29
        $this->getContainer()->share('entityManager', function () {
30
            $config = Setup::createAnnotationMetadataConfiguration(
31
                [dirname(__DIR__)],
32
                $this->getContainer()->get('config')['debug']
33
            );
34
35
            return EntityManager::create([
36
                'dbname' => 'blog',
37
                'user' => 'root',
38
                'password' => '',
39
                'host' => 'localhost',
40
                'charset' => 'utf8',
41
                'driver' => 'pdo_mysql'
42
            ], $config);
43
        });
44
45
        $this->getContainer()->share(
46
            PostRepositoryInterface::class,
47
            $this->getContainer()->get('entityManager')->getRepository(Post::class)
48
        );
49
50
        $this->getContainer()->share(
51
            CategoryRepositoryInterface::class,
52
            $this->getContainer()->get('entityManager')->getRepository(Category::class)
53
        );
54
    }
55
}
56