Passed
Push — master ( 319fcb...7a107a )
by Rafael
04:41
created

PluginConfigAnnotation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 40
c 0
b 0
f 0
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 5 1
B __construct() 0 14 5
A getConfig() 0 3 1
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\Annotation\Plugin;
12
13
use Doctrine\Common\Util\Inflector;
14
15
/**
16
 * Can use this annotation as base to se plugin options.
17
 * Override and define all plugin settings.
18
 */
19
abstract class PluginConfigAnnotation
20
{
21 6
    public function __construct(array $config = [])
22
    {
23
        //if only one value is given, the first property is set with the given value
24 6
        if (isset($config['value']) && count($config) === 1) {
25 6
            $ref = new \ReflectionClass(get_class($this));
26 6
            if ($ref->getProperties()) {
27 6
                $propName = $ref->getProperties()[0]->getName();
28 6
                $this->$propName = $config['value'];
29 6
                $this->config[Inflector::tableize($propName)] = $config['value'];
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
            }
31
        } else {
32 3
            foreach ($config as $key => $value) {
33
                //dynamically added to avoid ide auto-complete for this property
34 3
                $this->config[Inflector::tableize($key)] = $value;
35
            }
36
        }
37 6
    }
38
39
    /**
40
     * Must return the array with plugin config
41
     *
42
     * @return array
43
     */
44 1
    public function getConfig(): array
45
    {
46 1
        return $this->config ?? [];
47
    }
48
49
    /**
50
     * Must return the plugin name to apply this config
51
     *
52
     * @return string
53
     */
54 1
    public function getName(): string
55
    {
56 1
        preg_match('/\w+$/', get_class($this), $matches);
57
58 1
        return Inflector::tableize($matches[0]);
59
    }
60
}
61