Completed
Push — master ( c81543...bd2ead )
by Kévin
15s queued 10s
created

FormatsProvider::getFormatsFromAttributes()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Api;
15
16
use ApiPlatform\Core\Exception\InvalidArgumentException;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
19
/**
20
 * {@inheritdoc}
21
 *
22
 * @author Anthony GRASSIOT <[email protected]>
23
 */
24
final class FormatsProvider implements FormatsProviderInterface
25
{
26
    private $configuredFormats;
27
    private $resourceMetadataFactory;
28
29
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, array $configuredFormats)
30
    {
31
        $this->resourceMetadataFactory = $resourceMetadataFactory;
32
        $this->configuredFormats = $configuredFormats;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     *
38
     * @throws InvalidArgumentException
39
     */
40
    public function getFormatsFromAttributes(array $attributes): array
41
    {
42
        if (!$attributes || !isset($attributes['resource_class'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attributes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
43
            return $this->configuredFormats;
44
        }
45
46
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
47
48
        if (!$formats = $resourceMetadata->getOperationAttribute($attributes, 'formats', [], true)) {
49
            return $this->configuredFormats;
50
        }
51
52
        if (!\is_array($formats)) {
53
            throw new InvalidArgumentException(sprintf("The 'formats' attributes must be an array, %s given for resource class '%s'.", \gettype($formats), $attributes['resource_class']));
54
        }
55
56
        return $this->getOperationFormats($formats);
57
    }
58
59
    /**
60
     * Filter and populate the acceptable formats.
61
     *
62
     * @throws InvalidArgumentException
63
     */
64
    private function getOperationFormats(array $annotationFormats): array
65
    {
66
        $resourceFormats = [];
67
        foreach ($annotationFormats as $format => $value) {
68
            if (!is_numeric($format)) {
69
                $resourceFormats[$format] = (array) $value;
70
                continue;
71
            }
72
            if (!\is_string($value)) {
73
                throw new InvalidArgumentException(sprintf("The 'formats' attributes value must be a string when trying to include an already configured format, %s given.", \gettype($value)));
74
            }
75
            if (array_key_exists($value, $this->configuredFormats)) {
76
                $resourceFormats[$value] = $this->configuredFormats[$value];
77
                continue;
78
            }
79
80
            throw new InvalidArgumentException(sprintf("You either need to add the format '%s' to your project configuration or declare a mime type for it in your annotation.", $value));
81
        }
82
83
        return $resourceFormats;
84
    }
85
}
86