ODMQuery::setDocumentManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
4
namespace Doctrine\ODM\CouchDB\View;
5
6
use Doctrine\CouchDB\View\Query;
7
use Doctrine\ODM\CouchDB\DocumentManager;
8
9
class ODMQuery extends Query
10
{
11
    /**
12
     * @var DocumentManager
13
     */
14
    private $dm;
15
16
    /**
17
     * @var bool
18
     */
19
    private $onlyDocs = false;
20
21
    private $toArray = false;
22
23
    public function execute()
24
    {
25
        $response = $this->doExecute();
26
        $data = array();
27
        if ($this->dm && $this->getParameter('include_docs') === true) {
28
            $uow = $this->dm->getUnitOfWork();
29
            foreach ($response->body['rows'] AS $k => $v) {
30
                $doc = $uow->createDocument(null, $v['doc']);
31
                if ($this->toArray) {
32
                    $data[] = $doc;
33 View Code Duplication
                } else if ($this->onlyDocs) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
34
                    $response->body['rows'][$k] = $doc;
35
                } else {
36
                    $response->body['rows'][$k]['doc'] = $doc;
37
                }
38
            }
39
        }
40
41
        return ($this->toArray) ? $data : $this->createResult($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->toArray ? $data :...reateResult($response); of type array|Doctrine\CouchDB\View\Result adds the type array to the return on line 41 which is incompatible with the return type of the parent method Doctrine\CouchDB\View\AbstractQuery::execute of type Doctrine\CouchDB\View\Result.
Loading history...
42
    }
43
44
45
    /**
46
     * @param DocumentManager $dm
47
     */
48
    public function setDocumentManager(DocumentManager $dm)
49
    {
50
        $this->dm = $dm;
51
    }
52
53
    /**
54
     * @param  bool $flag
55
     * @return Query
56
     */
57
    public function onlyDocs($flag)
58
    {
59
        if ($flag) {
60
            $this->setIncludeDocs(true);
61
        }
62
        $this->onlyDocs = $flag;
63
        return $this;
64
    }
65
66
    public function toArray($flag)
67
    {
68
        $this->toArray = $flag;
69
        return $this;
70
    }
71
}
72