Completed
Push — master ( 2975e7...46c3ee )
by Rafael
07:49
created

NamingConventionFilterResolver::resolve()   F

Complexity

Conditions 16
Paths 400

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 16.3822

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 31
cts 35
cp 0.8857
rs 2.2333
c 0
b 0
f 0
cc 16
nc 400
nop 3
crap 16.3822

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Filter\Resolver;
12
13
use Doctrine\Common\Annotations\Reader;
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\HttpKernel\Kernel;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
use Ynlo\GraphQLBundle\Annotation\Filter;
18
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface;
19
use Ynlo\GraphQLBundle\Definition\ImplementorInterface;
20
use Ynlo\GraphQLBundle\Definition\NodeAwareDefinitionInterface;
21
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface;
22
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
23
use Ynlo\GraphQLBundle\Filter\FilterResolverInterface;
24
25
/**
26
 * Resolve custom filters using naming convention.
27
 * Filters should me placed under Filter folder on each bundle or App namespace
28
 * using the following format:
29
 *
30
 * {BundleNamespace}\Filter\{Node}\{FilterName}
31
 *
32
 * > Filters must implements FilterInterface and has the Filter annotation
33
 */
34
class NamingConventionFilterResolver implements FilterResolverInterface
35
{
36
    /**
37
     * @var KernelInterface
38
     */
39
    private $kernel;
40
41
    /**
42
     * @var Reader
43
     */
44
    private $reader;
45
46
    /**
47
     * @param KernelInterface $kernel
48
     * @param Reader          $reader
49
     */
50 1
    public function __construct(KernelInterface $kernel, Reader $reader)
51
    {
52 1
        $this->kernel = $kernel;
53 1
        $this->reader = $reader;
54 1
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 1
    public function resolve(ExecutableDefinitionInterface $executableDefinition, ObjectDefinitionInterface $node, Endpoint $endpoint): array
60
    {
61 1
        $types = [];
62 1
        if ($node instanceof ImplementorInterface && $node->getInterfaces()) {
63 1
            $types = array_merge($types, $node->getInterfaces());
64
        }
65 1
        if ($node instanceof NodeAwareDefinitionInterface && $node->getNode()) {
66 1
            $types[] = $node->getNode();
67
        }
68
69 1
        $paths = [];
70 1
        $bundles = $this->kernel->getBundles();
71 1
        foreach ($types as $type) {
72
            //search for filters using naming convention inside Bundle/Filter/...
73 1
            foreach ($bundles as $bundle) {
74 1
                $path = "{$bundle->getPath()}/Filter/$type";
75 1
                if (file_exists($path)) {
76 1
                    $paths[$path] = "{$bundle->getNamespace()}\Filter\\$type";
77
                }
78
            }
79
80
            //symfony ^4.0
81 1
            if (Kernel::VERSION_ID >= 40000) {
82
                $path = "{$this->kernel->getRootDir()}/Filter/$type";
83
                if (file_exists($path)) {
84 1
                    $paths[$path] = "App\Filter\\$type";
85
                }
86
            }
87
        }
88
89 1
        $resolvedFilters = [];
90 1
        foreach ($paths as $path => $namespace) {
91 1
            $finder = new Finder();
92 1
            foreach ($finder->in($path)->name('/.php$/')->getIterator() as $file) {
93 1
                $className = preg_replace('/.php$/', null, $file->getFilename());
94 1
                if ($file->getRelativePath()) {
95
                    $subNamespace = str_replace('/', '\\', $file->getRelativePath());
96
                    $fullyClassName = $namespace.'\\'.$subNamespace.'\\'.$className;
97
                } else {
98 1
                    $fullyClassName = $namespace.'\\'.$className;
99
                }
100
101 1
                if (class_exists($fullyClassName)) {
102
                    /** @var Filter $filter */
103 1
                    $filter = $this->reader->getClassAnnotation(new \ReflectionClass($fullyClassName), Filter::class);
104 1
                    if ($filter) {
105 1
                        $filter->resolver = $fullyClassName;
106 1
                        if (!$filter->name) {
107 1
                            preg_match('/\w+$/', $fullyClassName, $matches);
108 1
                            $filter->name = lcfirst($matches[0]);
109
                        }
110 1
                        $resolvedFilters[] = $filter;
111
                    }
112
                }
113
            }
114
        }
115
116 1
        return $resolvedFilters;
117
    }
118
}
119