Completed
Push — develop ( da2907...7e8dc0 )
by Axel
10s
created

DynamicRelationSubscriber::loadClassMetadata()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 8.8571
cc 5
eloc 8
nc 5
nop 1
crap 30
1
<?php
2
3
namespace Developtech\AgilityBundle\EventSubscriber;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
7
use Doctrine\ORM\Events;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
10
class DynamicRelationSubscriber implements EventSubscriber
11
{
12
    const MODEL_PROJECT = 'Developtech\AgilityBundle\Entity\Project';
13
    const MODEL_FEATURE = 'Developtech\AgilityBundle\Entity\Feature';
14
    const MODEL_FEEDBACK = 'Developtech\AgilityBundle\Entity\Feedback';
15
    const MODEL_BETA_TESTER = 'Developtech\AgilityBundle\Entity\BetaTester';
16
17
    /** @var string **/
18
    protected $userClass;
19
20
    /**
21
     * @param string $userClass
22
     */
23 1
    public function __construct($userClass) {
24 1
        $this->userClass = $userClass;
25 1
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30 1
    public function getSubscribedEvents()
31
    {
32
        return array(
33 1
            Events::loadClassMetadata,
34 1
        );
35
    }
36
37
    /**
38
     * @param LoadClassMetadataEventArgs $eventArgs
39
     */
40
    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
41
    {
42
        // the $metadata is the whole mapping info for this class
43
        $metadata = $eventArgs->getClassMetadata();
44
45
        switch($metadata->getName()) {
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<Doctrine\ORM\EntityManager>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
46
            case self::MODEL_PROJECT: return $this->mapProject($metadata);
0 ignored issues
show
Documentation introduced by
$metadata is of type object<Doctrine\ORM\EntityManager>, but the function expects a object<Doctrine\ORM\Mapping\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
47
            case self::MODEL_FEATURE: return $this->mapFeature($metadata);
0 ignored issues
show
Documentation introduced by
$metadata is of type object<Doctrine\ORM\EntityManager>, but the function expects a object<Doctrine\ORM\Mapping\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
48
            case self::MODEL_FEEDBACK: return $this->mapFeedback($metadata);
0 ignored issues
show
Documentation introduced by
$metadata is of type object<Doctrine\ORM\EntityManager>, but the function expects a object<Doctrine\ORM\Mapping\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
49
            case self::MODEL_BETA_TESTER: return $this->mapBetaTester($metadata);
0 ignored issues
show
Documentation introduced by
$metadata is of type object<Doctrine\ORM\EntityManager>, but the function expects a object<Doctrine\ORM\Mapping\ClassMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
50
            default: return;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
51
        }
52
    }
53
54
    /**
55
     * @param CLassMetadata $metadata
56
     */
57 1
    public function mapProject(ClassMetadata $metadata) {
58
        $metadata->mapManyToOne(array(
59 1
            'targetEntity'  => $this->userClass,
60 1
            'fieldName'     => 'productOwner',
61 1
            'cascade'       => array(),
62
            'joinColumn'    => array(
63
                'name' => 'product_owner_id',
64
                'referencedColumnName' => 'id',
65
                'onDelete' => 'SET NULL',
66
                'nullable' => true
67 1
            )
68
        ));
69
    }
70
71
    /**
72
     * @param CLassMetadata $metadata
73
     */
74 1
    public function mapFeature(ClassMetadata $metadata) {
75
        $metadata->mapManyToOne(array(
76 1
            'targetEntity'  => $this->userClass,
77 1
            'fieldName'     => 'responsible',
78 1
            'cascade'       => array(),
79
            'joinColumn'    => array(
80
                'name' => 'responsible_id',
81
                'referencedColumnName' => 'id',
82
                'onDelete' => 'SET NULL',
83
                'nullable' => true
84 1
            )
85
        ));
86
    }
87
88
    /**
89
     * @param CLassMetadata $metadata
90
     */
91 1
    public function mapFeedback(ClassMetadata $metadata) {
92
        $metadata->mapManyToOne(array(
93 1
            'targetEntity'  => $this->userClass,
94 1
            'fieldName'     => 'author',
95 1
            'cascade'       => array(),
96
            'joinColumn'    => array(
97
                'name' => 'author_id',
98
                'referencedColumnName' => 'id',
99
                'onDelete' => 'CASCADE',
100
                'nullable' => false
101 1
            )
102
        ));
103
        $metadata->mapManyToOne(array(
104 1
            'targetEntity'  => $this->userClass,
105 1
            'fieldName'     => 'responsible',
106 1
            'cascade'       => array(),
107
            'joinColumn'    => array(
108
                'name' => 'responsible_id',
109
                'referencedColumnName' => 'id',
110
                'onDelete' => 'SET NULL',
111
                'nullable' => true
112 1
            )
113
        ));
114
    }
115
116
    /**
117
     * @param CLassMetadata $metadata
118
     */
119 1
    public function mapBetaTester(ClassMetadata $metadata) {
120
        $metadata->mapOnetoOne([
121 1
            'targetEntity' => $this->userClass,
122 1
            'fieldName' => 'account',
123 1
            'cascade' => [],
124
            'joinColumn' => [
125
                'name' => 'account_id',
126
                'referencedColumnName' => 'id',
127
                'onDelete' => 'CASCADE',
128
                'nullable' => false
129 1
            ]
130
        ]);
131
    }
132
}
133