Passed
Pull Request — master (#182)
by Alex
06:05
created

LaravelBaseQuery::getVerbMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 29/09/19
6
 * Time: 4:55 PM
7
 */
8
9
namespace AlgoWeb\PODataLaravel\Query;
10
11
use AlgoWeb\PODataLaravel\Auth\NullAuthProvider;
12
use AlgoWeb\PODataLaravel\Controllers\MetadataControllerContainer;
13
use AlgoWeb\PODataLaravel\Enums\ActionVerb;
14
use AlgoWeb\PODataLaravel\Interfaces\AuthInterface;
15
use AlgoWeb\PODataLaravel\Providers\MetadataProvider;
16
use Illuminate\Support\Facades\App;
17
use POData\Common\InvalidOperationException;
18
use POData\Providers\Query\QueryResult;
19
20
abstract class LaravelBaseQuery
21
{
22
    /** @var AuthInterface */
23
    protected $auth;
24
    /** @var MetadataProvider */
25
    protected $metadataProvider;
26
    /** @var MetadataControllerContainer */
27
    protected $controllerContainer;
28
    protected $verbMap = [];
29
30
    public function __construct(AuthInterface $auth = null)
31
    {
32
        $this->auth = isset($auth) ? $auth : new NullAuthProvider();
33
        $this->metadataProvider = new MetadataProvider(App::make('app'));
34
        $this->controllerContainer = App::make('metadataControllers');
35
    }
36
37
    /**
38
     * Dig out local copy of POData-Laravel metadata provider.
39
     *
40
     * @return MetadataProvider
41
     */
42
    public function getMetadataProvider()
43
    {
44
        return $this->metadataProvider;
45
    }
46
47
    /**
48
     * Dig out local copy of controller metadata mapping.
49
     *
50
     * @return MetadataControllerContainer
51
     * @throws InvalidOperationException
52
     */
53
    public function getControllerContainer()
54
    {
55
        if (null === $this->controllerContainer) {
56
            throw new InvalidOperationException('Controller container must not be null');
57
        }
58
        return $this->controllerContainer;
59
    }
60
61
    public function getVerbMap()
62
    {
63
        if (0 == count($this->verbMap)) {
64
            $this->verbMap['create'] = ActionVerb::CREATE();
65
            $this->verbMap['update'] = ActionVerb::UPDATE();
66
            $this->verbMap['delete'] = ActionVerb::DELETE();
67
        }
68
        return $this->verbMap;
69
    }
70
71
    protected function getAuth()
72
    {
73
        return $this->auth;
74
    }
75
76
    /**
77
     * @param $sourceEntityInstance
78
     * @return mixed|null|\object[]
79
     */
80
    protected function unpackSourceEntity($sourceEntityInstance)
81
    {
82
        if ($sourceEntityInstance instanceof QueryResult) {
83
            $source = $sourceEntityInstance->results;
84
            $source = (is_array($source)) ? $source[0] : $source;
85
            return $source;
86
        }
87
        return $sourceEntityInstance;
88
    }
89
}
90