Passed
Pull Request — master (#206)
by Alex
05:39
created

SerialiseUtilitiesTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 12
c 2
b 0
f 0
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B checkElementsInput() 0 11 7
A checkSingleElementInput() 0 6 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 16/02/20
6
 * Time: 1:19 PM
7
 */
8
9
namespace AlgoWeb\PODataLaravel\Serialisers;
10
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Support\Collection;
13
use POData\Common\InvalidOperationException;
14
use POData\Providers\Query\QueryResult;
15
16
trait SerialiseUtilitiesTrait
17
{
18
    /**
19
     * @param QueryResult $entryObjects
20
     * @throws InvalidOperationException
21
     */
22
    protected function checkElementsInput(QueryResult &$entryObjects)
23
    {
24
        $res = $entryObjects->results;
25
        if (!(is_array($res) || $res instanceof Collection)) {
26
            throw new InvalidOperationException('!is_array($entryObjects->results)');
27
        }
28
        if (is_array($res) && 0 == count($res)) {
29
            $entryObjects->hasMore = false;
30
        }
31
        if ($res instanceof Collection && 0 == $res->count()) {
32
            $entryObjects->hasMore = false;
33
        }
34
    }
35
36
    /**
37
     * @param QueryResult $entryObject
38
     * @throws InvalidOperationException
39
     */
40
    protected function checkSingleElementInput(QueryResult $entryObject)
41
    {
42
        if (!$entryObject->results instanceof Model) {
43
            $res = $entryObject->results;
44
            $msg = is_array($res) ? 'Entry object must be single Model' : get_class($res);
45
            throw new InvalidOperationException($msg);
46
        }
47
    }
48
}
49