Issues (128)

src/AttributeReader/UploadableAttributeReader.php (2 issues)

1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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 Silverback\ApiComponentsBundle\AttributeReader;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Silverback\ApiComponentsBundle\Annotation\Uploadable;
18
use Silverback\ApiComponentsBundle\Annotation\UploadableField;
19
use Silverback\ApiComponentsBundle\Entity\Utility\ImagineFiltersInterface;
20
use Silverback\ApiComponentsBundle\Exception\BadMethodCallException;
21
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
22
use Silverback\ApiComponentsBundle\Exception\UnsupportedAnnotationException;
23
24
/**
25
 * @author Vincent Chalamon <[email protected]>
26
 */
27
final class UploadableAttributeReader extends AttributeReader implements UploadableAttributeReaderInterface
28
{
29
    private bool $imagineBundleEnabled;
30
31
    public function __construct(ManagerRegistry $managerRegistry, bool $imagineBundleEnabled)
32
    {
33
        $this->imagineBundleEnabled = $imagineBundleEnabled;
34
        parent::__construct($managerRegistry);
35
    }
36
37
    public function isConfigured(object|string $class): bool
38
    {
39
        $isConfigured = parent::isConfigured($class);
40
        if (!$isConfigured || $this->imagineBundleEnabled || !is_a($class, ImagineFiltersInterface::class)) {
41
            return $isConfigured;
42
        }
43
        throw new BadMethodCallException(sprintf('LiipImagineBundle is not enabled/installed so you should not configure Imagine filters on %s', \is_string($class) ? $class : $class::class));
0 ignored issues
show
The condition is_string($class) is always false.
Loading history...
44
    }
45
46
    public function getConfiguration(object|string $class): Uploadable
47
    {
48
        $uploadable = $this->getClassAttributeConfiguration($class, Uploadable::class);
49
        if (!$uploadable instanceof Uploadable) {
50
            throw new \LogicException(sprintf('getClassAnnotationConfiguration should return the type %s', Uploadable::class));
51
        }
52
53
        return $uploadable;
54
    }
55
56
    public function isFieldConfigured(\ReflectionProperty $property): bool
57
    {
58
        try {
59
            $this->getPropertyConfiguration($property);
60
        } catch (InvalidArgumentException $e) {
61
            return false;
62
        }
63
64
        return true;
65
    }
66
67
    public function getPropertyConfiguration(\ReflectionProperty $property): UploadableField
68
    {
69
        $attributes = $property->getAttributes(UploadableField::class);
70
        if (!\count($attributes)) {
71
            throw new InvalidArgumentException(sprintf('%s::%s does not have %s annotation', $property->getDeclaringClass()->getName(), $property->getName(), UploadableField::class));
72
        }
73
        /** @var UploadableField $attribute */
74
        $attribute = $attributes[0]->newInstance();
75
76
        if (!$this->imagineBundleEnabled && null !== $attribute->imagineFilters && \count($attribute->imagineFilters)) {
77
            throw new BadMethodCallException(sprintf('LiipImagineBundle is not enabled/installed so you should not configure Imagine filters on %s::$%s', $property->class, $property->getName()));
78
        }
79
80
        return $attribute;
81
    }
82
83
    /**     *
84
     * @return UploadableField[]|\Generator
85
     */
86
    public function getConfiguredProperties(object|string $data, bool $skipUploadableCheck = false): \Generator
87
    {
88
        if (!$skipUploadableCheck && !$this->isConfigured($data)) {
89
            throw new UnsupportedAnnotationException(sprintf('Cannot get field configuration for %s: is it not configured as Uploadable', \is_string($data) ? $data : $data::class));
90
        }
91
92
        $found = false;
93
        $reflectionProperties = (new \ReflectionClass($data))->getProperties();
94
        foreach ($reflectionProperties as $reflectionProperty) {
95
            try {
96
                $config = $this->getPropertyConfiguration($reflectionProperty);
97
                yield $reflectionProperty->getName() => $config;
98
                $found = true;
99
            } catch (InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
100
            }
101
        }
102
        if (!$found) {
103
            throw new UnsupportedAnnotationException(sprintf('No field configurations on your Uploadable component %s.', \is_string($data) ? $data : $data::class));
104
        }
105
    }
106
}
107