Completed
Push — master ( c6ec87...10b52b )
by Nikita
01:12
created

GraphQLManager::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace LaraComponents\GraphQL;
4
5
use InvalidArgumentException;
6
use LaraComponents\GraphQL\Contracts\GraphQLManager as GraphQLManagerContract;
7
8
class GraphQLManager implements GraphQLManagerContract
9
{
10
    /**
11
     * The application instance.
12
     *
13
     * @var \Illuminate\Foundation\Application
14
     */
15
    protected $app;
16
17
    /**
18
     * The array of resolved schemas.
19
     *
20
     * @var array
21
     */
22
    protected $schemas = [];
23
24
    /**
25
     * Create a new manager instance.
26
     *
27
     * @param  \Illuminate\Foundation\Application  $app
28
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30
    public function __construct($app)
31
    {
32
        $this->app = $app;
33
    }
34
35
    /**
36
     * Get a schema implementation by name.
37
     *
38
     * @param  string  $name
39
     * @return void
40
     */
41
    public function schema($name = null)
42
    {
43
        $name = $name ?: $this->getDefaultSchema();
44
45
        return $this->schemas[$name] = $this->get($name);
46
    }
47
48
    /**
49
     * Attempt to get the schema from the local cache.
50
     *
51
     * @param  string  $name
52
     * @return \Youshido\GraphQL\Schema\AbstractSchema
53
     */
54
    protected function get($name)
55
    {
56
        return isset($this->schemas[$name]) ? $this->schemas[$name] : $this->resolve($name);
57
    }
58
59
    /**
60
     * Resolve the given store.
61
     *
62
     * @param  string  $name
63
     * @return \Youshido\GraphQL\Schema\AbstractSchema
64
     *
65
     * @throws \InvalidArgumentException
66
     */
67
    protected function resolve($name)
68
    {
69
        $class = $this->getClass($name);
70
71
        if (is_null($class)) {
72
            throw new InvalidArgumentException("Schema [{$name}] is not defined.");
73
        }
74
75
        return $this->app->make($class);
0 ignored issues
show
Documentation introduced by
$class is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
    }
77
78
    /**
79
     * Get the schema configuration.
80
     *
81
     * @param  string  $name
82
     * @return array
83
     */
84
    protected function getClass($name)
85
    {
86
        return $this->app['config']["graphql.schemas.{$name}"];
87
    }
88
89
    /**
90
     * Get the default schema name.
91
     *
92
     * @return string
93
     */
94
    public function getDefaultSchema()
95
    {
96
        return $this->app['config']['graphql.schema'];
97
    }
98
}
99