Completed
Pull Request — develop (#39)
by Axel
14:01 queued 11:18
created

DynamicRelationSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 101
ccs 0
cts 62
cp 0
rs 10
c 1
b 0
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getSubscribedEvents() 0 6 1
A loadClassMetadata() 0 18 4
A mapProject() 0 10 1
A mapFeature() 0 10 1
A mapFeedback() 0 18 1
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
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
10
11
class DynamicRelationSubscriber implements EventSubscriber
12
{
13
    const MODEL_PROJECT = 'Developtech\AgilityBundle\Entity\Project';
14
    const MODEL_FEATURE = 'Developtech\AgilityBundle\Entity\Feature';
15
    const MODEL_FEEDBACK = 'Developtech\AgilityBundle\Entity\Feedback';
16
17
    /** @var string **/
18
    protected $userClass;
19
20
    /**
21
     * @param string $userClass
22
     */
23
    public function __construct($userClass) {
24
        $this->userClass = $userClass;
25
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30
    public function getSubscribedEvents()
31
    {
32
        return array(
33
            Events::loadClassMetadata,
34
        );
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
        $namingStrategy = $eventArgs
46
            ->getEntityManager()
47
            ->getConfiguration()
48
            ->getNamingStrategy()
49
        ;
50
51
        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...
52
            case self::MODEL_PROJECT: return $this->mapProject($metadata, $namingStrategy);
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...
53
            case self::MODEL_FEATURE: return $this->mapFeature($metadata, $namingStrategy);
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...
54
            case self::MODEL_FEEDBACK: return $this->mapFeedback($metadata, $namingStrategy);
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...
55
            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...
56
        }
57
    }
58
59
    /**
60
     * @param CLassMetadata $metadata
61
     * @param UnderscoreNamingStrategy $namingStrategy
62
     */
63
    public function mapProject(ClassMetadata $metadata, UnderscoreNamingStrategy $namingStrategy) {
0 ignored issues
show
Unused Code introduced by
The parameter $namingStrategy is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
        $metadata->mapManyToOne(array(
65
            'targetEntity'  => $this->userClass,
66
            'fieldName'     => 'productOwner',
67
            'cascade'       => array(),
68
            'joinColumn'    => array(
69
                'nullable' => false
70
            )
71
        ));
72
    }
73
74
    /**
75
     * @param CLassMetadata $metadata
76
     * @param UnderscoreNamingStrategy $namingStrategy
77
     */
78
    public function mapFeature(ClassMetadata $metadata, UnderscoreNamingStrategy $namingStrategy) {
0 ignored issues
show
Unused Code introduced by
The parameter $namingStrategy is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
        $metadata->mapManyToOne(array(
80
            'targetEntity'  => $this->userClass,
81
            'fieldName'     => 'developer',
82
            'cascade'       => array(),
83
            'joinColumn'    => array(
84
                'nullable' => true
85
            )
86
        ));
87
    }
88
89
    /**
90
     * @param CLassMetadata $metadata
91
     * @param UnderscoreNamingStrategy $namingStrategy
92
     */
93
    public function mapFeedback(ClassMetadata $metadata, UnderscoreNamingStrategy $namingStrategy) {
0 ignored issues
show
Unused Code introduced by
The parameter $namingStrategy is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
        $metadata->mapManyToOne(array(
95
            'targetEntity'  => $this->userClass,
96
            'fieldName'     => 'author',
97
            'cascade'       => array(),
98
            'joinColumn'    => array(
99
                'nullable' => false
100
            )
101
        ));
102
        $metadata->mapManyToOne(array(
103
            'targetEntity'  => $this->userClass,
104
            'fieldName'     => 'developer',
105
            'cascade'       => array(),
106
            'joinColumn'    => array(
107
                'nullable' => true
108
            )
109
        ));
110
    }
111
}
112