Completed
Push — master ( 89deac...cf3c32 )
by Bas
02:49
created

Grammar::wrapBindVar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Schema;
4
5
use Illuminate\Support\Fluent;
6
use Illuminate\Database\Schema\Grammars\Grammar as IlluminateGrammar;
7
use LaravelFreelancerNL\FluentAQL\Facades\AQB;
8
9
class Grammar extends IlluminateGrammar
10
{
11
    /**
12
     * ArangoDB handles transactions itself. However most schema actions
13
     * are not done through queries but rather through commands.
14
     * So we just run schema actions sequentially.
15
     *
16
     * @var bool
17
     */
18
    protected $transactions = false;
19
20
    /**
21
     * Compile AQL to check if an attribute is in use within a document in the collection.
22
     * If multiple attributes are set then all must be set in one document.
23
     * @param string $collection
24
     * @param Fluent $command
25
     * @return Fluent
26
     */
27
    public function compileHasAttribute($collection, Fluent $command)
28
    {
29
        if (is_string($command->attribute)) {
30
            $command->attribute = [$command->attribute];
31
        }
32
33
        $filter = [];
34
        foreach ($command->attribute as $attribute) {
35
            $filter[] = ['doc.'.$attribute, '!=', 'null'];
36
        }
37
38
        $aqb = AQB::for('doc', $collection)
39
            ->filter($filter)
0 ignored issues
show
Bug introduced by
$filter of type array|array<mixed,array<integer,string>> is incompatible with the type string expected by parameter $attribute of LaravelFreelancerNL\Flue...sQueryClauses::filter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
            ->filter(/** @scrutinizer ignore-type */ $filter)
Loading history...
40
            ->limit(1)
41
            ->return('true')
42
            ->get();
43
44
        $command->aqb = $aqb;
45
46
        return $command;
47
    }
48
49
    /**
50
     * Compile AQL to rename an attribute, if the new name isn't already in use.
51
     *
52
     * @param string $collection
53
     * @param Fluent $command
54
     * @return Fluent
55
     */
56
    public function compileRenameAttribute($collection, Fluent $command)
57
    {
58
        $bindings['@collection'] = $collection;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$bindings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $bindings = array(); before regardless.
Loading history...
59
60
        $bindings['from'] = $this->wrapBindVar($command->to);
61
        $bindings['to'] = $this->wrapBindVar($command->from);
62
63
        $filter = [
64
            ['doc.'.$command->from, '!=', 'null'],
65
            ['doc.'.$command->to],
66
        ];
67
68
        $aqb = AQB::for('doc', $collection)
69
            ->filter($filter)
0 ignored issues
show
Bug introduced by
$filter of type array<integer,array<integer,string>> is incompatible with the type string expected by parameter $attribute of LaravelFreelancerNL\Flue...sQueryClauses::filter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            ->filter(/** @scrutinizer ignore-type */ $filter)
Loading history...
70
            ->update(
71
                'doc',
72
                [
73
                    $command->from => 'null',
74
                    $command->to => 'doc.'.$command->from,
75
                ],
76
                $collection)
77
            ->options( ['keepNull' => true])
78
            ->get();
79
80
        $command->aqb = $aqb;
81
82
        return $command;
83
    }
84
85
    /**
86
     * Compile AQL to drop one or more attributes.
87
     *
88
     * @param string $collection
89
     * @param Fluent $command
90
     * @return Fluent
91
     */
92
    public function compileDropAttribute($collection, Fluent $command)
93
    {
94
        $filter = [];
95
        if (is_string($command->attributes)) {
0 ignored issues
show
introduced by
The condition is_string($command->attributes) is always false.
Loading history...
96
            $command->attributes = [$command->attributes];
97
        }
98
99
        $data = [];
100
        foreach ($command->attributes as $attribute) {
101
            $filter[] = ['doc.'.$attribute, '!=', 'null', 'OR'];
102
            $data[$attribute] = 'null';
103
        }
104
        $aqb = AQB::for('doc', $collection)
105
            ->filter($filter )
0 ignored issues
show
Bug introduced by
$filter of type array|array<mixed,array<integer,string>> is incompatible with the type string expected by parameter $attribute of LaravelFreelancerNL\Flue...sQueryClauses::filter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
            ->filter(/** @scrutinizer ignore-type */ $filter )
Loading history...
106
            ->update('doc', $data, $collection)
107
            ->options(['keepNull' => false])
108
            ->get();
109
110
        $command->aqb = $aqb;
111
112
        return $command;
113
    }
114
115
    /**
116
     * Prepare a bindVar for inclusion in an AQL query.
117
     *
118
     * @param $attribute
119
     * @return string
120
     */
121
    public function wrapBindVar($attribute)
122
    {
123
        $attribute = trim($attribute);
124
        if (strpos($attribute, '@') === 0) {
125
            $attribute = "`$attribute`";
126
        }
127
128
        return $attribute;
129
    }
130
}
131