Completed
Pull Request — master (#77)
by Thibaud
07:33
created

Record::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of Phraseanet SDK.
5
 *
6
 * (c) Alchemy <[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 PhraseanetSDK\Repository;
13
14
use PhraseanetSDK\AbstractRepository;
15
use PhraseanetSDK\Entity\Query;
16
use PhraseanetSDK\Exception\RuntimeException;
17
use Doctrine\Common\Collections\ArrayCollection;
18
19
class Record extends AbstractRepository
20
{
21
    /**
22
     * Find the record by its id that belongs to the provided databox
23
     *
24
     * @param  integer                      $databoxId The record databox id
25
     * @param  integer                      $recordId  The record id
26
     * @param  boolean                      $disableCache Bypass cache when fetching a single record
27
     * @return \PhraseanetSDK\Entity\Record
28
     * @throws RuntimeException
29 2
     */
30
    public function findById($databoxId, $recordId, $disableCache = false)
31 2
    {
32 2
        $path = sprintf('v1/records/%s/%s/', $databoxId, $recordId);
33
        $query = [];
34 2
35
        if (true === $disableCache) {
36
            $query['t'] = time();
37
        }
38 2
39
        $response = $this->query('GET', $path, $query);
40 2
41 1
        if (true !== $response->hasProperty('record')) {
42
            throw new RuntimeException('Missing "record" property in response content');
43
        }
44 1
45
        return \PhraseanetSDK\Entity\Record::fromValue($response->getProperty('record'));
0 ignored issues
show
Bug introduced by
It seems like $response->getProperty('record') targeting PhraseanetSDK\Http\APIResponse::getProperty() can also be of type array<integer,object<stdClass>> or null; however, PhraseanetSDK\Entity\Record::fromValue() does only seem to accept object<stdClass>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
46
    }
47
48
    /**
49
     * Find records
50
     *
51
     * @param  integer          $offsetStart The offset
52
     * @param  integer          $perPage     The number of item per page
53
     * @return ArrayCollection
54
     * @throws RuntimeException
55 2
     */
56
    public function find($offsetStart, $perPage)
57 2
    {
58 2
        $response = $this->query('POST', 'v1/records/search/', array(), array(
59 2
            'query'        => 'all',
60 2
            'offset_start' => (int) $offsetStart,
61 2
            'per_page'     => (int) $perPage,
62
        ));
63 2
64 1
        if (true !== $response->hasProperty('results')) {
65
            throw new RuntimeException('Missing "results" property in response content');
66
        }
67 1
68 1
        return new ArrayCollection(\PhraseanetSDK\Entity\Record::fromList(
69 1
            $response->getProperty('results')
0 ignored issues
show
Bug introduced by
It seems like $response->getProperty('results') targeting PhraseanetSDK\Http\APIResponse::getProperty() can also be of type null or object<stdClass>; however, PhraseanetSDK\Entity\Record::fromList() does only seem to accept array<integer,object<stdClass>>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
70
        ));
71
    }
72
73
    /**
74
     * Search for records
75
     *
76
     * @param  array                       $parameters Query parameters
77
     * @return \PhraseanetSDK\Entity\Query object
78
     * @throws RuntimeException
79 2
     */
80
    public function search(array $parameters = array())
81 2
    {
82 2
        $response = $this->query('POST', 'v1/search/', array(), array_merge(
83
            array('search_type' => 0),
84 2
            $parameters
85
        ));
86 2
87 1
        if ($response->isEmpty()) {
88
            throw new RuntimeException('Response content is empty');
89
        }
90 1
91
        return Query::fromValue($this->em, $response->getResult());
92
    }
93
}
94