ApieConfigResolver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
dl 0
loc 95
rs 10
c 1
b 0
f 0
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveConfig() 0 4 1
A getResolver() 0 6 2
A addExceptionsForExceptionMapping() 0 12 1
A createResolver() 0 50 3
A urlNormalize() 0 11 6
1
<?php
2
namespace W2w\Laravel\Apie\Providers;
3
4
use Illuminate\Auth\Access\AuthorizationException;
5
use Illuminate\Auth\AuthenticationException;
6
use Illuminate\Contracts\Cache\LockTimeoutException;
7
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8
use Illuminate\Contracts\Redis\LimiterTimeoutException;
9
use PDOException;
10
use Ramsey\Uuid\Exception\InvalidUuidStringException;
11
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
12
use Symfony\Component\OptionsResolver\Options;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
use Symfony\Component\Serializer\Exception\RuntimeException;
15
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
16
use W2w\Lib\Apie\Annotations\ApiResource;
17
use W2w\Lib\Apie\Resources\ApiResourcesInterface;
0 ignored issues
show
Bug introduced by
The type W2w\Lib\Apie\Resources\ApiResourcesInterface 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...
18
19
class ApieConfigResolver
20
{
21
    private static $configResolver;
22
23
    public static function resolveConfig(array $config)
24
    {
25
        $resolver = self::getResolver();
26
        return $resolver->resolve($config);
27
    }
28
29
    private static function getResolver(): OptionsResolver
30
    {
31
        if (!self::$configResolver) {
32
            self::$configResolver = self::createResolver(require __DIR__ . '/../../config/apie.php');
33
        }
34
        return self::$configResolver;
35
    }
36
37
    private static function createResolver(array $defaults): OptionsResolver
38
    {
39
        $resolver = new OptionsResolver();
40
41
        $resolver->setDefaults($defaults)
42
                 ->setAllowedTypes('use_deprecated_apie_object_normalizer', ['bool'])
43
                 ->setAllowedTypes('resources', ['string[]', ApiResourcesInterface::class])
44
                 ->setAllowedTypes('translations', ['string[]'])
45
                 ->setAllowedTypes('plugins', ['string[]'])
46
                 ->setAllowedTypes('resources-service', ['null', 'string'])
47
                 ->setAllowedTypes('mock', ['null', 'bool'])
48
                 ->setAllowedTypes('mock-skipped-resources', ['string[]'])
49
                 ->setAllowedTypes('base-url', 'string')
50
                 ->setAllowedTypes('api-url', 'string')
51
                 ->setAllowedTypes('disable-routes', ['null', 'bool'])
52
                 ->setAllowedTypes('swagger-ui-test-page', ['null', 'string'])
53
                 ->setAllowedTypes('apie-middleware', 'string[]')
54
                 ->setAllowedTypes('swagger-ui-test-page-middleware', 'string[]')
55
                 ->setAllowedTypes('bind-api-resource-facade-response', 'bool')
56
                 ->setAllowedTypes('metadata', 'string[]')
57
                 ->setAllowedTypes('resource-config', [ApiResource::class . '[]', 'array[]'])
58
                 ->setAllowedTypes('exception-mapping', 'int[]');
59
        $resolver->setDefault('metadata', function (OptionsResolver $metadataResolver) use (&$defaults) {
60
            $metadataResolver->setDefaults($defaults['metadata']);
61
62
            $urlNormalizer = function (Options $options, $value) {
63
                if (empty($value)) {
64
                    return '';
65
                }
66
                return self::urlNormalize($value);
67
            };
68
            $metadataResolver->setNormalizer('terms-of-service', $urlNormalizer);
69
            $metadataResolver->setNormalizer('license-url', $urlNormalizer);
70
            $metadataResolver->setNormalizer('contact-url', $urlNormalizer);
71
        });
72
        $resolver->setNormalizer('resource-config', function (Options $options, $value) {
73
            return array_map(function ($field) {
74
                return $field instanceof ApiResource ? $field : ApiResource::createFromArray($field);
75
            }, $value);
76
        });
77
        $resolver->setNormalizer('contexts', function (Options $options, $value) use (&$defaults) {
78
            return array_map(function ($field) use ($defaults) {
79
                return self::createResolver($defaults)->resolve($field);
80
            }, $value);
81
        });
82
        $resolver->setNormalizer('exception-mapping', function (Options $options, $value) {
83
            ApieConfigResolver::addExceptionsForExceptionMapping($value);
84
            return $value;
85
        });
86
        return $resolver;
87
    }
88
89
    public static function addExceptionsForExceptionMapping(array& $array)
90
    {
91
        $array[AuthorizationException::class] = 403;
92
        $array[AuthenticationException::class] = 401;
93
        $array[UnexpectedValueException::class] = 415;
94
        $array[RuntimeException::class] = 415;
95
        $array[InvalidUuidStringException::class] = 422;
96
        $array[LockTimeoutException::class] = 502;
97
        $array[LimiterTimeoutException::class] = 502;
98
        $array[FileNotFoundException::class] = 502;
99
        $array[PDOException::class] = 502;
100
        return $array;
101
    }
102
103
    private static function urlNormalize($value)
104
    {
105
        if ('http://' !== substr($value, 0, 7) && 'https://' !== substr($value, 0, 8)) {
106
            $value = 'https://' . $value;
107
        }
108
        $parsedUrl = parse_url($value);
109
        if (empty($parsedUrl) || !in_array($parsedUrl['scheme'], ['http', 'https']) || !filter_var($value, FILTER_VALIDATE_URL)) {
110
            throw new InvalidOptionsException('"' . $value . '" is not a valid url');
111
        }
112
113
        return $value;
114
    }
115
}
116