UpdateQuery::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 6
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