Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — master ( 52ba54...22032e )
by Jérémiah
08:20
created

TypeResolver::string2Type()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\Resolver;
13
14
use GraphQL\Type\Definition\Type;
15
use Psr\Cache\CacheItemPoolInterface;
16
use Symfony\Component\Cache\Adapter\ArrayAdapter;
17
18
class TypeResolver extends AbstractResolver
19
{
20
    /**
21
     * @var CacheItemPoolInterface
22
     */
23
    private $cacheAdapter;
24
25 26
    public function __construct(CacheItemPoolInterface $cacheAdapter = null)
26
    {
27 26
        $this->cacheAdapter = null !== $cacheAdapter ? $cacheAdapter : new ArrayAdapter(0, false);
28 26
    }
29
30
    /**
31
     * @param string $alias
32
     *
33
     * @return \GraphQL\Type\Definition\Type
34
     */
35 32
    public function resolve($alias)
36
    {
37 32
        if (null === $alias) {
38 15
            return;
39
        }
40 32
        $item = $this->cacheAdapter->getItem(md5($alias));
41
42 32
        if (!$item->isHit()) {
43 26
            $type = $this->string2Type($alias);
44 24
            $item->set($type);
45 24
            $this->cacheAdapter->save($item);
46 24
        }
47
48 30
        return $item->get();
49
    }
50
51 26
    private function string2Type($alias)
52
    {
53 26
        if (false !== ($type = $this->wrapTypeIfNeeded($alias))) {
54 6
            return $type;
55
        }
56
57 25
        return $this->baseType($alias);
58
    }
59
60 25
    private function baseType($alias)
61
    {
62 25
        $type = $this->getSolution($alias);
63 25
        if (null !== $type) {
64 24
            return $type;
65
        }
66
67 1
        throw new UnresolvableException(
68 1
            sprintf('Unknown type with alias "%s" (verified service tag)', $alias)
69 1
        );
70
    }
71
72 26
    private function wrapTypeIfNeeded($alias)
73
    {
74
        // Non-Null
75 26
        if ('!' === $alias[strlen($alias) - 1]) {
76 4
            return Type::nonNull($this->string2Type(substr($alias, 0, -1)));
77
        }
78
        // List
79 26
        if ($this->hasNeedListOfWrapper($alias)) {
80 5
            return Type::listOf($this->string2Type(substr($alias, 1, -1)));
81
        }
82
83 25
        return false;
84
    }
85
86 26
    private function hasNeedListOfWrapper($alias)
87
    {
88 26
        if ('[' === $alias[0]) {
89 6
            $got = $alias[strlen($alias) - 1];
90 6
            if (']' !== $got) {
91 1
                throw new UnresolvableException(
92 1
                    sprintf('Malformed ListOf wrapper type "%s" expected "]" but got .', $alias, json_encode($got))
93 1
                );
94
            }
95
96 5
            return true;
97
        }
98
99 25
        return false;
100
    }
101
102 23
    protected function postLoadSolution($solution)
103
    {
104
        // also add solution with real type name if needed for typeLoader when using autoMapping
105 23
        if ($solution && !isset($this->getSolutions()[$solution->name])) {
106 1
            $this->addSolution($solution->name, function () use ($solution) {
107 1
                return $solution;
108 1
            });
109 1
        }
110 23
    }
111
112 24
    protected function supportedSolutionClass()
113
    {
114 24
        return Type::class;
115
    }
116
}
117