Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
94.59% |
35 / 37 |
|
91.30% |
21 / 23 |
|
28.57% |
6 / 21 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| BaseParameter | |
94.59% |
35 / 37 |
|
91.30% |
21 / 23 |
|
28.57% |
6 / 21 |
|
80.00% |
4 / 5 |
64.48 | |
0.00% |
0 / 1 |
| __construct | |
92.59% |
25 / 27 |
|
83.33% |
10 / 12 |
|
15.38% |
2 / 13 |
|
0.00% |
0 / 1 |
20.15 | |||
| getDefaultValues | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCurrentValues | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| clearCache | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAnnotations | |
100.00% |
7 / 7 |
|
100.00% |
8 / 8 |
|
20.00% |
1 / 5 |
|
100.00% |
1 / 1 |
12.19 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App; |
| 4 | |
| 5 | use App\Entity\Parameter as ParameterEntity; |
| 6 | use Doctrine\ORM\EntityManagerInterface; |
| 7 | use Psr\Cache\InvalidArgumentException; |
| 8 | use ReflectionClass; |
| 9 | use ReflectionProperty; |
| 10 | use Symfony\Component\Cache\Adapter\FilesystemAdapter; |
| 11 | use Symfony\Contracts\Cache\ItemInterface; |
| 12 | use Throwable; |
| 13 | |
| 14 | class BaseParameter |
| 15 | { |
| 16 | public const string PARAMETER_KEY_CACHE = 'parameter'; |
| 17 | /** |
| 18 | * @var string[] |
| 19 | */ |
| 20 | protected array $defaultValues = []; |
| 21 | /** |
| 22 | * @var array<string|null> |
| 23 | */ |
| 24 | protected array $currentValues = []; |
| 25 | protected FilesystemAdapter $cache; |
| 26 | /** |
| 27 | * @var ReflectionProperty[] |
| 28 | */ |
| 29 | private array $paramProperties; |
| 30 | /** |
| 31 | * @var ?Param[] |
| 32 | */ |
| 33 | private ?array $annotations = null; |
| 34 | |
| 35 | /** |
| 36 | * @throws ExceptionApplication |
| 37 | */ |
| 38 | public function __construct(EntityManagerInterface $entityManager) |
| 39 | { |
| 40 | try { |
| 41 | $this->cache = new FilesystemAdapter(); |
| 42 | // Initializes a 'ReflectionClass' class that allows you to report certain information from a class. |
| 43 | // In this case, the 'Parameter' class. |
| 44 | $paramClass = new ReflectionClass($this); |
| 45 | // Get all the properties of the class as an array of 'ReflectionProperty' objects. |
| 46 | $this->paramProperties = $paramClass->getProperties(ReflectionProperty::IS_PUBLIC); |
| 47 | $paramNames = []; |
| 48 | // Get the 'name' value and the default values of each object present |
| 49 | // in $paramProperties. |
| 50 | foreach ($this->paramProperties as $paramProperty) { |
| 51 | $paramName = $paramProperty->getName(); |
| 52 | // Add the 'name' to the final array. |
| 53 | $paramNames[] = $paramName; |
| 54 | // Adds to the array "$paramName" as key and the default text of the properties as value |
| 55 | /* @phpstan-ignore-next-line */ |
| 56 | $this->defaultValues[$paramName] = $this->{$paramName}; |
| 57 | } |
| 58 | |
| 59 | /** @var ParameterEntity[] $dbParameters */ |
| 60 | $dbParameters = $this->cache->get( |
| 61 | self::PARAMETER_KEY_CACHE, |
| 62 | function (ItemInterface $item) use ($entityManager) { |
| 63 | $item->expiresAfter(7200); |
| 64 | // Selecting the 'parameter' table from the database, using the Doctrine QueryBuilder |
| 65 | $mainQuery = $entityManager->createQueryBuilder(); |
| 66 | $mainQuery->select('parameter') |
| 67 | ->from(ParameterEntity::class, 'parameter'); |
| 68 | |
| 69 | return $mainQuery->getQuery()->getResult(); |
| 70 | }, |
| 71 | 0.0 |
| 72 | ); |
| 73 | // Loop over the array containing all records (stored as objects) of the 'parameter' table |
| 74 | foreach ($dbParameters as $dbParameter) { |
| 75 | // Retrieves the 'name' value of the records |
| 76 | $dbParameterName = $dbParameter->getName(); |
| 77 | // Checks if the 'name' field of the record matches that of one of the parameters |
| 78 | if (in_array($dbParameterName, $paramNames)) { |
| 79 | // retrieve the 'value' of the record |
| 80 | $dbParameterValue = $dbParameter->getValue(); |
| 81 | // changes the value of the parameter in question to that of the database record |
| 82 | // And adds the record to the '$dbParameterValue' table |
| 83 | $this->{$dbParameterName} = $dbParameterValue; |
| 84 | $this->currentValues[$dbParameterName] = $dbParameterValue; |
| 85 | } |
| 86 | } |
| 87 | } catch (Throwable) { |
| 88 | throw new ExceptionApplication('Erreur fatal paramètre DB'); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @return string[] |
| 94 | */ |
| 95 | public function getDefaultValues(): array |
| 96 | { |
| 97 | return $this->defaultValues; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @return array<string|null> |
| 102 | */ |
| 103 | public function getCurrentValues(): array |
| 104 | { |
| 105 | return $this->currentValues; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @throws InvalidArgumentException |
| 110 | */ |
| 111 | public function clearCache(): void |
| 112 | { |
| 113 | $this->cache->delete(self::PARAMETER_KEY_CACHE); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @return Param[] |
| 118 | */ |
| 119 | public function getAnnotations(): array |
| 120 | { |
| 121 | if (null === $this->annotations) { |
| 122 | $this->annotations = []; |
| 123 | foreach ($this->paramProperties as $paramProperty) { |
| 124 | $annotations = $paramProperty->getAttributes(Param::class); |
| 125 | if ($annotations) { |
| 126 | $this->annotations[$paramProperty->getName()] = $annotations[0]->newInstance(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return $this->annotations; |
| 132 | } |
| 133 | } |