ManyToAnyListener   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 80.43%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 77
ccs 37
cts 46
cp 0.8043
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A postLoad() 0 9 2
A preRemove() 0 12 2
A postPersist() 0 24 4
A postGenerateSchema() 0 16 2
1
<?php
2
3
namespace JMS\JobQueueBundle\Entity\Listener;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use Doctrine\Persistence\ManagerRegistry;
7
use JMS\JobQueueBundle\Entity\Job;
8
9
/**
10
 * Provides many-to-any association support for jobs.
11
 *
12
 * This listener only implements the minimal support for this feature. For
13
 * example, currently we do not support any modification of a collection after
14
 * its initial creation.
15
 *
16
 * @see http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/annotations/ManyToAny.html
17
 * @author Johannes M. Schmitt <[email protected]>
18
 */
19
class ManyToAnyListener
20
{
21
    private $registry;
22 52
    private $ref;
23
24 52
    public function __construct(ManagerRegistry $registry)
25 52
    {
26 52
        $this->registry = $registry;
27 52
        $this->ref = new \ReflectionProperty('JMS\JobQueueBundle\Entity\Job', 'relatedEntities');
28
        $this->ref->setAccessible(true);
29 32
    }
30
31 32
    public function postLoad(\Doctrine\ORM\Event\LifecycleEventArgs $event)
32 32
    {
33 2
        $entity = $event->getEntity();
34
        if (!$entity instanceof \JMS\JobQueueBundle\Entity\Job) {
35
            return;
36 32
        }
37 32
38
        $this->ref->setValue($entity, new PersistentRelatedEntitiesCollection($this->registry, $entity));
39
    }
40
41
    public function preRemove(LifecycleEventArgs $event)
42
    {
43
        $entity = $event->getEntity();
44
        if (!$entity instanceof Job) {
45
            return;
46
        }
47
48
        $con = $event->getEntityManager()->getConnection();
49
        $con->executeStatement("DELETE FROM jms_job_related_entities WHERE job_id = :id", array(
50
            'id' => $entity->getId(),
51
        ));
52 48
    }
53
54 48
    public function postPersist(\Doctrine\ORM\Event\LifecycleEventArgs $event)
55 48
    {
56 4
        $entity = $event->getEntity();
57
        if (!$entity instanceof \JMS\JobQueueBundle\Entity\Job) {
58
            return;
59 48
        }
60 48
61 4
        $con = $event->getEntityManager()->getConnection();
62 4
        foreach ($this->ref->getValue($entity) as $relatedEntity) {
63 4
            $relClass = \Doctrine\Common\Util\ClassUtils::getClass($relatedEntity);
64
            $relId = $this->registry->getManagerForClass($relClass)->getMetadataFactory()->getMetadataFor($relClass)->getIdentifierValues($relatedEntity);
65 4
            asort($relId);
66
67
            if (!$relId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relId of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68
                throw new \RuntimeException('The identifier for the related entity "' . $relClass . '" was empty.');
69 4
            }
70 4
71 4
            $con->executeStatement("INSERT INTO jms_job_related_entities (job_id, related_class, related_id) VALUES (:jobId, :relClass, :relId)", array(
72 4
                'jobId' => $entity->getId(),
73
                'relClass' => $relClass,
74
                'relId' => json_encode($relId),
75 48
            ));
76
        }
77 52
    }
78
79 52
    public function postGenerateSchema(\Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs $event)
80
    {
81
        $schema = $event->getSchema();
82 52
83 22
        // When using multiple entity managers ignore events that are triggered by other entity managers.
84
        if ($event->getEntityManager()->getMetadataFactory()->isTransient('JMS\JobQueueBundle\Entity\Job')) {
85
            return;
86 52
        }
87 52
88 52
        $table = $schema->createTable('jms_job_related_entities');
89 52
        $table->addColumn('job_id', 'bigint', array('notnull' => true, 'unsigned' => true));
90 52
        $table->addColumn('related_class', 'string', array('notnull' => true, 'length' => '150'));
91 52
        $table->addColumn('related_id', 'string', array('notnull' => true, 'length' => '100'));
92 52
        $table->setPrimaryKey(array('job_id', 'related_class', 'related_id'));
93
        $table->addForeignKeyConstraint('jms_jobs', array('job_id'), array('id'));
94
    }
95
}
96