Passed
Push — master ( c80e12...1c5165 )
by Kirill
04:32
created

ExecutionsProvider   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 0
loc 146
ccs 40
cts 48
cp 0.8333
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 16 3
A scalar() 0 10 2
B cast() 0 26 6
A count() 0 5 1
A sum() 0 5 1
A avg() 0 5 1
A max() 0 5 1
A min() 0 5 1
A collect() 0 4 1
A first() 0 4 1
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Query;
11
12
use Illuminate\Support\Arr;
13
use RDS\Hydrogen\Query;
14
use RDS\Hydrogen\Collection\Collection;
15
16
/**
17
 * Class ExecutionsProvider
18
 * @mixin Query
19
 */
20
trait ExecutionsProvider
21
{
22
    /**
23
     * @param string ...$fields
24
     * @return object[]|iterable
25
     */
26 30
    public function get(string ...$fields): iterable
27
    {
28 30
        $processor = $this->getRepository()->getProcessor();
0 ignored issues
show
Bug introduced by
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
30 30
        if (\count($fields)) {
31 12
            return \collect($processor->getArrayResult($this))->map(function (array $data) use ($fields) {
32 12
                if (\count($fields)) {
33 12
                    return Arr::only($data, $fields);
34
                }
35
36
                return $data;
37 12
            });
38
        }
39
40 20
        return $processor->getResult($this);
41
    }
42
43
    /**
44
     * @param string $field
45
     * @param string|null $typeOf
46
     * @return mixed
47
     * @throws \LogicException
48
     */
49 10
    public function scalar(string $field, string $typeOf = null)
50
    {
51 10
        $result = \data_get($this->first($field), $field);
52
53 10
        if ($typeOf !== null) {
54 10
            return $this->cast($result, $typeOf);
55
        }
56
57
        return $result;
58
    }
59
60
    /**
61
     * @param mixed $result
62
     * @param string $typeOf
63
     * @return array|\Closure|object
64
     */
65 10
    private function cast($result, string $typeOf)
66
    {
67 10
        $typeOf = \strtolower($typeOf);
68
69
        switch ($typeOf) {
70 10
            case 'callable':
71
                return function (callable $applicator = null) use ($result) {
72
                    return ($applicator ?? '\\value')($result);
73
                };
74
75 10
            case 'object':
76
                return (object)$result;
77
78 10
            case 'array':
79 10
            case 'iterable':
80
                return (array)$result;
81
        }
82
83 10
        $function = $typeOf . 'val';
84
85 10
        if (! \function_exists($function)) {
86
            throw new \InvalidArgumentException('Could not cast to type ' . $typeOf);
87
        }
88
89 10
        return $function($result);
90
    }
91
92
    /**
93
     * @param string|null $field
94
     * @return int
95
     * @throws \LogicException
96
     */
97 2
    public function count(?string $field = 'id'): int
98
    {
99 2
        return $this->select('COUNT(' . $field . ') AS __count')
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
100 2
            ->scalar('__count', 'int');
101
    }
102
103
    /**
104
     * @param string|null $field
105
     * @return int
106
     * @throws \LogicException
107
     */
108 2
    public function sum(string $field = null): int
109
    {
110 2
        return $this->select('SUM(' . $field . ') AS __sum')
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
111 2
            ->scalar('__sum', 'int');
112
    }
113
114
    /**
115
     * @param string|null $field
116
     * @return int
117
     * @throws \LogicException
118
     */
119 2
    public function avg(string $field = null): int
120
    {
121 2
        return $this->select('AVG(' . $field . ') AS __avg')
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
122 2
            ->scalar('__avg', 'int');
123
    }
124
125
    /**
126
     * @param string|null $field
127
     * @return int
128
     * @throws \LogicException
129
     */
130 2
    public function max(string $field = null): int
131
    {
132 2
        return $this->select('MAX(' . $field . ') AS __max')
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
133 2
            ->scalar('__max', 'int');
134
    }
135
136
    /**
137
     * @param string|null $field
138
     * @return int
139
     * @throws \LogicException
140
     */
141 2
    public function min(string $field = null): int
142
    {
143 2
        return $this->select('MIN(' . $field . ') AS __min')
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
144 2
            ->scalar('__min', 'int');
145
    }
146
147
    /**
148
     * @param string ...$fields
149
     * @return Collection
150
     */
151 2
    public function collect(string ...$fields): Collection
152
    {
153 2
        return Collection::wrap($this->get(...$fields));
154
    }
155
156
    /**
157
     * @param string[] $fields
158
     * @return object|null
159
     * @throws \LogicException
160
     */
161 10
    public function first(string ...$fields)
162
    {
163 10
        return \array_first($this->get(...$fields));
164
    }
165
}
166