Issues (52)

src/Concerns/HasLimit.php (1 issue)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Concerns;
5
6
use Level23\Druid\Limits\Limit;
7
use Level23\Druid\OrderBy\OrderBy;
8
use Level23\Druid\Types\SortingOrder;
9
use Level23\Druid\Limits\LimitInterface;
10
use Level23\Druid\Types\OrderByDirection;
11
12
trait HasLimit
13
{
14
    /**
15
     * @var \Level23\Druid\Limits\Limit|null
16
     */
17
    protected ?Limit $limit = null;
18
19
    /**
20
     * @var OrderByDirection|null
21
     */
22
    protected ?OrderByDirection $direction = null;
23
24
    /**
25
     * We can only order by fields if there is a limit specified (....., I know... ).
26
     * When the user applies an order by, but does not specify a limit, we will use this
27
     * high number as a limit.
28
     *
29
     * @var int
30
     */
31
    public static int $DEFAULT_MAX_LIMIT = 999999;
32
33
    /**
34
     * Limit out result by N records.
35
     * The "offset" parameter tells Druid to skip this many rows when returning results.
36
     *
37
     * @param int      $limit
38
     * @param int|null $offset
39
     *
40
     * @return $this
41
     */
42 29
    public function limit(int $limit, ?int $offset = null): self
43
    {
44 29
        if ($this->limit instanceof LimitInterface) {
45 1
            $this->limit->setLimit($limit);
46
        } else {
47 29
            $this->limit = new Limit($limit);
48
        }
49
50 29
        if ($offset !== null) {
51 3
            $this->limit->setOffset($offset);
0 ignored issues
show
The method setOffset() does not exist on null. ( Ignorable by Annotation )

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

51
            $this->limit->/** @scrutinizer ignore-call */ 
52
                          setOffset($offset);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        }
53
54 29
        return $this;
55
    }
56
57
    /**
58
     * Sort the result. This only applies for GroupBy and TopN Queries.
59
     * You should use `orderByDirection()` for TimeSeries, Select and Scan Queries.
60
     *
61
     * @param string                  $dimensionOrMetric The dimension or metric where you want to order by.
62
     * @param string|OrderByDirection $direction         The direction of your order. Default is "asc".
63
     * @param string|SortingOrder     $sortingOrder      The algorithm used to order the result.
64
     *
65
     * @return $this
66
     */
67 16
    public function orderBy(
68
        string $dimensionOrMetric,
69
        string|OrderByDirection $direction = OrderByDirection::ASC,
70
        string|SortingOrder $sortingOrder = SortingOrder::LEXICOGRAPHIC
71
    ): self {
72 16
        $order = new OrderBy($dimensionOrMetric, $direction, $sortingOrder);
73
74 15
        if (!$this->limit) {
75 2
            $this->limit = new Limit();
76
        }
77
78 15
        $this->limit->addOrderBy($order);
79
80 15
        return $this;
81
    }
82
83
    /**
84
     * In which order should we return the result.
85
     * This only applies to TimeSeries, Select and Scan Queries. Use `orderBy()` For GroupBy and TopN Queries.
86
     *
87
     * @param string|OrderByDirection $direction The direction of your order.
88
     *
89
     * @return $this
90
     */
91 8
    public function orderByDirection(string|OrderByDirection $direction = OrderByDirection::DESC): self
92
    {
93 8
        $this->direction = is_string($direction) ? OrderByDirection::make($direction) : $direction;
94
95 8
        return $this;
96
    }
97
98
    /**
99
     * @return \Level23\Druid\Limits\LimitInterface|null
100
     */
101 3
    public function getLimit(): ?LimitInterface
102
    {
103 3
        return $this->limit;
104
    }
105
}