DatabaseServiceProvider::register()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 34
ccs 0
cts 28
cp 0
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
crap 2
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\Entity\Setting;
8
use Albert221\Blog\Entity\Tag;
9
use Albert221\Blog\Repository\CategoryRepositoryInterface;
10
use Albert221\Blog\Repository\PostRepositoryInterface;
11
use Albert221\Blog\Repository\SettingRepositoryInterface;
12
use Albert221\Blog\Repository\TagRepositoryInterface;
13
use Doctrine\ORM\EntityManager;
14
use Doctrine\ORM\Tools\Setup;
15
use DoctrineExtensions\Query\Mysql\MatchAgainst;
16
use League\Container\ServiceProvider\AbstractServiceProvider;
17
18
class DatabaseServiceProvider extends AbstractServiceProvider
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected $provides = [
24
        'entityManager',
25
        PostRepositoryInterface::class,
26
        CategoryRepositoryInterface::class,
27
        TagRepositoryInterface::class,
28
        SettingRepositoryInterface::class
29
    ];
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function register()
35
    {
36
        $this->getContainer()->share('entityManager', function () {
37
            $config = Setup::createAnnotationMetadataConfiguration(
38
                [dirname(__DIR__)],
39
                $this->getContainer()->get('config')['debug']
40
            );
41
            $config->addCustomStringFunction('MATCH', MatchAgainst::class);
42
43
            $connectionConfig = $this->getContainer()->get('config')['database'];
44
45
            return EntityManager::create($connectionConfig, $config);
46
        });
47
48
        $this->getContainer()->share(
49
            PostRepositoryInterface::class,
50
            $this->getContainer()->get('entityManager')->getRepository(Post::class)
51
        );
52
53
        $this->getContainer()->share(
54
            CategoryRepositoryInterface::class,
55
            $this->getContainer()->get('entityManager')->getRepository(Category::class)
56
        );
57
        
58
        $this->getContainer()->share(
59
            TagRepositoryInterface::class,
60
            $this->getContainer()->get('entityManager')->getRepository(Tag::class)
61
        );
62
63
        $this->getContainer()->share(
64
            SettingRepositoryInterface::class,
65
            $this->getContainer()->get('entityManager')->getRepository(Setting::class)
66
        );
67
    }
68
}
69