Completed
Push — master ( e7c5cf...05695e )
by Alejandro
17s
created

ApiKeyFixture   A

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