2019-01-27 14:14:18 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-01-27 14:14:18 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
|
|
|
use Doctrine\Common\DataFixtures\FixtureInterface;
|
2019-12-16 23:46:27 +03:00
|
|
|
use Doctrine\Persistence\ObjectManager;
|
2019-01-27 14:14:18 +03:00
|
|
|
use ReflectionObject;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
|
|
|
|
class ApiKeyFixture implements FixtureInterface
|
|
|
|
{
|
|
|
|
public function load(ObjectManager $manager): void
|
|
|
|
{
|
|
|
|
$manager->persist($this->buildApiKey('valid_api_key', true));
|
|
|
|
$manager->persist($this->buildApiKey('disabled_api_key', false));
|
|
|
|
$manager->persist($this->buildApiKey('expired_api_key', true, Chronos::now()->subDay()));
|
|
|
|
$manager->flush();
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:49:54 +03:00
|
|
|
private function buildApiKey(string $key, bool $enabled, ?Chronos $expiresAt = null): ApiKey
|
2019-01-27 14:14:18 +03:00
|
|
|
{
|
|
|
|
$apiKey = new ApiKey($expiresAt);
|
|
|
|
$refObj = new ReflectionObject($apiKey);
|
|
|
|
$keyProp = $refObj->getProperty('key');
|
|
|
|
$keyProp->setAccessible(true);
|
|
|
|
$keyProp->setValue($apiKey, $key);
|
|
|
|
|
|
|
|
if (! $enabled) {
|
|
|
|
$apiKey->disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $apiKey;
|
|
|
|
}
|
|
|
|
}
|