AbstractEntityQuery::assignEntity()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 3
nop 1
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\Model\ModelInterface;
16
use Moss\Storage\Query\Accessor\AccessorInterface;
17
use Moss\Storage\Query\EventDispatcher\EventDispatcherInterface;
18
use Moss\Storage\Query\Relation\RelationFactoryInterface;
19
20
/**
21
 * Abstract Entity Query
22
 * For queries that expect require entity instance
23
 * Ensures that entity is of expected type
24
 *
25
 * @package Moss\Storage\Query\OperationTraits
26
 */
27
abstract class AbstractEntityQuery extends AbstractQuery
28
{
29
    protected $instance;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param Connection               $connection
35
     * @param mixed                    $entity
36
     * @param ModelInterface           $model
37
     * @param RelationFactoryInterface $factory
38
     * @param AccessorInterface        $accessor
39
     * @param EventDispatcherInterface $dispatcher
40
     */
41
    public function __construct(Connection $connection, $entity, ModelInterface $model, RelationFactoryInterface $factory, AccessorInterface $accessor, EventDispatcherInterface $dispatcher)
42
    {
43
        parent::__construct($connection, $model, $factory, $accessor, $dispatcher);
44
        $this->assignEntity($entity);
45
    }
46
47
    /**
48
     * Assigns entity instance
49
     * Asserts if entity instance is of expected type
50
     *
51
     * @param array|object $entity
52
     *
53
     * @throws QueryException
54
     */
55
    protected function assignEntity($entity)
56
    {
57
        $entityClass = $this->model->entity();
58
59
        if ($entity === null) {
60
            throw new QueryException(sprintf('Missing required entity of class "%s"', $entityClass));
61
        }
62
63
        if (!is_array($entity) && !$entity instanceof $entityClass) {
64
            throw new QueryException(sprintf('Entity must be an instance of "%s" or array got "%s"', $entityClass, $this->getType($entity)));
65
        }
66
67
        $this->instance = $entity;
68
    }
69
70
    /**
71
     * Assigns primary condition
72
     *
73
     * @throws QueryException
74
     */
75 View Code Duplication
    protected function setPrimaryKeyConditions()
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...
76
    {
77
        foreach ($this->model->primaryFields() as $field) {
78
            $value = $this->accessor->getPropertyValue($this->instance, $field->name());
79
            $this->builder->andWhere(
80
                sprintf(
81
                    '%s = %s',
82
                    $this->connection->quoteIdentifier($field->mappedName()),
83
                    $this->builder->createNamedParameter($value, $field->type())
84
                )
85
            );
86
        }
87
    }
88
}
89