Completed
Push — master ( 93ed75...8dabb9 )
by Klaas
03:51
created

DBase::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 28
nc 2
nop 1
dl 0
loc 37
ccs 0
cts 34
cp 0
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: ktammling
5
 * Date: 16.06.17
6
 * Time: 14:56
7
 */
8
9
namespace Framework;
10
11
use Doctrine\ORM\Tools\Setup;
12
use Doctrine\ORM\EntityManager;
13
use Doctrine\DBAL\DriverManager;
14
use Doctrine\DBAL;
15
use SimpleThings\EntityAudit\AuditConfiguration;
16
use SimpleThings\EntityAudit\AuditManager;
17
use Doctrine\Common\EventManager;
18
19
class DBase {
20
21
    protected $dbal;
22
    protected $entityManager;
23
24
    public function __construct($Config)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
    {
26
27
        $dbal_config = new DBAL\Configuration();
28
        $this->dbal = DriverManager::getConnection($Config['dbase'], $dbal_config);
29
        $orm_config = Setup::createAnnotationMetadataConfiguration(array(SCRIPT_BASE.'/src/entities'), $Config['general']['devMode'], null, null, false);
30
        if (extension_loaded('apcu')) {
31
            $orm_config->setQueryCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
32
            $orm_config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
33
            $orm_config->setResultCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
34
        }
35
        $auditconfig = new AuditConfiguration();
36
        $auditconfig->setAuditedEntityClasses(array(
37
            'Entity\Addresses',
38
            'Entity\Prefixes',
39
            'Entity\User',
40
            'Entity\Vrfs'
41
        ));
42
        $auditconfig->setGlobalIgnoreColumns(array(
43
            'created_at',
44
            'updated_at'
45
        ));
46
        $auditconfig->setUsernameCallable(function() {
47
            return $_SESSION['Username'];
48
        });
49
        $evm = new EventManager();
50
        $auditManager = new AuditManager($auditconfig);
51
        $auditManager->registerEvents($evm);
52
        $EntityManager = EntityManager::create($Config['dbase'], $orm_config, $evm);
53
        $EntityManager->getConfiguration()->addCustomStringFunction('inet_aton', 'Application\DQL\InetAtonFunction');
54
        $EntityManager->getConfiguration()->addCustomStringFunction('inet6_aton', 'Application\DQL\Inet6AtonFunction');
55
        $EntityManager->getConfiguration()->addCustomStringFunction('MATCH_AGAINST', 'Application\DQL\MatchAgainstFunction');
56
        $this->entityManager = $EntityManager;
57
58
        return true;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
59
60
    }
61
62
    /**
63
     * @return DBAL\Connection
64
     */
65
    public function getDbal()
66
    {
67
        return $this->dbal;
68
    }
69
70
    /**
71
     * @return EntityManager
72
     */
73
    public function getEntityManager()
74
    {
75
        return $this->entityManager;
76
    }
77
78
79
80
}