UpdateQuery   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 36.84 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 18
Bugs 1 Features 3
Metric Value
wmc 7
c 18
b 1
f 3
lcom 1
cbo 9
dl 35
loc 95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A setQuery() 0 5 1
A assignValue() 13 13 2
A execute() 14 14 2
A reset() 0 12 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 read data from table
24
 *
25
 * @author  Michal Wachowski <[email protected]>
26
 * @package Moss\Storage
27
 */
28
class UpdateQuery extends AbstractEntityValueQuery implements UpdateQueryInterface
29
{
30
    const EVENT_BEFORE = 'update.before';
31
    const EVENT_AFTER = 'update.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
        $this->setPrimaryKeyConditions();
54
    }
55
56
    /**
57
     * Sets query instance with delete operation and table
58
     */
59
    protected function setQuery()
60
    {
61
        $this->builder = $this->connection->createQueryBuilder();
62
        $this->builder->update($this->connection->quoteIdentifier($this->model->table()));
63
    }
64
65
    /**
66
     * Assigns value to query
67
     *
68
     * @param FieldInterface $field
69
     */
70 View Code Duplication
    protected function assignValue(FieldInterface $field)
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...
71
    {
72
        $value = $this->accessor->getPropertyValue($this->instance, $field->name());
73
74
        if ($value === null) {
75
            $this->getValueFromReferencedEntity($field);
76
        }
77
78
        $this->builder->set(
79
            $this->connection->quoteIdentifier($field->mappedName()),
80
            $this->builder->createNamedParameter($value, $field->type())
81
        );
82
    }
83
84
    /**
85
     * Executes query
86
     * After execution query is reset
87
     *
88
     * @return mixed
89
     */
90 View Code Duplication
    public function execute()
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...
91
    {
92
        $this->dispatcher->fire(self::EVENT_BEFORE, $this->instance);
93
94
        $this->builder->execute();
95
96
        $this->dispatcher->fire(self::EVENT_AFTER, $this->instance);
97
98
        foreach ($this->relations as $relation) {
99
            $relation->write($this->instance);
100
        }
101
102
        return $this->instance;
103
    }
104
105
    /**
106
     * Resets adapter
107
     *
108
     * @return $this
109
     */
110
    public function reset()
111
    {
112
        $this->builder->resetQueryParts();
113
        $this->relations = [];
114
        $this->resetBinds();
115
116
        $this->setQuery();
117
        $this->values();
118
        $this->setPrimaryKeyConditions();
119
120
        return $this;
121
    }
122
}
123