DoctrineHelper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 0
cbo 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A executeDQL() 0 9 2
A executeSQL() 0 10 2
A dropTables() 0 6 2
1
<?php
2
3
namespace kujaff\VersionsBundle\Model;
4
5
/**
6
 * Helps your using Doctrine
7
 * Just needs a property ContainerInterface $container
8
 */
9
trait DoctrineHelper
10
{
11
    /**
12
     * Execute a DQL query (only for SELECT, UPDATE or DELETE)
13
     *
14
     * @param string $dql
15
     * @param array $parameters
16
     * @return mixed
17
     */
18
    protected function executeDQL($dql, array $parameters = array())
19
    {
20
        $em = $this->container->get('doctrine')->getManager();
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
        $query = $em->createQuery($dql);
22
        foreach ($parameters as $name => $value) {
23
            $query->setParameter($name, $value);
24
        }
25
        return $query->getResult();
26
    }
27
28
    /**
29
     * Execute raw SQL
30
     *
31
     * @param string $sql
32
     * @param array $parameters
33
     * @return \Doctrine\DBAL\Statement
34
     */
35
    protected function executeSQL($sql, array $parameters = array())
36
    {
37
        $em = $this->container->get('doctrine')->getManager();
38
        $stmt = $em->getConnection()->prepare($sql);
39
        foreach ($parameters as $name => $value) {
40
            $stmt->bindValue($name, $value);
41
        }
42
        $stmt->execute();
43
        return $stmt;
44
    }
45
46
    /**
47
     * Drop tables if exists
48
     *
49
     * @param array $tables
50
     */
51
    protected function dropTables(array $tables)
52
    {
53
        foreach ($tables as $table) {
54
            $this->executeSQL('DROP TABLE IF EXISTS ' . $table);
55
        }
56
    }
57
}
58