DeleteQuery::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
c 7
b 1
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
/*
4
* This file is part of the moss-storage package
5
*
6
* (c) Michal Wachowski <[email protected]>
7
*
8
* For the full copyright and license information, please view the LICENSE
9
* file that was distributed with this source code.
10
*/
11
12
namespace Moss\Storage\Query;
13
14
use Doctrine\DBAL\Connection;
15
use Moss\Storage\GetTypeTrait;
16
use Moss\Storage\Model\ModelInterface;
17
use Moss\Storage\Query\Accessor\AccessorInterface;
18
use Moss\Storage\Query\EventDispatcher\EventDispatcherInterface;
19
use Moss\Storage\Query\Relation\RelationFactoryInterface;
20
21
/**
22
 * Query used to delete data from table
23
 *
24
 * @author  Michal Wachowski <[email protected]>
25
 * @package Moss\Storage
26
 */
27
class DeleteQuery extends AbstractEntityQuery implements DeleteQueryInterface
28
{
29
    const EVENT_BEFORE = 'delete.before';
30
    const EVENT_AFTER = 'delete.after';
31
32
    use GetTypeTrait;
33
34
    protected $instance;
35
36
    /**
37
     * Constructor
38
     *
39
     * @param Connection               $connection
40
     * @param mixed                    $entity
41
     * @param ModelInterface           $model
42
     * @param RelationFactoryInterface $factory
43
     * @param AccessorInterface        $accessor
44
     * @param EventDispatcherInterface $dispatcher
45
     */
46 View Code Duplication
    public function __construct(Connection $connection, $entity, ModelInterface $model, RelationFactoryInterface $factory, AccessorInterface $accessor, EventDispatcherInterface $dispatcher)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        parent::__construct($connection, $entity, $model, $factory, $accessor, $dispatcher);
49
50
        $this->setQuery();
51
        $this->setPrimaryKeyConditions();
52
    }
53
54
    /**
55
     * Sets query instance with delete operation and table
56
     */
57
    protected function setQuery()
58
    {
59
        $this->builder = $this->connection->createQueryBuilder();
60
        $this->builder->delete($this->connection->quoteIdentifier($this->model->table()));
61
    }
62
63
    /**
64
     * Executes query
65
     * After execution query is reset
66
     *
67
     * @return mixed
68
     */
69
    public function execute()
70
    {
71
        foreach ($this->relations as $relation) {
72
            $relation->delete($this->instance);
73
        }
74
75
        $this->dispatcher->fire(self::EVENT_BEFORE, $this->instance);
76
77
        $this->builder->execute();
78
        $this->accessor->identifyEntity($this->model, $this->instance, null);
79
80
        $this->dispatcher->fire(self::EVENT_AFTER, $this->instance);
81
82
        return $this->instance;
83
    }
84
85
    /**
86
     * Resets adapter
87
     *
88
     * @return $this
89
     */
90
    public function reset()
91
    {
92
        $this->builder->resetQueryParts();
93
        $this->relations = [];
94
        $this->resetBinds();
95
96
        $this->setQuery();
97
        $this->setPrimaryKeyConditions();
98
99
        return $this;
100
    }
101
}
102