ReadOneQuery   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 6
Bugs 2 Features 1
Metric Value
wmc 6
c 6
b 2
f 1
lcom 1
cbo 5
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 21 4
A reset() 0 7 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
 * Query used to read one entity from table
22
 *
23
 * @author  Michal Wachowski <[email protected]>
24
 * @package Moss\Storage
25
 */
26
class ReadOneQuery extends ReadQuery
27
{
28
    /**
29
     * Constructor
30
     *
31
     * @param Connection               $connection
32
     * @param ModelInterface           $model
33
     * @param RelationFactoryInterface $factory
34
     * @param AccessorInterface        $accessor
35
     * @param EventDispatcherInterface $dispatcher
36
     */
37
    public function __construct(Connection $connection, ModelInterface $model, RelationFactoryInterface $factory, AccessorInterface $accessor, EventDispatcherInterface $dispatcher)
38
    {
39
        parent::__construct($connection, $model, $factory, $accessor, $dispatcher);
40
        $this->limit(1);
41
    }
42
43
    /**
44
     * Executes query
45
     * After execution query is reset
46
     *
47
     * @return mixed
48
     * @throws QueryException
49
     */
50
    public function execute()
51
    {
52
        $this->dispatcher->fire(ReadQuery::EVENT_BEFORE);
53
54
        $stmt = $this->executeQuery();
55
        $result = $this->model->entity() ? $this->fetchAsObject($stmt) : $this->fetchAsAssoc($stmt);
56
57
        if (!count($result)) {
58
            throw new QueryException(sprintf('Result out of range or does not exists for "%s"', $this->model->entity()));
59
        }
60
61
        $this->dispatcher->fire(ReadQuery::EVENT_AFTER);
62
63
        $result = array_slice($result, 0, 1, false);
64
65
        foreach ($this->relations as $relation) {
66
            $result = $relation->read($result);
67
        }
68
69
        return $result[0];
70
    }
71
72
    /**
73
     * Resets adapter
74
     *
75
     * @return $this
76
     */
77
    public function reset()
78
    {
79
        parent::reset();
80
        $this->limit(1);
81
82
        return $this;
83
    }
84
}
85