Issues (115)

src/Services/AbstractService.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace VGirol\JsonApi\Services;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Collection;
7
use VGirol\JsonApi\Contracts\ServiceContract;
8
9
abstract class AbstractService implements ServiceContract
10
{
11
    /**
12
     * @var Collection
13
     */
14
    private $parameters;
15
16
    /**
17
     * Undocumented function
18
     *
19
     * @return string
20
     */
21
    abstract protected function getConfigKey(): string;
22
23
    /**
24
     * Undocumented function
25
     *
26
     * @param Request $request
27
     *
28
     * @return Collection
29
     */
30
    abstract protected function parseParameters($request);
31
32
    /**
33
     * Undocumented function
34
     *
35
     * @param Request $request
36
     * @param boolean $force
37
     *
38
     * @return $this
39
     */
40
    public function parseRequest($request = null, $force = false)
41
    {
42
        $request = $request ?? request();
43
        if (is_null($this->parameters) || $force) {
44
            $this->parameters = $this->parseParameters($request);
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * Undocumented function
52
     *
53
     * @param Request $request
54
     *
55
     * @return boolean
56
     */
57
    public function hasQuery($request = null): bool
58
    {
59
        return $this->parseRequest($request)->parameters->isNotEmpty();
60
    }
61
62
    /**
63
     * Undocumented function
64
     *
65
     * @return Collection
66
     */
67
    public function parameters()
68
    {
69
        return $this->parameters;
70
    }
71
72
    /**
73
     * Undocumented function
74
     *
75
     * @param string|int $key
76
     * @param mixed      $default
77
     *
78
     * @return mixed
79
     */
80
    public function value($key, $default = null)
81
    {
82
        return isset($this->parameters) ? $this->parameters->get($key, $default) : $default;
83
    }
84
85
    /**
86
     * Undocumented function
87
     *
88
     * @return array
89
     */
90
    public function getQueryParameter(): array
91
    {
92
        return $this->hasQuery() ? [$this->getConfigKey() => $this->parameters()->toArray()] : [];
93
    }
94
95
    /**
96
     * Undocumented function
97
     *
98
     * @param string $separator
99
     *
100
     * @return string
101
     */
102
    public function implode(string $separator = ', '): string
103
    {
104
        return $this->parameters->join($separator);
105
    }
106
107
    public function allowedByServer(): bool
108
    {
109
        return config("jsonapi.{$this->getConfigKey()}.allowed", true);
110
    }
111
112
    public function allowedForRoute($request = null): bool
113
    {
114
        $request = $request ?? request();
115
116
        return $request->routeIs(config("jsonapi.{$this->getConfigKey()}.routes"));
0 ignored issues
show
The method routeIs() does not exist on Illuminate\Http\Request. ( Ignorable by Annotation )

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

116
        return $request->/** @scrutinizer ignore-call */ routeIs(config("jsonapi.{$this->getConfigKey()}.routes"));

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...
117
    }
118
119
    public function allowed($request = null): bool
120
    {
121
        return $this->allowedByServer() && $this->allowedForRoute($request);
122
    }
123
}
124