Completed
Push — master ( adecaf...4dd8bf )
by Filipe
02:35
created

QueryObject::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Repository;
11
12
use Slick\Database\Sql\Select;
13
use Slick\Orm\RepositoryInterface;
14
15
/**
16
 * QueryObject
17
 *
18
 * @package Slick\Orm\Repository
19
 * @author  Filipe Silva <[email protected]>
20
 */
21
class QueryObject extends Select implements QueryObjectInterface
22
{
23
24
    /**
25
     * @var RepositoryInterface
26
     */
27
    protected $repository;
28
29
    /**
30
     * QueryObject has a repository as a dependency.
31
     *
32
     * @param RepositoryInterface $repository
33
     */
34
    public function __construct(RepositoryInterface $repository)
35
    {
36
        $this->repository = $repository;
37
        $this->adapter = $repository->getAdapter();
38
        parent::__construct(
39
            $repository->getEntityDescriptor()->getTableName()
40
        );
41
    }
42
43
    /**
44
     * Retrieve all records matching this select query
45
     *
46
     * @return \Slick\Database\RecordList
47
     */
48
    public function all()
49
    {
50
        $data = $this->adapter->query($this, $this->getParameters());
51
        return $this->repository->getEntityMapper()->createFrom($data);
52
    }
53
54
    /**
55
     * Retrieve first record matching this select query
56
     *
57
     * @return mixed
58
     */
59
    public function first()
60
    {
61
        $sql = clone($this);
62
        $sql->limit(1);
63
        $result = $this->adapter->query($sql, $sql->getParameters());
64
        return  (count($result) > 0) ? $result[0] : null;
65
    }
66
}