ApiKeyFixture   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildApiKey() 0 13 2
A load() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
6
7
use Cake\Chronos\Chronos;
8
use Doctrine\Common\DataFixtures\FixtureInterface;
9
use Doctrine\Persistence\ObjectManager;
10
use ReflectionObject;
11
use Shlinkio\Shlink\Rest\Entity\ApiKey;
12
13
class ApiKeyFixture implements FixtureInterface
14
{
15
    public function load(ObjectManager $manager): void
16
    {
17
        $manager->persist($this->buildApiKey('valid_api_key', true));
18
        $manager->persist($this->buildApiKey('disabled_api_key', false));
19
        $manager->persist($this->buildApiKey('expired_api_key', true, Chronos::now()->subDay()));
20
        $manager->flush();
21
    }
22
23
    private function buildApiKey(string $key, bool $enabled, ?Chronos $expiresAt = null): ApiKey
24
    {
25
        $apiKey = new ApiKey($expiresAt);
26
        $refObj = new ReflectionObject($apiKey);
27
        $keyProp = $refObj->getProperty('key');
28
        $keyProp->setAccessible(true);
29
        $keyProp->setValue($apiKey, $key);
30
31
        if (! $enabled) {
32
            $apiKey->disable();
33
        }
34
35
        return $apiKey;
36
    }
37
}
38