InsertQuery   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 7 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 15
Bugs 1 Features 2
Metric Value
wmc 9
c 15
b 1
f 2
lcom 1
cbo 9
dl 7
loc 100
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A setQuery() 0 5 1
A assignValue() 0 17 4
A execute() 0 17 2
A reset() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Definition\FieldInterface;
17
use Moss\Storage\Model\ModelInterface;
18
use Moss\Storage\Query\Accessor\AccessorInterface;
19
use Moss\Storage\Query\EventDispatcher\EventDispatcherInterface;
20
use Moss\Storage\Query\Relation\RelationFactoryInterface;
21
22
/**
23
 * Query used to insert entity into table
24
 *
25
 * @author  Michal Wachowski <[email protected]>
26
 * @package Moss\Storage
27
 */
28
class InsertQuery extends AbstractEntityValueQuery implements InsertQueryInterface
29
{
30
    const EVENT_BEFORE = 'insert.before';
31
    const EVENT_AFTER = 'insert.after';
32
33
    use GetTypeTrait;
34
35
    protected $instance;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param Connection               $connection
41
     * @param mixed                    $entity
42
     * @param ModelInterface           $model
43
     * @param RelationFactoryInterface $factory
44
     * @param AccessorInterface        $accessor
45
     * @param EventDispatcherInterface $dispatcher
46
     */
47 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...
48
    {
49
        parent::__construct($connection, $entity, $model, $factory, $accessor, $dispatcher);
50
51
        $this->setQuery();
52
        $this->values();
53
    }
54
55
    /**
56
     * Sets query instance with delete operation and table
57
     */
58
    protected function setQuery()
59
    {
60
        $this->builder = $this->connection->createQueryBuilder();
61
        $this->builder->insert($this->connection->quoteIdentifier($this->model->table()));
62
    }
63
64
    /**
65
     * Assigns value to query
66
     *
67
     * @param FieldInterface $field
68
     */
69
    protected function assignValue(FieldInterface $field)
70
    {
71
        $value = $this->accessor->getPropertyValue($this->instance, $field->name());
72
73
        if ($value === null && $field->attribute('autoincrement')) {
74
            return;
75
        }
76
77
        if ($value === null) {
78
            $this->getValueFromReferencedEntity($field);
79
        }
80
81
        $this->builder->setValue(
82
            $this->connection->quoteIdentifier($field->mappedName()),
83
            $this->builder->createNamedParameter($value, $field->type())
84
        );
85
    }
86
87
    /**
88
     * Executes query
89
     * After execution query is reset
90
     *
91
     * @return mixed
92
     */
93
    public function execute()
94
    {
95
        $this->dispatcher->fire(self::EVENT_BEFORE, $this->instance);
96
97
        $this->builder()->execute();
98
99
        $result = $this->connection->lastInsertId();
100
        $this->accessor->identifyEntity($this->model, $this->instance, $result);
101
102
        $this->dispatcher->fire(self::EVENT_AFTER, $this->instance);
103
104
        foreach ($this->relations as $relation) {
105
            $relation->write($this->instance);
106
        }
107
108
        return $this->instance;
109
    }
110
111
    /**
112
     * Resets adapter
113
     *
114
     * @return $this
115
     */
116
    public function reset()
117
    {
118
        $this->builder->resetQueryParts();
119
        $this->relations = [];
120
        $this->resetBinds();
121
122
        $this->setQuery();
123
        $this->values();
124
125
        return $this;
126
    }
127
}
128