Completed
Push — master ( d1b735...0bfc48 )
by Daniel
08:29
created

PhpcrOdmExtension::load()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
rs 8.7449
cc 5
eloc 32
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
7
use Doctrine\DBAL\DriverManager;
8
use Doctrine\ODM\PHPCR\Configuration;
9
use Doctrine\ODM\PHPCR\DocumentManager;
10
use Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver;
11
use Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver;
12
use Doctrine\ODM\PHPCR\NodeTypeRegistrator;
13
use Jackalope\RepositoryFactoryDoctrineDBAL;
14
use Jackalope\Transport\DoctrineDBAL\RepositorySchema;
15
use PhpBench\DependencyInjection\Container;
16
use PhpBench\DependencyInjection\ExtensionInterface;
17
use PHPCR\SimpleCredentials;
18
use Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\PhpcrOdmAgent;
19
20
class PhpcrOdmExtension implements ExtensionInterface
21
{
22
    public function getDefaultConfig()
23
    {
24
        return [
25
            'db_path' => __DIR__ . '/../../../../cache/test.sqlite',
26
        ];
27
    }
28
29
    public function load(Container $container)
30
    {
31
        $container->register('phpcr_odm', function (Container $container) {
32
            $dbPath = $container->getParameter('db_path');
33
            $registerNodeTypes = false;
34
35
            $connection = DriverManager::getConnection([
36
                'driver'    => 'pdo_sqlite',
37
                'path' => $dbPath,
38
            ]);
39
40
            // automatically setup the schema if the db doesn't exist yet.
41
            if (!file_exists($dbPath)) {
42
                if (!file_exists($dir = dirname($dbPath))) {
43
                    mkdir($dir);
44
                }
45
46
                $schema = new RepositorySchema();
47
                foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) {
48
                    $connection->exec($sql);
49
                }
50
51
                $registerNodeTypes = true;
52
            }
53
54
            $factory = new RepositoryFactoryDoctrineDBAL();
55
            $repository = $factory->getRepository([
56
                'jackalope.doctrine_dbal_connection' => $connection,
57
            ]);
58
            $session = $repository->login(new SimpleCredentials(null, null), 'default');
59
60
            if ($registerNodeTypes) {
61
                $typeRegistrator = new NodeTypeRegistrator();
62
                $typeRegistrator->registerNodeTypes($session);
63
            }
64
65
            $xmlDriver = new XmlDriver([__DIR__ . '/mappings']);
66
            $annotationDriver = new AnnotationDriver(new AnnotationReader(), [
67
                __DIR__ . '/../../../vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Document',
68
            ]);
69
            $chain = new MappingDriverChain();
70
            $chain->addDriver($xmlDriver, 'Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional\Model');
71
            $chain->addDriver($annotationDriver, 'Doctrine');
72
73
            $config = new Configuration();
74
            $config->setMetadataDriverImpl($chain);
75
76
            return DocumentManager::create($session, $config);
77
        });
78
79
        $container->register('psi_object_agent.phpcr_odm', function (Container $container) {
80
            return new PhpcrOdmAgent($container->get('phpcr_odm'));
81
        });
82
    }
83
}
84