Delete   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 3
c 7
b 2
f 0
lcom 1
cbo 6
dl 0
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 27 3
1
<?php
2
3
namespace Analogue\ORM\Commands;
4
5
use Analogue\ORM\Exceptions\MappingException;
6
7
class Delete extends Command
8
{
9
    /**
10
     * Execute the Delete Statement
11
     *
12
     * @throws MappingException
13
     * @throws \InvalidArgumentException
14
     * @return false|void
15
     */
16
    public function execute()
17
    {
18
        $aggregate = $this->aggregate;
19
20
        $entity = $aggregate->getEntityObject();
21
22
        $mapper = $aggregate->getMapper();
23
24
        if ($mapper->fireEvent('deleting', $entity) === false) {
25
            return false;
26
        }
27
28
        $keyName = $aggregate->getEntityMap()->getKeyName();
29
        
30
        $id = $this->aggregate->getEntityId();
31
32
        if (is_null($id)) {
33
            throw new MappingException('Executed a delete command on an entity with "null" as primary key');
34
        }
35
36
        $this->query->where($keyName, '=', $id)->delete();
37
38
        $mapper->fireEvent('deleted', $entity, false);
39
40
        // Once the Entity is successfully deleted, we'll just set the primary key to null.
41
        $aggregate->setEntityAttribute($keyName, null);
42
    }
43
}
44