Passed
Push — master ( c180c0...233e00 )
by Divine Niiquaye
12:12
created

Schema::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\GraphQL;
19
20
use GraphQL\Language\AST\DocumentNode;
0 ignored issues
show
Bug introduced by
The type GraphQL\Language\AST\DocumentNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use GraphQL\Language\Parser;
0 ignored issues
show
Bug introduced by
The type GraphQL\Language\Parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use GraphQL\Language\Source;
0 ignored issues
show
Bug introduced by
The type GraphQL\Language\Source was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use GraphQL\Type\Schema as TypeSchema;
0 ignored issues
show
Bug introduced by
The type GraphQL\Type\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use GraphQL\Utils\AST;
0 ignored issues
show
Bug introduced by
The type GraphQL\Utils\AST was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use GraphQL\Utils\BuildSchema;
0 ignored issues
show
Bug introduced by
The type GraphQL\Utils\BuildSchema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
27
/**
28
 * Definitions of GraphQL schemas.
29
 *
30
 * @author Divine Niiquaye Ibok <[email protected]>
31
 */
32
class Schema implements \Countable
33
{
34
    private const CACHE_PREFIX = '.cache.php';
35
36
    private array $schemas;
37
    private int $count = 0;
38
39
    public function __construct(array $schemas = [], private $cacheDir = null)
40
    {
41
        foreach ($schemas as $name => $schema) {
42
            if ($schema instanceof TypeSchema) {
43
                $this->schemas[$name] = $schema;
44
                ++$this->count;
45
46
                continue;
47
            }
48
49
            $this->set($name, $schema);
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function count(): int
57
    {
58
        return $this->count;
59
    }
60
61
    public function set(string $name, string|TypeSchema|\Closure $schema): void
62
    {
63
        ++$this->count;
64
65
        if (null !== $cache = $this->cacheDir) {
66
            if (\file_exists($cache = ($cache . $name . self::CACHE_PREFIX))) {
67
                $this->schemas[$name] = AST::fromArray(require $cache);
68
69
                return;
70
            }
71
        }
72
73
        $this->schemas[$name] = $schema = $this->resolveSchema($schema instanceof \Closure ? $schema() : $schema, $cache);
0 ignored issues
show
Unused Code introduced by
The assignment to $schema is dead and can be removed.
Loading history...
74
    }
75
76
    public function get(string $name): ?TypeSchema
77
    {
78
        return $this->schemas[$name] ?? null;
79
    }
80
81
    private function resolveSchema($schema, ?string $cache): TypeSchema
82
    {
83
        if ($schema instanceof TypeSchema) {
84
            return $schema;
85
        }
86
87
        if ($schema instanceof DocumentNode) {
88
            $schema = Parser::parse($schema);
89
        } elseif (\is_string($schema)) {
90
            if (!\is_file($schema)) {
91
                throw new \InvalidArgumentException(\sprintf('Schema file "%s" does not exist.', $schema));
92
            }
93
94
            if ('php' === \pathinfo($schema, \PATHINFO_EXTENSION)) {
95
                return require $schema;
96
            }
97
98
            $schema = Parser::parse(new Source(\file_get_contents($schema), \pathinfo($schema, \PATHINFO_BASENAME)));
99
        }
100
101
        if (null !== $cache) {
102
            if (!\is_dir($directory = \dirname($cache))) {
103
                @\mkdir($directory, 0775, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

103
                /** @scrutinizer ignore-unhandled */ @\mkdir($directory, 0775, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
104
            }
105
106
            \file_put_contents($cache, "<?php\nreturn " . \var_export(AST::toArray($schema), true) . ";\n");
107
        }
108
109
        return (new BuildSchema($schema))->buildSchema();
110
    }
111
}
112