LaravelBaseQuery::getAuth()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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